C – For, While, Do While and Infinite Loop

For, While, Do While and Infinite Loop

For Loop

The for loop is one of the powerful loop and flexible loop which provides a more concise loop control structure.

It is a pre-test or entry control loop similar to while loop. A program can also use a while loop instead of for loop.

If loops are nested then the for loop is used.

for(expression1; expression2; expression3)
statement;

Generally

for(initialization; test_expression; increment)
statement;
next_statement;

Expression1  is evaluated, which is used to initialize the loop. Then the expression2 is evaluated which is simply known as the test_expression.

If it is true or nonzero, then the loop body is executed, and then the expression3 will evaluate and end.

If it is false then the loop will be terminated and next_statement will evaluate.

C Code

#include<stdio.h>
#include<conio.h>
void main(){
       int i;
       for(i=1; i<=40; i++)
              printf("%dt",i);
        getch();
}

While Loop

The while loop is an entry control loop or it is also known as pretest loop . It repeats a statement or a block of statements while its controlling expression is found to be true. The general format is;

while (expression)
          statement;

the statement which is repeated is called the body of the loop. The statement can be single statement or compound statement.

In the while loop at first expression is evaluated. If it is true or nonzero, the statement following while is executed and control is passed back to the beginning of the while loop.

The body of the while loop is executed repeatedly until the expression is zero or false. After the termination of the loop control passes to the next statement.

C Code

#include<stdio.h>
#include<conio.h>
void main(){
    int i;
i=1;
    while (i<= 10){
           printf("t %d ", i);
           i++;
    }
    getch();
}

i is the loop control variable which is initialized as i=1;. The condition is i<=10 is evaluated and if found to true the statement printf(“t %d “, i); is executed and the value of i is incremented by 1.

If the condition is false then control passes to the next statement. If the condition is applied false then the statement never executed.

Do While Loop

The do-while-loop is an exit control loop or it is also known as post test unlike while loop and for loop .

The loop body is executed at least once even though the condition is false because test expression is at the bottom of the loop.

Do{
statement1;
}while (test_expression);  

Generally

loop initialization;
do    {     
statement1;   
} while (test_expression);   
next_statement; The statement1 is executed before test_expression is evaluated.

If the value of test_expression is found to be true or non zero, then control passes back to the beginning of the do statement and the process is repeated.

When an expression is false or zero, then control is passed to the statement next statement

C Code

#include<stdio.h>
#include<conio.h>
void main(){
        int i;
        i=1;
        do{
            printf(“t %d”, i);
            i++;
        }while(i<40);
   getch();
}

Infinite Loop

A loop is said to be infinite if it never terminates. While specifying the condition, sometime it happens that the condition (test_expression) will never becomes zero and we do not have other way to escaping the loop. In this case the program is stuck in the loop infinitely.

C Code

#include<stdio.h>
#include<conio.h>
void main(){
     int n;
     printf("Number");
     scanf('%d",&n);
     while(n!=0)
{
     printf("%d t",n);
     n--;
getch();
}

C Code

#include<stdio.h>
#include<conio.h>
void main(){
int i;
       for(i=1; i>=0; i++)
       printf("I Am Infinite");
getch();
}