In the first iteration of the loop in the above example, '*' gets printed and the condition ( n == 2 ) is checked. Since the value of n is 1, therefore the condition becomes false and n++ increases the value of 'n' to 2. Again '*' gets printed and since this time the condition of if satisfies, the break statement terminates the loop.

This is an infinite loop. To terminate this we are using break. If a user enters 0 then the condition of if will get satisfied and the break statement will terminate the loop.

continue

continue statement works similar to break statement. The only difference is that break statement terminates the loop whereas continue statement passes control to the conditional test i.e., where the condition is checked.

In short, it passes control to the nearest conditional test in do...while loop, or the condition of while in while loop, or the condition of for in for statements skipping the rest of the statements in the loop.

In the above example, notice that n = 5 is not printed because as the value of 'n' becomes 5, the if condition becomes true and the statements in the body of the if statements get executed. Thus n = n+1 increases the value of 'n' to 6 and continue statement passes the control to the test condition and rest of the statements are not executed ( cout << "n = " << n << endl; n++; - these two statements are not executed when n is 5 ). Then again the iteration starts and the numbers get printed from 6 onwards.

Knowing is not enough, we must apply. Willing is not enough, we must do.-Bruce Lee