Thursday, 29 May 2014

Do-While Loop in C

While loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i.e., loops are used in performing repetitive work in programming.
Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop.
There are 3 types of while loops in C programming:
  1. while loop
  2. do...while loop
  3. infinite while loop.

1.WHILE LOOP:
               The ‘while’ loop allows execution of statements inside block of loop only if condition in loop succeeds.
Syntax:
            initialization
            while(condition)
            {
                   ..........
                   ..........
                   .......... 
                   condition change statement;
            }
In the pseudo code above :
  • Variable initialization is the initialization of counter of loop before start of ‘while’ loop
  • Condition is any logical condition that controls the number of times execution of loop statements
  • Condition Change statement is the increment/decrement of counter or which change the condition.


Ex:

#include<stdio.h>
#include<conio.h>
int main()
{
          int i;
          i=10;
          while(i)
          {
                  printf("Value of i :%d", i);
                  i++;
          }          return 0;
}

2. do...while loop:
It is another loop like ‘for’ loop in C. But do-while loop allows execution of statements inside block of loop for one time for sure even if condition in loop fails.

Syntax:
           initialization
            do
            {
                   Statement Block
                   ..........
                   .......... 
                   condition change statement;
            }while(condition)


Example:
#include
int main() 
{
   int j = -5;
   do
   {          
       printf("%d\n",j);           
       j = j + 1;     
   }while(j <= 0);        
   return 0;  
} 

Infinite While Loop:

 We can use infinite while loop in C to iterate loop for infinite times. We can Even use Infinite while loop for operation in which we cannot decide how many iteration does it take at compile time. There are many ways we declare infinite while loop:

Way 1 : Semicolon at the end of While
#include<stdio.h>

 void main()

{

        int num=300;

        while(num>255);  //Note it Carefully

        printf("Hello");

} 
Output:
It won't Print anything

Semicolon at the end of while indicated while without body. In the program variable num doesn’t get incremented , condition remains true forever. As Above program does not have Loop body , It won’t print anything.

Way 2 : Non-Zero Number as a Parameter

#include
void main()
{
        while(1)
        printf("Hello");
}
Output:
 Infinite Time "Hello" word 
Non Zero is specified in the While Loop means Loop will have always TRUE condition specified inside. As condition inside loop doesn’t  get changed, condition inside while remains true forever.

Way 3 : Subscript Variable Remains the same:
#include
void main()
{
      int num=20; 
      while(num>10)      
      {     
           printf("Hello");      
           printf(" C ");
      }
}
Output:
Infinite Time "Hello C" word

Way 4 : Character as a Parameter in While  Loop:

#include<stdio.h>
void main()
{
        while('A')
        printf("Hello");
}
Output:
Infinite Time "Hello" word
Explanation:
       Character is Represented in integer in the form of ASCII internally.Any Character is Converted into Non-zero Integer ASCII value Any Non-zero ASCII value is TRUE condition , that is why Loop executes forever

0 comments :

Post a Comment