do-while loop

why do we use the Do-While loop? What's the point of using it? what is the difference between do-while loop and the for loop?

What does the following loop do?

#include <iostream>
using namespace std;
int main ()
{
int num = 0;

do

{

cin>> num;

}while (num < 0 || num >100);
system ("pause")
return 0;
}
The biggest difference is that a do/while() loop will always be executed at least once. A while() loop and a for() loop may fail to run at all if the condition initially evaluates to false.

Perhaps you should find and read some documentation for the different types of loops. This is a good starting point: http://www.cplusplus.com/doc/tutorial/control/
And perhaps you should bookmark this link: http://www.cplusplus.com/doc/tutorial/



Thanks!
so in that case, the code above will let me enter a number at least once, right?
what about the condition? When will the loop quit executing the cin statement?
Did you read the links I posted?

When will the loop quit executing the cin statement?


It may never end, depending on what you entered into the cin statement. But if you enter an integer that causes the condition statement to evaluate to false the while will end.

Please use code tags when posting code.

Thank you for your help! I read the links and they were very helpful.
I am sorry if I didn't use any code tags. I am new to the website and I think it will take some time to get used to it :P

Thanks again!
1
2
3
4
5
6
7
do{
}while(true)

if(true)

while(true){
}

They check to see if the condition is true. Your minor example also fails as it sets answer to 34, and if you meant answer == 34 it is still evaluating if it is true or not. If it is true it will loop again, if it is false it jumps out of the loop.
Last edited on
Topic archived. No new replies allowed.