loop statement

C Programming Loops

Loops cause program to execute the certain block of code repeatedly until test condition is false. Loops are used in performing repetitive task in programming.

A loop consist of  two different segments, one is the body of the loop and other is known as control statement. The control statement tests conditions and then directs the repeated execution of the statement contained in the body of the loop.

Depending on the position of the control statement in the loop, a control structure may be classified as entry controlled loop or exit controlled loop.

Entry-controlled loop:

  • In the entry controlled loop, the controlled conditions are tested before the start of the loop execution.
  • If the condition is not satisfied, then the body of the loop is not executed.
  • The entry controlled loop is known as pre-test loop.

Exit-controlled loop:

  • In the exit controlled loop, the controlled conditions are tested at the end of the body of the loop.
  • As the test is performed at the end, therefore the body of the loop is executed unconditionally for the first time.
  • The exit controlled loop is known as post-test loop.

There are 3 types of loops in C programming:

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

The for loop:

  • for loop is an entry-controlled loop

Syntax:

 

Working of for loop:

  1. The initialization statement is executed first, and only once. This step allows you to declare and initialize any loop control variables.
  2. Next, the expression/condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
  3. After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
  4. The expression/condition is now evaluated again. If it is true, the loop executes and the process repeats itself . After the condition becomes false, the for loop terminates.

 

Example:

Program to print the all the digits in ascending order. Program to print the all the digits in descending order.

 

Program to print the sum of integers in a given range Program to print the sum of squre of numbers in a given range

Additional Features of for loop:

  • More than one variable can be initialized in the for loop statement

Example: for(i=0,j=1; i<j; ++j)

  • Like initialization there may be more than one update section in the for loop

Example: for(i=0,j=1; i<j; ++j, i–)

  • The test condition may contains compound relation

Example: for(i=0,j=1; i<j && j<20 ; ++j)

  • Some of the section may remain blank, but semicolon should be there to separate the sections

Example:

 

  • If the test condition is not present the loop becomes an infinite loop. Such loop can be broken using break or goto statement.

Nested for loop:

When a for loop present inside another for loop, that is known as nested for loop.

Demo Program for Nested for loop:

Sample Output
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

 

The while loop:

Syntax:

Working of for loop:

  1. First the Test condition is evaluated if it is true(non-zero), then the body of the loop is executed. After that again the condition is tested if it is true the body is executed again this process continues till the condition is true.
  2. If the condition is false or expression value is zero then the control goes outside the loop.
  3. The body of the loop may consist of one or more statements
  4. If braces are not given while loop takes just next statement as the part of the loop
  5. As the condition is tested at the beginning of the loop, it is known as entry controlled loop

Example:

Program to print the all the digits in ascending order. Program to print the all the digits in descending order.
  • Nested while loop can be used same way as the nested for loop

 The do-while loop:

Syntax:

Working of do-while loop:

  1. After encountering the do statement, the program proceeds to evaluate the body of the loop first
  2. At the end of the loop test condition is tested. If the condition is true the program continues to execute the body of the loop once again. This process continues as long as the condition is true.
  3. When the condition becomes false the control will come outside the for loop.
  4. As the condition is tested at the end of the loop it is known as exit controlled loop.
  5. In do-while loop body of the loop is executed at least once.

Example:

Program to print the all the digits in ascending order. Program to print the all the digits in descending order.
  • Nested do-while loop can be used same way as the nested for loop
  • One type of loop can be present inside any other type of loop

Difference between while and do-while:

While do-while
1. It is a entry control loop 1. It is a exit control loop
2. The while loop will check the condition first before executing the content. 2.The do while loop executes the content of the loop before checking the condition of the while
3.The code block may or may not executed depending on the condition. 3. The code block is executed at least once even if the condition is false
4.Syntax:

while(condition/expression){

code block

}

4. Syntax:

do{

code block

}while(condition/expression);