Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

kris100 Group Title

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

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. Muhammad.Ateeq.Azam Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • 2 years ago
  2. Muhammad.Ateeq.Azam Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • 2 years ago
  3. ktobah Group Title
    Best Response
    You've already chosen the best response.
    Medals 5

    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.

    • 2 years ago
  4. Tomas.A Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    did u write it urself? :D

    • 2 years ago
  5. ktobah Group Title
    Best Response
    You've already chosen the best response.
    Medals 5

    You mean me @Tomas.A ?

    • 2 years ago
  6. Tomas.A Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    well yes

    • 2 years ago
  7. ktobah Group Title
    Best Response
    You've already chosen the best response.
    Medals 5

    Yeah, it was from my brain lol

    • 2 years ago
  8. LemuelPogi Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 2 years ago
  9. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @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.

    • 2 years ago
  10. ktobah Group Title
    Best Response
    You've already chosen the best response.
    Medals 5

    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

    • 2 years ago
  11. Tomas.A Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
  12. ktobah Group Title
    Best Response
    You've already chosen the best response.
    Medals 5

    Haha thanks Tomas.

    • 2 years ago
  13. kris100 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
  14. ktobah Group Title
    Best Response
    You've already chosen the best response.
    Medals 5

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

    • 2 years ago
  15. kris100 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
  16. kris100 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 2 years ago
  17. ktobah Group Title
    Best Response
    You've already chosen the best response.
    Medals 5

    You are welcome, glad to hear that.

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.