C language – Looping Statements in C Programming

Loops in c language

The C language provides varied control structures that allow more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times.

The loop in a program consists of two parts, one is the body of the loop and another one is the control statement. The control statement is used to test the condition and then directs the repeated execution of the statements in the body of the loop.

Any looping statement would include the following steps:

⊕ Initialization of a condition variable.

⊕   Test the control statements.

⊕  Expecting the body of the loop depending on the condition.

⊕  Updating the condition variable.

The following are the loop structures available in the C programming language.

  • while…
  • do…while
  • for…
Loops in c programming language

The while loop in C language

It is a repetitive control structure, used to execute the statements with the body until the condition becomes false.

The while loop is an entry-controlled loop statement, which means the condition is evaluated first and if it is true, then the body of the loop is executed.

After executing the body of the loop, the condition is once again evaluated and if it is true, the body is executed once again, the process of repeated execution of the body of the loop continues until the condition finally becomes false and the control is transferred out of the loop.

Syntax:
    while(condition)
    {
    ...
    body of the loop;
    ...
    }


Flow Diagram

While loop in c language

The body of the loop may have one or more statements, the blocking with the braces is needed only if the body contains two or more statements.

Example program

 Addition of numbers up to 10 by using the while loop.

Output:

The sum fo numbers upto 10 is 55

Explanation: Here, the body of the loop executes until the condition becomes false. When the condition is false the printf() displays the result.

 

The do..while loop

In C language while loop makes a test of condition before the loop is executed. Therefore, the body of the loop may not be executed at all, if the condition is not satisfied at the first attempt.

In some situations, it may be necessary to execute the body of the loop, before the test condition is performed. In such a situation the do..while loop is useful.

It is also a repetitive control structure and executes the body of the loop once irrespective of the condition, then it checks the condition and continues the execution until the condition becomes false.

Syntax:
 do
 {
    .....
    body of the loop;
    .....
 } while(condition);

Flow Diagram

do while loop in c langauge

Here, the statements within the body of the loop are executed once, then it evaluates the condition, if it is true, then it executes the body until the condition becomes false.

Example program

Program to print n numbers using do..while loop.

Output:

Enter the number : 6
the number are     0
the number are     1
the number are     2
the number are     3
the number are     4
the number are     5

Explanation: This program prints n number on the screen, where n value can be supplied by the user.

The for loop in C language

The for loop is another repetitive structure and it is used to execute a set of instructions repeatedly until the condition becomes false.

The assignment, incrementation, or decrementation, and condition checking are done in for statement only, whereas other control structures are not offered all these features in one statement.

Syntax:
for(initilise counter;increment/decrement counter)
{
   ...
   body of the loop;
   ...
}

Flow Diagram

For loop on c language

For loop has three parts

  •  Initialize counter is used to initialize a counter variable.
  • The test condition is used to test the condition.
  • Increment/decrement counter is used to increment or decrement counter variable.

If there is a single statement within the for loop, the blocking with braces is not necessary, if more than one statement includes in the body of the loop, the statements within the body must be blocked with braces.

Example program

Addition of numbers up to 10 by using the for a loop.

Output:

The addition of numbers upto 10 is 55

Explanation: In the program, the variables i and sum are declared as integer-type variables.

The body of the for loop is executed 10 times, each time the sum is calculated as sum=sum+i. Finally the prints the value of sum as output.

Other statements related to loops

The break statement in the c language

The break statement is used to terminate the loop. When the keyword break is used inside any ‘C’ loop, control is automatically transferred to the first statement after the loop. A break is usually associated with an if statement.

When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

Syntax:
break;
break statement in c programming language
break statement in c programming language

Example program

Program to print the number up to 5 using a break statement

Output:

1 2 3 4 5

Explanation: In this program, the printf statement prints the value of ‘i’ up to 5 when ‘i’ reaches 6the if  statement becomes true. So the break statement transfers the control to the outside of the for loop.

The continue statement in the c language

In C programming language, some situations want to take control to the beginning of the loop, bypassing the statements inside the loop which have not yet been executed, for this purpose the continue is used.

When the statement continues is encountered inside any C loop control automatically passes to the beginning of the loop.

Syntax:
        continue;

Like break statement, the continue statements are also associated with if statement.

Example program

Calculate the sum of the given positive numbers.

 

Output:

Enter any number....10
Enter any number....5
Enter any number....15
Enter any number....25
Enter any number....-10
Enter any number....50
Sum is....105

Explanation:

Here, the for loops reads 5 numbers, for each number it checks whether it is less than zero, if it is less than zero it can not add and asks for the next number(the control will automatically transfer to the for loop using the continue) if it is greater than zero, it adds to the variable sum, finally after completing the loop it displays the result.

continue in loop c language
continue statement in c language

The goto statement in c language 

A number of ways of controlling the flow of execution based on the conditions. C language provides the goto statement to transfer control unconditionally from one place to another place in the program.

A goto statement can cause program control almost anymore in the program unconditionally.

The goto statement requires a label to identify the place to move the execution. A label is a valid variable name and must be ended with a colon (:).

goto statement in c programming

Example:

Output:

 value of a: 10
 value of a: 11
 value of a: 12 
 value of a: 13
 value of a: 14
 value of a: 16
 value of a: 17
 value of a: 18
 value of a: 19

FAQ's