#Mastery20 Use of loops with for

The for repetition structure handles all the details of counter-controlled repetition. The picture shows an example.

The general format of the for structure is

for ( initialization statement; loop continuation condition; increment )

When the for structure begins executing, the control variable i is declared and inicialized to 0. Then, the loop-continuation i <= size is checked. The inicial value of is 0, so the condition is satisfied and the body statement prints the value of i, namely 0. Then the expresion i++  increments control variable i and the loop begins again with the loop-continuation test. The control variable is now equal to 1, so the final value is not exceeded and the program performs the body statement again. This process continues until the control variable i is incremented upper than the value size given by the user- this causes the loop-continuation test to fail and repetition to terminate. The program continues by performing the first statement after the for structure.

You can check my Github repository -TC1017 to see more code: https://github.com/Auralgo/-TC1017

 

From: “C++ How to program” by Deitel.

CC BY 4.0 #Mastery20 Use of loops with for by Aurora Alvarado is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.