While Loop

How to use the while loop? What is the difference between while and do-while?
simple example pls.
1
2
3
4
5
6
7
8
9
10
11
// While
while(condition)
{
   // Code to execute
}

// Do-While
do
{
   // Code to execute
} while (condition);


Difference is that the while loop may never run because the condition may not be met. The do-while loop will always run at least once, then the condition will be assessed to see if further iterations are needed.
#include<iostream>
using namespace std;
main()
{
while(pnum!=cnum)
{
if(pnum>cnum)
cout<<"Lower!"<<endl;
else
cout<<"Higher !"<<endl;
cout<<"Guess again! ";
cin>>pnum;
ctr++;

}
}

#include<iostream>
using namespace std;
main()
{
do
{
if(pnum>cnum)
cout<<"lower!"<<endl;
else
cout<<"higher !"<<endl;
cout<<"guess again! ";
cin>>pnum;
ctr++;

}while(pnum!=cnum);
}

this is a sample of while and do while loop just make the observation to understand it
Last edited on
If I want to print odd numbers between 0-10, how to print it using while and do while?
You'll need to figure that out. You won't learn if somebody gives you the solution outright.

Have a go.
That is quite basic, i'd prefer googling it before trying a forum. But here is a simple answer to your question.

Firstly, syntax

1
2
3
4
5
while (condition)
{
      //... expression  

} // end of while 


1
2
3
4
5
6
do
{
      //.. expression

} while(condition);



the while loop is designed to run only if the condition inside the parenthesis remains true. It will only execute if the condition is TRUE. else, it will not run, unlike the do while loop, which will run AT LEAST 1 time.

For example,

1
2
3
4
5
6
int x = 5;
do
{
    std::cout<<"inside do while loop, only once though !!!" ;

} while (x!=5); 


in the above code, even though the condition is false, the loop will execute at least 1 time. This is the advantage of do while. It will run at least one time regardless of the condition.
whereas...

1
2
3
4
5
int x = 5;
while(x!=5)
{
   std::cout<<"inside while loop";
}


The above code will not execute as the condition is false.

Hope this helps :)
how to find odd numbers between 0-10 using a while loop?
Seriously, this isn't that hard. Your reluctance to have a go makes me reluctant to help.

Try it yourself first. Post your progress.
Is this the correct way?

#include <iostream>
using namespace std;

int main()
{
int x = 0;


while (x < 10)
{
if (x % 2 != 0)
cout << x << endl;
x++;
}

system ("pause");
return 0;
}
Did u try it out ? :p
Looks good to me.

Is it printing out odd numbers?
yes. is it wrong? if there is a wrong part please let me know.
It is fine. You might want to read this article on system() functions. http://www.cplusplus.com/articles/j3wTURfi

Please use code tags next time.
Okay but I tried these codes not works as I wish. Can I know what can I use behalf of system("pause"); ?
Okay but I tried these codes not works as I wish. Can I know what can I use behalf of system("pause"); ?


http://cplusplus.com/forum/beginner/1988/ .
I can not understand why you guys don't understand my need. I need to prevent closing my console app in the end. for this I used system ("pause"). other options in the site didn't help me to do it. Kindly send it to me with an example. Thanks
yes. is it wrong? if there is a wrong part please let me know.

that's weird, we're the one should ask you that question (my opinion dude).

I can not understand why you guys don't understand my need. I need to prevent closing my console app in the end. for this I used system ("pause"). other options in the site didn't help me to do it. Kindly send it to me with an example. Thanks

that's one example. you make me thinking: do you want us to just give you an answer without any learning process? (i don't mean to be cocky here :) )
Topic archived. No new replies allowed.