User blog comment:Nayuta Ito/Brainfuck/@comment-1605058-20160703080113

Here is explanation why multiplication program works. The program operates on four cells, which I will call A, B, C, D (from left to right) and is initialized with number n in cell A, m in cell B, 0 in cells C and D, and the pointer starts at cell B. I will denote this state by |n ^m 0 0|, where ^ indicates pointer's position.

First we decrease B by one, move to A and start a loop: [->>+>+<<<] If you stare at this for a second, you will notice that this clears the contents of A and adds n to values stored in C and D. The contents are now |^0 m-1 n n|. Afterwards, this piece of code: >>>[-<<<+>>>] moves the content of D to A, so that the state is |n m-1 n ^0|. Now we pointer to cell B and the main loop restarts (unless m-1=0).

If you do a similar analysis of what happens with each run of the main loop, you will see that what changes in the end is: B is decreased by 1, C is increased by n. Since we break out of the main loop exactly when B hits 0, that is, when the main loop was executed m times, so C was increased by n m times, we see that number nm is now stored in C and ready to be returned.