C++ Programming Code Examples C++ > For Loops and While Loops Code Examples for loop is something similar to while loop but it is more complex. for loop is something similar to while loop but it is more complex. for loop is constructed from acontrol statement that determines how many times the loop will run and a command section. Command section is either a single command or a block of commands. //for single statement for(control statement) statement; //for multiple statement for(control statement) { block of statement } Control statement itself has three parts: for ( initialization; test condition; run every time command ) Initialization part is performed only once at for loop start. We can initialize a loop variable here. Test condition is the most important part of the loop. Loop will continue to run if this condition is valid (true). If the condition becomes invalid (false) then the loop will terminate. Run every time command section will be performed in every loop cycle. We use this part to reach the final condition for terminating the loop. For example we can increase or decrease loop variable's value in a way that after specified number of cycles the loop condition becomes invalid and for loop can terminate. #include <stdio.h> int main () { int a; /* for loop execution */ for( a = 10; a < 20; a = a + 1 ){ printf("value of a: %d\n", a); } return 0; }