While Loop in C++

While Loop

On this page  we will discuss the While loop in C++ . In this type of loop, the test condition is tested before entering the loop body. Such types of loops are called Entry Controlled Loops. Concept of while loop is important to understand the basics of C++ programming.
While loop in C++

While Loop Syntax

while(condition(s))
{
    // execute statement(s)

}
  • Certain condition(s) are checked before the loop
  • If the condition(s) is(are) true, code inside is executed
  • The condition then is evaluated again. If true then the code inside is executed again.
  • This will keep happening until the condition doesn’t become false.
While Loop in C++
Program 1: (Print first 10 Natural Numbers)
Run
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1; // initialization
    
    // condition
    while(i <= 10)
    {
        cout << i << " ";
        i++; // increment
    }
    
    return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Program 2: (Sum of first N Natural Numbers)
Run
#include <iostream>
using namespace std;
 
int main()
{
    int num = 20, sum = 0;
    
    int i = 1;
    while(i <= num)
    {
        sum = sum + i;
        i++;
    }
    
    cout << "Sum of first " << num << " Natural numbers: " << sum;
    
    return 0;
}
Output:
Sum of first 20 Natural numbers: 210

Infinite while loop

A while loop can run infinitely if the conditions are not checked properly. Following are examples of while loops running infinitely –

  • Explicitly writing true in condition
  • Setting conditions that they will always be true
  • Forgetting to increment the iterator of the loop
Syntax for Infinite while loop
// condition will always be true infinitely
while(true)
{
    // do some work here
}
Example 1
#include <iostream>
int i = 100;
    
// condition will always be true infinitely
while(i > 10)
{
    cout << i << " ";
    i++;
}
// will keep on printing numbers from 100 to infinity

Inappropriate use of the semicolon

  • A semicolon after the condition part will stop the next statements not to  execute
  • Hence all the statements after the semicolon are not executed and just dead statements

Run

#include

using namespace std;

int main ()
{

  int i = 1;

  while (i <= 10);		//semicolon is stopping not to go down

  {

    court << i << "\t";		//loop body is never executed

    i++;

  }

  cout << "This will never print";

}

If you execute this code you will get an empty screen as output because  after semicolon the increment  statements and two cout statements are not allowed to execute i.e after semicolon whatever it may be nothing is executed in case of loops.

 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription