Monday, August 26, 2019

Looping Statements in C


Looping statements:
Types of Loops
Depending upon the position of a control statement in a program, a loop is classified into two types:
1. Entry controlled loop
2. Exit controlled loop

In an entry controlled loop, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.

The control conditions must be well defined and specified otherwise the loop will execute an infinite number of times. The loop that does not stop executing and processes the statements number of times is called as an infinite loop. An infinite loop is also called as an "Endless loop."
Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.
The specified condition determines whether to execute the loop body or not.

The while statement
It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.
After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body.
In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. 

Syntax:
while (condition)
{
   statements;
}

Example:
#include<stdio.h>
#include<conio.h>
main()
{
        int num=1;       //initializing the variable
        clrscr();
        while(num<=10)   //while loop with condition
        {
                 printf("%d\n",num);
                 num++;           //incrementing operation
        }
        getch();
}

The do … while statement
It is an exit-controlled loop. The do…while loop checks the condition at the end of the loop. This means that the statements inside the loop body will be executed at least once even if the condition is never true. The do…while loop is mainly used in the case where we need to execute the loop at least once. The do…while loop is mostly used in menu-driven programs where the termination condition depends upon the end user.

Syntax:
do
{
   statements;
} while (condition);

Example:
#include<stdio.h>
#include<conio.h>
main()
{
        int num=1;       //initializing the variable
        clrscr();
        do
        {
                 printf("%d\n",num);
                 num++;           //incrementing operation
        } while(num<=10);        //while loop with condition
        getch();
}

In the do…while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.

The for statement
A for loop is a more efficient loop structure in 'C' programming.  Like while loop it is an entry-controlled loop. The main advantage of using this loop is that, one can specify initialization, condition and increment / decrement operation on single statement.
The general syntax of for loop is

Syntax:
for (initialization; condition; incrementation or decrementation )
{
  statements;
}
Ø  The initialization of the for loop is performed only once.
Ø  The condition is a Boolean expression that tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned.
Ø  The incrementation/decrementation increases (or decreases) the counter by a set value.
Example 1:
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        clrscr();
        for(i=1;i<=10;i++)       //for loop to print 1-10 numbers
        {
                 printf("\n I = %d",i);           //to print the number
        }
        getch();
}

Example 2:
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        clrscr();
        for(i=10;i>=0;i--)       //for loop to print 10 to 0 numbers
        {
                 printf("\n I = %d",i);           //to print the number
        }
        getch();
}

Example 3:
#include<stdio.h>
#include<conio.h>
main()
{
        int i,no;
        clrscr();
        printf(“Enter a number for Multiplication Table ”);
        scanf(“%d”,&no);
        for(i=1;i<=10;i++)       //for loop to print 1-10 numbers
        {
                 printf("\n %d",i*no);            //to print the number
        }
        getch();
}

Example 4:
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        clrscr();
        for(i=1;i<=10;i++)      
        {
                 if(i%2==0)
                 {
                         printf("\n Even Number = %d",i);
                 }
                 else
                 {
                         printf("\n Odd Number = %d",i);
                 }
        }
        getch();
}


No comments:

Post a Comment