Posts

Showing posts with the label for statement

for statement in c plus plus

Image
The for statement The syntax of the for statement is: For (initialization; test expression; update expression) { // Boby of loop – block of statements } The three parts of the for statement determine the loop actions. First, the loop initializes the relevant variables. Then there is a test expression, evaluated as true or false. If the test expression is TRUE (this means the test expression is non-zero) the sequence of instructions which forms the body of the loop is executed. Finally, expressions are updated and the process returns to the test expression part of the loop. (The initialization in a loop takes place only when the program enters the loop.) The sum program can be rewritten using for statement as follows: int sumSoFar = 0; //(Start of loop; test condition; increment the loop variable;) for ( int i=1; I < =100; I = i+1)  { sumSoFar = sumSoFar +I; // This is the body of the loop } Example of for loop Here are some examples of simple for loop...