for loop have following sintax: for(init-statement; condition; loop-statement) body;init-statement executes once before loop as whole. Usually you will place loop variable initialization here (int i = 0) condition is checked each time when loop is about to run again. if it is true, body of the loop executes, if false — statement after loop executes. (i < 10) loop-statement executes after each iteration. Usually you will change loop variable here. (++i) body is a statement (or several statements encased in curly brackets) which executes on each iteration. (std::cout << '*';)
if you combine all examples on parts of the loop ypu will get:
for (initialization; condition; increase) statement;
and its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.
It works in the following way:
1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.
2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).
3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.
4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.
Some notes for you:
a) (int)number/2 will be parsed as ((int)number) / 2
b) However it is an integer division, so result will always be integer.
c) In C++ you should use static_cast<int>(variable), not C-style (int)variable