while Loop in C++
Loops programming construct is used to execute one or more instructions repeatedly until some condition is satisfied.
The statements inside while loop gets executed until the condition become FALSE. while loop also known as iterative statement. To iterate means to repeat. If we want to repeat execution of some action or statements several times, we use LOOPs.
Practice Programs
(i)
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
system("color fc");
int x;
x = 0;
while(x<5)
{
cout<<"Stephen Hawking"<<endl;
x++;
}
return 0;
}
(ii)
#include<iostream>
using namespace std;
int main()
{
int x, y;
x=1;
while(y = 5)
{
cout<<"Stephen Hawking"<<endl;
x++;
}
return 0;
}
(iii)
#include<iostream>
using namespace std;
int main()
{
int x, y = 10;
x=2;
while(y == 10)
{
cout<<"Stephen Hawking"<<endl;
x++;
}
return 0;
}
(iv)
#include<iostream>
using namespace std;
int main()
{
int i, x, y;
x = 2;
y = 3;
i=2;
while(x+y)
{
cout<<"Stephen Hawking"<<endl;
i++;
}
return 0;
}
(v)
#include<iostream>
using namespace std;
int main()
{
int i, x, y;
x = 2;
y = 3;
i=2;
while((x+y)>0)
{
cout<<"Stephen Hawking"<<endl;
i++;
}
return 0;
}
(vi)
#include<iostream>
using namespace std;
int main()
{
int i, x, y;
x = 2;
y = 3;
i=2;
while((x+y)<0)
{
cout<<"Stephen Hawking"<<endl;
i++;
}
return 0;
}
(vii)
#include<iostream>
using namespace std;
int main()
{
int i;
i=2;
while(i<=5)
{
cout<<"Stephen Hawking"<<endl;
cout<<"Galaxy"<<endl;
i++;
}
return 0;
}
(viii)
#include<iostream>
using namespace std;
int main()
{
int i, x, y;
for(i = 2; i<=5; i++)
cout<<"Stephen Hawking"<<endl;
cout<<"Cosmologist"<<endl;
cout<<"Galaxy"<<endl;
return 0;
}
(ix)
#include<iostream>
using namespace std;
int main()
{
int i, x, y;
i=2;
while(i<=5)
{
cout<<"Stephen Hawking"<<endl;
cout<<"Cosmologist"<<endl;
cout<<"Galaxy"<<endl;
i++;
}
return 0;
}