Printing numbers using While Loop in C++


The while loop is repeats a statement or block while its controlling expression is true.The condition can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. When condition becomes false, control passes to the next line of code immediately following the loop.

  • If the condition to true, the code inside the while loop is executed.
  • The condition is evaluated again.
  • This process continues until the condition is false.
  • When the condition to false, the loop stops.

This is a C++ program that prompts the user to enter a starting value and an ending value, and then prints all the integers from the starting value up to and including the ending value using a while loop. Here's how the program works:

  • It declares two integer variables s and e to store the starting and ending values, respectively.
  • It prompts the user to enter the starting value by printing the message "Enter the starting value:" to the console using the cout object, and then reads the input from the user into the variable s using the cin object.
  • It prompts the user to enter the ending value by printing the message "Enter the ending value:" to the console using the cout object, and then reads the input from the user into the variable e using the cin object.
  • It uses a while loop to print all the integers from the starting value up to and including the ending value. The loop condition s<=e ensures that the loop continues as long as s is less than or equal to e.
  • Inside the loop, the current value of s is printed to the console using the cout object, followed by a newline character (\n) to move to the next line.
  • The value of s is then incremented by 1 using the postfix increment operator s++.
  • The loop continues until the condition s<=e is no longer true.
  • The program then exits by returning 0 to the operating system using the return statement

Source Code

/*

    while(condition)
    {
        Statement;
    }

*/
#include<iostream>
using namespace std;
int main()
{
    int s,e;
    cout<<"\nEnter the starting value:";
    cin>>s;
    cout<<"\nEnter the ending value:";
    cin>>e;
    while(s<=e)
    {
        cout<<"\n"<<s;
        s++;
    }
    return 0;
}

To download raw file Click Here

Output

Enter the starting value:1
Enter the ending value:10
1
2
3
4567
8
9
10

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples