Loops

LOOPS: 
  • For loop
  • while loop
  • do while loop
To execute a set of instructions repeatedly until a particular condition is being satisfied.

For Loop:
       
       The for is usually appropriate for loops in which the initialization and increment are single  
       statements and logically related, since it is more compact than while and it keeps the loop 
       control statements together in one place. 

Syntax:
        
          for (expr1; expr2; expr3) 

Example program using for loop: 
       main() 
       { 
            int counter,a[10]; 
            for(counter=0;counter<10;counter++) 
            { 
                     printf(“Enter a[%d]=”,counter); 
                     scanf(“%d\n”,a[counter]); 
            } 
           printf(“Array elements:”); 
           for(counter=0;counter<10;counter++) 
                  printf(“%d”,a[counter]); 
       } 

Explanation:
        
         In above program array a length 10.It occupies 20 bytes in memory. 
         Counter is normal integer variable. It is used to read and display the array values. 
         First FOR LOOP is used to read the array values. 
         Second FOR LOOP is used to display the array values. In this second FOR LOOP using 
         one line. So, no need of brace brackets 

While Loop: 
The expression is evaluated. If it is non-zero, statement is executed and expression is reevaluated. 

This cycle continues until expression becomes zero, at which point execution resumes after statement.

Syntax:
        
          while (expression) 
              statement 
Example program using while loop: 
       main() 
       { 
              intcounter,a[10]; 
              counter=0; 
              while(counter<10) 
              { 
                      printf(“Enter a[%d]=”,counter); 
                      scanf(“%d\n”,a[counter]); 
                      counter+=1; 
              } 
             printf(“Array elements:”); 
             counter=0; 
             while(counter<10) 
             { 
                     printf(“%d”,a[counter]); 
                     counter+=1; 
            } 
       } 

Explanation:
 In above program array a length 10.It occupies 20 bytes in memory. 
Counter is normal integer variable. It is used to read and display the array values. 

Before using counter variable to initialize counter. Then use it in WHILE LOOP.
First WHILE LOOP is used to read the array values. 
Second WHILE LOOP is used to display the array values. 

Do While Loop: 
       In do while loop first execute the statements then it checks the condition. 

Syntax:
        do{ 
                 Statement1; 
                  ... 
                 Statement n; 
             }while(condition); 

Example program using for loop:
        main() 
        { 
                 int counter; 
                 counter=0; 
                 do 
                 { 
                        printf(“%d”,counter); 
                  }while(counter>0); 
         } 
 

Conclusion:
In FOR LOOP:          No need to initialize variable before the LOOP 
In WHILE LOOP: 
          To initialize the variable before the LOOP 
           Increment/decrement the variable within the LOOP 
In DO WHILE LOOP: 
           Once it execute If the condition is TRUE/FALSE. 
     

1.What is the difference between while loop and for loop in C language?

while loop - used for looping until a condition is satisfied and when it is unsure how many times the code should be in loop
for loop - used for looping until a condition is satisfied but it is used when you know how many times the code needs to be in loop

No comments:

Post a Comment