| 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(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{ 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.
A for loop repeats a code block for a set number of times. It is divided into three sections:
cout << i << "\n"; } The numbers 0 to 9 are printed on the screen in this example. |
|