C++ While loop with Examples - C++ Infinite While loop

 

C++ While loop with Examples - C++ Infinite While loop

The while loop:

  • A while statement is a general repetition statement that can be used in a variety of programming situations. It has this general form
  • while is reserved word
  • In a while statement, the statement following the expression is executed repeatedly as long as the expression evaluates to a non-zero value/true.
  • Naturally, this means that somewhere in the while statement must be a statement altering the tested expression’s value
  • For now, however, considering just the expression and the statement following the parentheses, the computer uses this process in evaluating a while statement
  • The while statement literally loops back on itself to recheck the expression until it evaluates to zero (becomes false).
  • First of all condition is evaluated.
  • If it is true the control enters the body of the loop and executes all the statements in the body
  • The condition is evaluated again
  • This process continues until the condition is false 

Flowchart of While loop:

Flowchart of While loop
  • Here, key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
  • While loop terminates only when the condition becomes false.
  • The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false.
  • This way we can end the execution of while loop otherwise the loop would execute indefinitely. 

Example:

  • Display counting from 1 - 10 using while loop

#include<iostream>
using namespace std;
int main(){
    int n=1;
    while(n<=10){
        cout<<n<<" ";
        n++;
    }
    return 0;
}

Algorithm of While loop:

1) n is assigned a value 
2) The while condition is checked (n<=10). At this point there are two possibilities: 
        a) condition is true: the statement is executed (to step 3)
        b) condition is false: ignore statement and continue after It (to step 5) 
3) Execute statement: 
        cout << n ; 
        n++;
        (prints the value of n and increased n by 1) 
4) End of block. Return automatically to step 2. 
5) Continue the program right after the block

Example 2:

  • Ask the user to enter a number(n). Display the numbers from 1 to n and their sum using while loop.

#include<iostream>
using namespace std;
int main(){
    int i=1 , n, sum=0;
    cout<<"Enter a number:";
    cin>>n;
    while(i<=n){
        sum=sum+i;
        cout<<i<<" ";
        i++;
    }
    cout<<"\nThe sum is :"<<sum;
    return 0;
}

Infinite While loop:

  • Infinite While loop 
  • A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely. 
  • An example of infinite while loop: 
  • Infinite while Loop Example:

#include<iostream>
using namespace std;
int main(){
    int i=1;
    while(i<5){
        cout<<"The value of i is:"<<i<<endl;
        i--; 
    }

Output:


output of Infinite While loop
continue.....
  • This loop would never end as I’m decrementing the value of i which is 1 so the condition i<5 would never return false.

Task:

1)Ask the user to enter a starting and ending number. Display all the even numbers in the given range using while loop.
2) Ask the user to enter five random numbers and display the total(sum of the numbers) and the average of these numbers using while loop 




Post a Comment

0 Comments