0
Can somebody tell me how to use loop
loop
2 Answers
+ 1
loop basically used to done those work which we have to done many time.
0
There are three kinds of loops. for, do-while and while.
for:
for( inizialitation of variable; test to continue doing the loop; what to do in each step) { ... }
an example:
for ( int i = 0; i < 10; i++) { ... }
this can be traduced to:
First, create a variable i and assign 0 to it.
Each step, if i < 10 is true continue doing the loop and increment i by 1.
do while:
do { statements } while (condition);
The name says it. Do what is inside the curly braces while the condition is true.
example:
int i = 0;
do { cout << i++; } while (i < 10);
This prints the numbers 0 to 9.
while:
while ( condition ) { ... };
Exactly the same as do while, except for, if the condition is false in the loops first iteration, the code inside the while doesn't execute, while in the do-while, if the condition is false at first, the code inside executes once.