C Programming Tutorial
About Lesson

C Loops

In this tutorial, we will learn about the C loops, for loop, nested for loop and infinite for loop and its working with the help of some examples.


C Loops

In computer programming, loops are used to repeat a block of code. Loops in Programming come into use when we need to repeatedly a block of statements.
For example, let’s say we want to show a message 100 times. Then instead of writing the print statement 100 times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in our programs by making effective use of loops.

In programming, a loop is a sequence of instructions that is repeated until a certain condition is reached.


Types of Loops

There are 3 types of loops in C++:
1) for loop
2) while loop
3) do...while loop

This tutorial focuses on C for loop. We will learn about the other type of loops in the upcoming tutorials.


For loop

  • For loop is a Entry Controlled Loop or we can say repetition control structure.
  • It allows us to write a loop that is executed a specific number of times.
  • The loop enables us to perform ‘n’ number of steps together in one line.
  • For loop is used to execute a set of statement repeatedly until a particular condition is satisfied.

The syntax of for-loop is:

for (initialization; condition; update) {
    // body of-loop 
}

 

How for loop works?

  • First step : Initialization part of for loop happens first and only once.
  • Second step : Then, the condition in for loop is evaluated on each loop iteration.
    • If the condition is true,
      Then the statements inside for loop get executed again.
    • If the condition is false,
      Then the statements in for loop does not execute and the for loop terminates.
  • Third step :After every execution of for loop, the increment/decrement
  • Fourth step : Again the test expression is evaluated.

To learn more about operators used in above example, click Relational and Logical operators.


Flowchart of For Loop

C for loop flowchart
Flowchart of for loop in C

Example 1: for loop

// Print numbers from 1 to 20
#include <stdio.h>
int main() {
  int a;
  for (a = 1; a <= 20; ++a)
  {
    printf("%d ", a);
  }
  return 0;
}

Output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

 

Working of for loop in above example :

  • First step : Variable a is initialized to 1.
  • Second step : Then the condition a<=20 is being checked, the condition is evaluated to true because a = 1 and the statements inside the for loops are executed.
  • Third step : Then the increment/decrement statement will execute and the flow will be returned to the conditional part where the updated value of a will be checked i.e. a = 2 .
  • Fourth step : Third step is repeat until the given condition is not evaluated to false which is a = 21 and for loop is terminated.

Example 2: for loop

// Program to calculate the sum the numbers from 1 to 100
#include <stdio.h>
int main(){
  int a, sum = 0;
  for(a = 1; a <= 100 ; a++)
  {
    sum += a;
  }
  printf("Sum of integers from 1 to 100 is %d", sum);
  return 0;
}

Output

Sum of integers from 1 to 100 is 5050 

 

Working of for loop in above example :

  • First step : Variable sum is initialized to 0 and Variable a is initialized to 1.
  • Second step : Then the condition a<=100 is being checked, the condition is evaluated to true because a = 1 and the statements inside the for loops are executed.
  • Third step : Then the increment/decrement statement will execute and the flow will be returned to the conditional part where the updated value of a will be checked i.e. a = 2 .
  • Fourth step : Third step is repeat until the given condition is not evaluated to false which is a = 101 and for loop is terminated.

Nested For Loop

A for loop within another for loop is called Nested For loop.

Syntax :

for (initialization; condition; increment/decrement) 
{
  for (initialization; condition; increment/decrement)
  {
    // body of inner for-loop
  }
  // body of outer for-loop
}

Example:

// C++ program to display a triangular pattern with 5 rows
#include <stdio.h>

int main() {
  int rows = 5;
  for (int i = 1; i <= rows; ++i)
  {
    for (int j = 1; j <= i; ++j)
    {
      printf("* ", sum);
    }
    printf("\n", sum);
  }
  return 0;
}

Output

*
* *
* * *
* * * *
* * * * *

In this program, the outer loop iterates from 1 to rows.

The inner loop iterates from 1 to i. Inside the inner loop, we print the character '*'.

Note : There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level.

If you don’t understand above example, We recommended you to know little more about
Nested Loop.


Infinite for loop

If the condition in a for loop is always true, it runs forever (until memory is full).

Syntax :

for (initialization; condition; increment/decrement) 
{  // block of code
}

In the above program, the condition is always true which will then run the code for infinite times.


Loop Control Statements

These control statements change the execution of the loop from its normal execution. The loop control structures are –
1. break statement – It is used to end the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
2. continue statement – It skip some statements according to the given condition.
3. goto statement – It transfer control to the labeled statement.

error: Content is protected !!