While loop

While loop in Cpp programming language

While loop in Cpp programming language

We will discuss in this tutorial about While loop in Cpp programming language

Loops in Java

In Java, there are three types of loops:

While loop in C++

While loop in Cpp programming language

While loop

C++ while loop is used to execute statement(s) repeatedly until the particular condition is satisfied. Initially, test expression is evaluated and if the outcome is true then the statements inside the while loop(loop body) are executed. Whereas, when the test expression returns false, the flow of control comes out of the loop or exits from the while loop and goes to the next statements after while loop.

Declaration

The syntax of the while loop

while(Test_expression)
{
//Body of loop
//statement(s)
}

How to work while loop in C++

 

Flow diagram – while loop in C++

In the above flowchart, Initially, the test expression is evaluated by while loop

When the test expression is true, codes inside the body of the while loop is executed.

This action repeated until the condition returns false.

When condition returns false, the flow of control skips the execution.

The flow of control comes out of the loop and jumps to the next statement in the programme.

 

Example

Program 1

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    cout << "While loopin C++" << endl;
    int i=1;
    while(i<=10){
        cout<<i<< endl;
        i++;
    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

while loop in C++
1
2
3
4
5
6
7
8
9
10

 

This is a very simple program for beginners to display 1 to 10 number(Positive) using while loop in C++

In the above program

Initially, i=1(already assigned) text expression i<=10 is true and display 1, then i is updated 2

then, i=2, text expression i<=10 is true and displays 2, then i is updated  to 3

then, i=3, text expression i<=10 is true and displays 3, then i is updated to 4

then, i=4, text expression i<=10 is true and displays 4, then i is updated to 5

then, i=5, text expression i<=10 is true and displays 5, then i is updated to 6

then, i=6, text expression i<=10 is true and displays 6, then i is updated to 7

then, i=7, text expression i<=10 is true and displays 7, then i is updated to 8

then, i=8, text expression i<=10 is true and displays 8, then i is updated to 9

then, i=9, text expression i<=10 is true and displays 9, then i is updated to 10

then, i=10, text expression i<=10 is true and display 10, then i is updated to 11

finally, i=11, text expression i<=10 is false hence while loop is terminate thus flow of control skips from the loop.

 

Program 2

Find total value of 1 to 1000 positive numbers

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    //find sum of 1 - 1000 positive integer
    int sum=0;//variable declaration
    int i=0;
    while(i<=1000){
         sum+=i; //equal to sum=sum+i
    i++;
    }
cout<<"Total value of 1 to 1000 of positive numbers :"<< sum<<endl;

    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Total value of 1 to 1000 of positive numbers :500500

 

Find the factorial value of given numbers

Program 3

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{     //calculate factorial of given number
    int num,i=1;
   int fact=1;//variable declaration
    cout<<"Enter the number for calculate factorial\n";

    cin>>num;
    while(i<=num){
        fact*=i;
        i++;
    }
        cout<<"Factorial value of:"<< num<<"= is"<<fact <<endl;

    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

Enter the number of calculate factorial
6
factorial of 5: 720

 

 

Infinite Wile loop in C ++

A while loop that never ends is called an infinite while loop. Here, the provided condition is always true(Never returns false). According to the situation, the loop executes continuously with no ending point.

Program 1

#include <iostream>

using namespace std;

int main()
{
   int i=1;
    while(i>=1){//condition never returns false
            i++;
    cout <<"The value "<<i+1 <<":"<<i<< endl;
    }
    return 0;
}

 

When the above code is executed, it produces the following results:

The value 1 :1
The value 2 :2
The value 3 :3
The value 4 :4
The value 5 :5
The value 6 :6
The value 7 :7
The value 8 :8

.........
.........

Never end the loop

 

Displaying the elements of the array using while loop in C++language

An array is a data structure used to store the sequence of elements of the same data type.

We can display elements of single dimension array using while loop in C++ programming.

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
   int arr[]={56,78,76,43,59,83,24,67};
   int i=0;
    while(i<=7){
    cout <<arr[i]<< endl;
            i++;

    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

56
78
76
43
59
83
24
67

 

 

Suggested for you

Nested while loop in Java

Nested while loop in C++

Nested while loop in C

Nested while loop in Python

C++ wile keyword

C while keyword

Python while keyword

While loop in Java programming language
Do while loop in C programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago