Question:
While and Do loop differences (C++)?
Owen Chayner
2011-03-11 05:15:27 UTC
Hey guys,

I have recently started learning C++ by a book. I am trying to get a head start because i really have a passion for computers, and i want to do software engineering at uni. Anyways, as I so far understood, the difference between a while and a do loop is that before the loop is started, (in the while loop) the int that we are testing is already initialized.
For example:
char x = 'y';
while (x == 'y' )
{
cout <<"Play again?(y/n)
cin >> x;
}
cout <<"Okay, bye. ";

While using a do loop, we are testing it without know anything about the char (in this case).

Is that right? I really think that that is not the case, but that is how i understand it at the moment. The book doesn't really tell me that much on when to use one or the other loop. Can you guys explain this for me please?

Cheers.
Four answers:
Silent
2011-03-11 05:24:53 UTC
No, that's not quite right.



The main difference between a while loop and a do-while loop lies in when the condition is evaluated. With a while loop, it's evaluated before the loop is run; with a do-while loop, it's evaluated after the loop is run



In other words, with a while loop, the procedure goes like this:



1. Check the condition. If it's true, go to step 2. Otherwise, move on.

2. Run the code in the body of the loop.

3. Go to step 1.



With a do-while loop, the procedure goes like this:



1. Run the code in the body of the loop.

2. Check the condition. If it's true, go to step 1. Otherwise, move on.



You can see from this that a do-while loop will always run at least once, even if the condition is never true. In contrast, a while loop whose condition is never true will never run. The do-while loop is therefore used primarily in situations when you want to make sure the code inside always runs at least once.
Emarketingdesk Innovative
2011-03-11 05:37:42 UTC
In While loop the condition is tested first and then the statements are

executed if the condition turns out to be true.

In do while the statements are executed for the first time and then the

conditions are tested, if the condition turns out to be true then the

statements are executed again.



A typical scenario to use do While loop.

I would like to get a specified input from user. Here first I will get the

input then I will check whether we got the specified input other wise we

will again ask for the input.

eg.,

do

{

char input;

printf("say yes or no :(y/n)";

input = getchar();

}while(!(input == 'y' || input == 'n'));



Techblog

http://www.innovativeinfosolutions.com/techblog
?
2016-10-02 07:37:24 UTC
together as loop repeats specific statements based on the circumstance checked for loop facilitates in initialisation of a variable and repeats specific statements in keeping with circumstance checked and likewise facilitates in increment/decrement of the variable
?
2011-03-11 05:22:30 UTC
while loop runs only if statement is true

do while will run at-least once even if statement is false .

int a=0

do{

cout<<"i will run once";

}

while(a==2);



output is i will run once .



while(a==2)

{

cout<<"i will run once";

}



output is nothing , loop the program did not enter the loop .


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...