How To Use C++ Loops Explain With Example

How To Use C++ Loops Explain With Example
How To Use C++ Loops Explain With Example 

How To Use C++ Loops Explain With Example

Loop: To run the body statement continuously until a required condition is fulfilled is called looping. 

It is used to perform looping operations when the condition is false the execution of the loop will be terminated.

While loop: While loop when program execution starts before condition check then statement execution. while loop is the most useful loop.it is called the while loop.


Syntax of while loop

while(condition)

{

//statement

Increment/decrement

}


How To Use while loop in C++?

#include <iostream>

using namespace std;

int main()

{

int a=1;

while(a<=10)

{

cout<<" "<<a;

a++;

}

*****OUTPUT*****

1 2 3 4 5 6 7 8 9 10


How To Use while loop user input in C++?

#include <iostream>

using namespace std;

int main()

{

int i=1,n;

cout<<"Enter any number:";

cin>>n;

while(i<=10)

{

cout<<" "<<n*i;

i++;

}

*****OUTPUT*****

Enter any number:4

4 8 12 16 20 24 28 32 36 40


Do-while loop: A do-while loop is similar to the while loop with one important difference. the do-while loop is executed at least one time. do-while loop if the test expression is true the body of the loop is executed again and test expression is evaluated.

Some Others Languages Loops

👉C Loops 

👉Java Loops 

👉Python Loops


Syntax of do-while loop

do

{

//statement

while(condition);

}


How To Use do-while loop in C++?

#include <iostream>

using namespace std;

int main()

{

int i=1;

do 

{

cout<<" "<<i;

i++;

}

while(i<=10);

}

*****OUTPUT*****

1 2 3 4 5 6 7 8 9 10


How To Use do-while loop user input in C++?

#include <iostream>

using namespace std;

int main()

{

int i=1,n;

cout<<"Enter any number:";

cin>>n;

do

{   

{

cout<<" "<<n*i;

i++;

}

}

while(i<=10);

}

*****OUTPUT*****

Enter any number:5

5 10 15 20 25 30 35 40 45 50


For Loop: A for loop in three-parts initialization, condition, and increment/decrement. 

An initialization part is executed only one time and then a condition check.

for loop is very easy to understand loop because three-parts only one line. it is called the for a loop.


Syntax of for loop

for(initialization;condition;increment/decrement)

for(int i=1;i<=10;i++)

{

//Statement;

}


How To Use for loop in C++? 

#include <iostream>

using namespace std;

int main()

{

for(int i=1;i<=10;i++)

{

cout<<" "<<i;

}

}

*****OUTPUT*****

1 2 3 4 5 6 7 8 9 10


How To Use for loop user input in C++?

#include <iostream>

using namespace std;

int main()

{

int n;

cout<<"Enter any number:";

cin>>n;

for(int i=1;i<=10;i++)

{

cout<<" "<<i*n;

}

}

*****OUTPUT*****

Enter any number:7

7 14 21 28 35 42 49 56 63 70

 

Nested Loop: A nested loop inside another loop is called the nested loop.

Syntax of Nested loop

for(initialization;condition;increment/decrement)

{

for(initialization;condition;increment/decrement)

{

Statement

}

Statement

}


How To Use Nested loop input in C++?

#include <iostream>

using namespace std;

int main()

{

for(int i=1;i<=10;i++)

{

cout<<" "<<i;

}

}

*****OUTPUT*****

1 2 3 4 5 6 7 8 9 10


How To Use Nested Loop user input in C++?

#include <iostream>

using namespace std;

int main()

{

int n,x,num;

cout<<"Enter any number:";

cin>>num;

for(int i=1;i<=num;i++)

{

n=i,x=0;

for(int j=2;j<=n-1;j++

{

if(n%j==0)

{                                                      

x=1;

break;                                                                    

}

}                           

if(x==0)                          

cout<<" "<<n;

}

}

*****OUTPUT*****

Enter any number:25

1 2 3 5 7 11 13 17 19 23




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.