1.

Iterative statements

Answer»

Suppose you want to write  “Write it hundred times” hundred times. One way to do this is to use cout 100 times. But don’t worry, there’s an easy way. You can use iterative statements to write the same statements 100 times.

  • while loop
int i=1;
while(i <=100){
cout << "Write it hundred times \n";
i++;
}

The above code will print the statement 100 times (i.e. until the value of i is less than 101).

  • do-while loop
int i=1;
do{
cout << "Write it hundred times \n";
i++;
}
while(i<=100);

The do-while loop is very similar to the while loop. The difference is that in the while loop the condition is checked first; and in the do-while loop, the condition is checked after certain tasks have been performed.

  • for loop

A for loop repeats a code block for a set number of times. It is divided into three sections:


  1. Initializing a counter.

  2. Condition

  3. The counter increment/decrement

for (int i = 0; i < 10; i++) {
cout << i << "\n";
}

The numbers 0 to 9 are printed on the screen in this example. 




Discussion

No Comment Found