## kris100 3 years ago Can anyone teach me how to do a "do while loop" in c++. and give me an easy , moderate and hard examples. :))

int main() { int x=5; do{ cout<<x<<endl; x++; }while(x!=10); }

do while loop is used and it is iterated at least once

3. ktobah

Well to understand this let write down the general form of the While and do while loops: 1- While : while(condition){ // Do some work } 2- Do while : do{ // Some work }while(condition); We start with the condition, it's a logical expression, it can be simple or complex, usually the condition is made from mathematical operators (<,>,=,<=,>=), or logical operators (And [&], Or [|], Not [!]), lets take an example : Simple condition : 3 < 5 --> when you evaluate this, it's true forever. 10 >= 20 --> when you evaluate this, it's false forever. Complex condition : ( 3 < 5 ) & ( 10 >= 20 ) --> It's (true) and (false) = false. ( 3 < 5 ) & ! ( 10 >= 20 ) --> It's (true) and Not (false) = (true) and (true)= true. So that was about the condition, just a NOTE, you must never put an affectation in a condition, like this : if (a = 2) Forbidden, because in programming languages the sign '=' means affectation, the sign of equality is '==', means you can write : if (a == 2) So what the while loop does ? It first evaluate the condition, if it's true it enter the loop and start doing its work, else (the condition is false) it exits the loop. That mean, the while loop can iterate 0, 1 or N time. '0' time if the condition is false from the beginning, '1' time when the condition is true just for one time and 'N' time the other case. Now, what the do while loop does ? At first it doesn't evaluate the condition (why ? Easily because as we all know the execution of a program is sequential from the top to the bottom, in the case of do while loop, we have 'do' then the 'condition'), So the do while loop will at least execute its work 1 time (means do 1 iteration at least). After that if evaluate the condition, if it's 'true' then it will move to the next iteration else ('false') it will exit. Note : for both the while and do while loops, if you are using for example an integer 'i' and you iterate according to it [example : while(i<5)], then you must INITIALIZE 'i' before the loop and INCREMENT 'i' inside the loop. Finally, I'm going to give you an example of each one : 1- While loop: int i = 1; // The Note, I initialized 'i' before the loop. while(i <= 2){ print("Hello Kris100"); i = i+1; // The Note, I increment 'i' inside the loop. } Execution : iteration 1 : i = 1 i <= 2 --> Yes, so print "Hello Kris100", then increment i, i = 2. iteration 2 : i = 2 i <= 2 --> Yes, so print "Hello Kris100", then increment i, i = 3. iteration 3 : i = 3 i <= 2 --> No, so exit the loop. The result of this loop will be : Hello Kris100 Hello Kris100 2- Do While loop: int i = 1; // The Note, I initialized 'i' before the loop. do{ print("Hello Kris100"); i = i+1; // The Note, I increment 'i' inside the loop. }while(i <= 2); Execution : iteration 1 : i = 1 print "Hello Kris100", then increment i, i = 2. i <= 2 --> Yes, so iterate. iteration 2 : i = 2 print "Hello Kris100", then increment i, i = 3. i <= 2 --> No, so exit the loop The result of this loop will be : Hello Kris100 Hello Kris100 I hope that helps you a little bit, good luck. ktobah.

4. Tomas.A

did u write it urself? :D

5. ktobah

You mean me @Tomas.A ?

6. Tomas.A

well yes

7. ktobah

Yeah, it was from my brain lol

8. LemuelPogi

just remember the difference between do while and while statements is that in do while is will run and check if the statement is true... if true then run. But in While it will check first if the statement is true then it will run, also while looks like this: while(condition) { } but do while looks like this do { }while(condition); note that the do while ends with a semicolon xD

9. farmdawgnation

@ktobah That was a spectacular answer. I won't lie, I did some google searching to make sure it wasn't stolen. I was impressed. haha.

10. ktobah

Haha I'm happy that you like it, and glad that you are a honest person haha, and that u take time to be sure it isn't stolen, cool

11. Tomas.A

lol i checked it in google too :D for the same reason cause its awesome :P

12. ktobah

Haha thanks Tomas.

13. kris100

ktobah is awesome is good at c++ and other programming languages. like a master in programming!

14. ktobah

Haha thanks @kris100 but I'm not a master lol, I just know a little bit.

15. kris100

base on what i read on your replies on my problem its all correct. it means your a master. lol

16. kris100

Anyways thanks @ktobah i got good grade in c++ this last preliminary exam. yey! you helped me improve my knowledge in c++! THANKS!

17. ktobah

You are welcome, glad to hear that.