Loading...
C++

Loop Control Statement

Loop control statements in c are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false. Loops are used to repeat a block of code.
There are 3 types of loop control statements in C language. They are,
1. while
2. do-while
3. for

  1. While statement:
    The while statement continuously executes a block of statements until given condition is false.
    –> The while loop evaluates the test expression.
    –> If the test expression is true, codes inside the body of while loop is evaluated.
    –> Then, the test expression is evaluated again. This process goes on the test expression is false. 
    –> When the test expression is false, while loop is terminated.
    Syntax:
    while(expression)
    {
      statement(s)
    }
    Example:
    #include<iostream>
    using namespace std;
    void main()
    {
      int num, i=1, factorial=1;
      cout<<“Enter a positive integer:”;
      cin>>number;
      while (i<=number)
      {
        factorial=factorial*i’
        i++;
      }
      cout<<“Factorial of “<<number<<“=”<<factorial;
    }
  2. do…while Loop:
    The do…while loop is a variant of the while loop with one important difference. The body of do…while loop is executed at least once. Then, only the test expression is checked.
    –> The code inside while loop is executed at least once. Then the test expression is checked.
    –> If the test expression is true, then the body of loop is executed. This process continues until the test expression becomes false.
    –> When the test expression is false, do…while loop is terminated.
    Syntax:
    do
    {
      statements;
    }while(test_Expression);
    Example:
    #include<iostream>
    using namespace std;
    void main()
    {
      float sum=0.0, num;
      do
      {
        cout<<“Enter a number:”;
        cin>>num;
        sum+=num;     //sum=sum+num
      }while(num!=0.0)
       cout<<“Total sum=”<<sum
    }
  3. for Loop:
    A for loop is a repetition control structure that allows to write a loop that needs to execute a specific number of times.
    –> The initialization is executed once at the beginning.
    –> Then, the test expression is evaluated.
    –> If the test expression is false, for loop is terminated. But if the test expression is true, code inside the loop is executed and the expression is updated.
    –> Again, the test expression is evaluated and this process is repeated until the test expression is false.
    Syntax:
    for(initialization; condition; increment/decrement)
    {
      statements;
    }
    Example:
    #include<iostream>
    using namespace std;
    void main()
    {
      int i, n, fact=1;
      cout<<“Enter a positive integer:”
      cin>>n;
      for(i=1; i<=n; i++)
      {
        fact*=i;      //fact=fact*i
      }
      cout<<“Factorial of “<<n<<“=”<<fact;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *