Iterative Statements or Concept of looping

When we need to do a task more then one or two times we need not to write a code particular times .C language supports iterative statements those are simply called loops . 

Loops are used to repeat the execution of a statement/ set of statements , depending on the conditions .




C language supports three types of iterative or loops  those are as follows...

1. for loop
2. while loop
3. do while loop

Types of loop

 There are two types of looping concept that has been described 


1. Entry Controlled Loop / pre-test loops :- In these, test condition is checked before the execution of statement. 

Minimum number of repetition is 0.

Example of this is :- for loop and while loop.


2. Exit Control Loop / post-test loops :- In these, test condition is checked after the execution of the statements. minimum number of repetition is one. 

For example:- do while loop.


Description of loops are as follows-


For Loop

This loop is use to perform a particular task until unless the particular condition gets false .


SYNTAX OF FOR LOOP :- 


  1. for (initializationStatement; testExpression
  2. ; updateStatement)
  3. {
  4.       // statements inside the body of loop 
  5. }


Characteristics of FOR loop :- 

  1. When a for loop is used, the loop control variable is initialized only once.
  2. With every iteration of the loop, the value of the loop control variable is updated and the condition is checked. 
  3. If the condition is true, the statement block of the loop is executed else, the control jumps to the immediate statement outside the for loop. 
  4. Updating the loop control variable may include incrementing / decrementing the loop control variable or setting it to some other value like, i = i+2, where i is the loop control variable. 


Flowchart of for loop




 for loop Example

  1. // Print numbers from 1 to 10
  2. #include <stdio.h>
  3. #include <conio.h>

  4. int main() 
  5.  int i;                                                  // Initialize

  6.  for (i = 1; i < 11; ++i)
  7.  {
  8.    printf ("%d ", i);
  9.   }
  10. getch();
  11. }

Output 

  1.  
  2. 1  2   3   4   5   6   7   8   9   10


  1. i is initialized to 1.
  2. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for loop is executed. This will print the 1 (value of i) on the screen.
  3. The update statement ++i is executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body of for loop is executed. This will print 2 (value of i) on the screen.
  4. Again, the update statement ++i is executed and the test expression i < 11 is evaluated. This process goes on until i becomes 11.
  5. When i becomes 11, i < 11 will be false, and the for loop terminates.

While Loop 

while loop is also a entry-controlled loop like for loop. while  LOOP is different fro for loop in this following manner as it has  

1. Different syntax. 
2. For loop is preferred where we have fixed number of iterations.

 It is not always true that every problem will-be solved by using  for loop it depends on every programmers thinking which approach or while loop he/she uses.

hence here comes an important point :

Note: Every problem which can be implemented using for loop, we can use while loop also and vice-versa. 


SYNTAX OF WHILE LOOP :- 



  1. while (testExpression)
  2. {
  3.     // statements inside the body of the loop 
  4. }


Characteristics of while loop :- Following are the characteristics of while loop. 

  1. The while loop evaluates the test expression inside the parenthesis ().
  2. If the test expression is true still  statements inside the body of while loop are executed. Then, the test expression is evaluated again.
  3. The process goes on until the test expression is evaluated to false.
  4. if the test expression is false, the loop terminates (ends).
  5. While loop is used to repeat a task until a particular condition is false. 


Flowchart of while loop



 while loop Example

  1. // Print number from 1 to 5
  2. #include <stdio.h>
  3. #include <conio.h>

  4. int main() 
  5.  int i=1;                                 // Initialize

  6.  while (i <= 5)
  7.  {
  8.     printf ("%d\n ", i);         //  \n for new line
  9.       ++1;                                // Increment
  10.   }
  11. getch();
  12. }

Output

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5


Do While loop

This is third part of  the looping  and its  syntax and characteristics are follow:


SYNTEX OF  DO....WHILE  LOOP :- 


  1. do 
  2. {
  3.       // statements inside the body of the loop 
  4. }
  5.  while (testExpression);


Characteristics of do while loop :-  

  1. The do-while loop is a exit-controlled/ post-test loop in which the test condition is tested at the end (exit point) of the loop. 
  2. The body of the loop gets executed at least one time (even if the condition is false). 
  3. Entry in the loop is automatic there is only a choice to continue it further or not


Flowchart of do while loop



 do while Example

  1. // Print number from 1 to 10
  2. #include <stdio.h>
  3. #include <conio.h>

  4. int main() 
  5.  int i;                                 
  6.  // do while loop
  7. i=1                            // Initialize
  8. do
  9.   {
  10.      printf ("%d\n ", i);  
  11.       i++;                             // Increment
  12.   }
  13.  while (i<= 10)           // Condition
  14. getch();
  15. }

Output
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10