Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

While loop in C++ programming language

Posted on July 19, 2016May 16, 2020

Table of Contents

  • While loop in C++ programming language
    • How while loop works
    • While loop programs

While loop in C++ programming language

In this article, we discuss the While in C++ programming language.

In the C++ programming language, the while loop used to executes the block of code repeatedly until the particular condition is satisfied.

 

Generally, loops are used to repeat a block of code

C++ language has three types of loops

  1. For loop
  2. while loop
  3. Do-while loop

In this post, we are going to learn the While in Cpp programming language. with example program

Syntax

While loop in C++ programming language
Syntax
Syntax in while loop in C++

How while loop works

  • First, the test expression evaluates the test expression only once
  • When the test expression is true. statements inside the body of while loop is executed.
  • this process happening on until the test expression is false
  • When the test expression is false, The control exits from the loop body and while-loop is terminated

 

Flow diagram

While loop in C++ programming language
flow diagram
Flow diagram of While loop in C++

Test Expression is checked on each and every entry of the while loop

 

While loop programs

Program 1

This program used to display natural numbers 1 to 10

#include <iostream>

using namespace std;

int main()
{
int counter=1; // initialized variable
while(counter<=10) //Test expresiion
{
cout << counter<<"-> number of student" << endl; //Display statement
counter++;  //Increment statement
}

return 0;
}

 

When the above code is executed it produces the following result

While loop in C++ programming language
Example

Programme 2

This program allows to enter a number and calculate the factorial of given number

#include <iostream>
using namespace std;

int main() {
int number, i = 1, factorial = 1;
cout<< "Enter a positive integer: ";
cin >> number;

while ( i <= number) {
factorial *= i;      //factorial = factorial * i;
++i;
}

cout<<"Factorial of "<<number<<" = "<<factorial;
return 0;
}

 

When the above code is executed it produces the following result

While loop in C++ programming language
Example

Program 3

The program allows the user to enter a number and calculates sum of 1 to given number

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

int main()
{
    int num,sum=0;

    cout << "Enter a positive integer" << endl;
    cin>>num;
    int i=1;
    while(i<=num){
        sum+=num;  //sum=sum+num;
        i++;
    }
    cout<<"The sum of 1 to "<<num<<" : "<<sum;
    getch();
    return 0;
}

When the above code is executed it produces the following result

Enter a positive integer
40
The sum of 1 to 40 : 1600

 

 

Related post

For Loop in C++     C++ Array

Do-while Loop C++      C++ Class and Object
For statements in Java
While statements in Java
Do-while Loop in Java
For statements in C
While statements In C
If condition C++

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes