0

while

I dont understand it well . Can anyone give a breif summary about it???

23rd Oct 2017, 7:51 AM
hussein fneish
hussein fneish - avatar
1 Odpowiedź
+ 9
Suppose you want to print from 1000 to 2000. Since writing - cout<<"1000"; cout<<"1001"; .... cout<<"2000"; is not a good practice, you'll have to use loop. There are 3 types of loops : while, for, do-while. To write a while loop you'll have to keep 4 components in mind: 1. initialization: where to start from (like 1000) 2. body of loop (like, cout << 1000) 3. iteration: what will be the next value (like 1001, 1002 ... they are incremented by one) 4. condition: when to stop (like when it's 2000, we won't go for 2001) So, the code should be like: int i = 1000; // initialization, it executes once while ( i <= 2000) // condition cout << i << endl; // loop body i = i + 1; // iteration } So, i starts from 1000. Then the condition is checked - Is i less than or equal to 2000? If yes, i is printed and incremented, so it becomes 1001. Again the condition is checked and the whole process is repeated until i becomes 2001. when i is 2001, the condition is false and the loop is stopped.
23rd Oct 2017, 8:13 AM
Shamima Yasmin
Shamima Yasmin - avatar