CodesJava

Easy learning with example program codes

CPP While Loop

CPP While Loop is a loop statement which can be used in various forms to execute a block of code continuously as long as the condition of the loop is true, and stops only when the condition fails. These are:

While Loop :

While loop is used to execute a group of action as long as the specified condition is true.

 

Syntax:

while (condition)

{

 code to be executed if the condition is true;

}

 Nested While Loop :

Nested While loop is While loop inside a While loop and so on which is used to run multiple loop conditions.

 

Syntax:

while (condition)

{

while (condition)

{

 code to be executed if the condition is true;

}

}

Infinitive While loop :

Infinitive While loop is a While loop with condition 1 and thus running for infinite number of times.

 

Syntax:

while (1)

{

code to be executed;

}

 

Example of While loop

#include <iostream>
using namespace std;
int main()
{
int i = 1,j = 1;
while (i <= 10)
{
cout << i << endl;
for (j = 1; j <= i; j++)
{
cout << j;
}
cout << endl;
i++;
}
return 0;
}

Output

1         
1                
2            
12             
3          
123                     
4           
1234           
5            
12345              
6                         
123456                   
7                             
1234567           
8                                
12345678                      
9           
123456789        
10                                
12345678910
Please follow and like us:
error
Posted in CPP   








Copyright © 2019 CodesJava DMCA.com Protection Status