C Programming

Learning Loops in C

Loops are the basic concept of C language. Loops mean iterations. When we want to execute some statements multiple times or repeatedly in our program, in that case, we use a looping statement. Loops actually help the coder with do not repeat the same line in our program. It reduces the lines of code in the program and decreases the program’s execution time.

There are three types of loops present in the C language. They are:

  1. While
  2. Do-while
  3. For

We will discuss each loop below.

1. While Loop

Programming Example 1

#include<stdio.h>

int main()
{
    int i = 1 ;     //Initialization
    while ( i<= 5 )     //condition
    {
printf( " Avishek " ) ;
i++ ;        //flow
    }
    return 0 ;
}

Output

Explanation

Here i is a control variable to control the loop. The condition part will be treated as either true or false. If it is true, then it enters inside the loop. In the flow part, we can increment or decrement the control variable’s value. Here, the condition will be checked 1 time more than printing the value because when the condition gets false, another time condition will be checked.

While loop is also called an entry-controlled loop because here, we can either inside the loop’s body inside the block when the condition is true.

Programming Example 2

#include<stdio.h>

int main()
{
    int i = 0 , n ;
printf( " Enter a number " ) ;
scanf("%d",&n);
    while ( i<= n )
    {
printf( " %d ", i );
i++ ;
    }
    return 0 ;
}

Output

Explanation

In this program, we want to print some values from the user some range with the help of a while loop. For this, we use the scanf() function to take input from the user. Then we simply run a while loop to execute the program.

Programming Example 3

#include<stdio.h>

int main()
{
    int i = 0 , n , sum = 0 ;
printf( " Enter a number " ) ;
scanf("%d",&n);
    while ( i<= n )
    {
        sum = sum + i ;
i++ ;
    }
printf( " sum of 1 to %d is %d ", n , sum );
    return 0 ;
}

Output

Explanation

The above-mentioned program is also an example of the while loop. Here we want to sum up some numbers that are given by the user. As we are doing the same action, we just run the while loop. It just sums the values repeatedly by executing the same statement multiple times.

Do While Loop

Programming Example 4

#include<stdio.h>

int main ()
{
    int i = 1 ; //Initialisation
    do
    {
printf( " Avishek " ) ;
i++ ; //flow
    } while ( i<= 5 ) ; //condition
    return 0 ;
}

Output

Explanation

Do while loop is also called the exit control loop. Because in this loop, we can enter at least one time without checking the condition. After performing one operation & increment the value of the control variable ā€œiā€, the condition is in check. Here, the condition will be checked as same as the no of operation is performed, like printing the name Avishek.

Programming Example 5

#include<stdio.h>

int main()
{
    int i = 0 , n ;
printf( " Enter a number " ) ;
scanf("%d",&n);
    do
    {
printf( " %d ", i );
i++ ;
}while ( i<= n ) ;
    return 0 ;
}

Output

Explanation

In this program, we want to print some values from the user some range with the help of the Do-while loop. For this, we use the scanf() function to take input from the user. Then we simply run a Do-while loop to execute the program

Programming Example 6

#include<stdio.h>

int main()
{
    int i = 0 , n , sum = 0 ;
printf( " Enter a number " ) ;
scanf("%d",&n);
   do
    {
        sum = sum + i ;
i++ ;
}  while ( i<= n ) ;
printf( " sum of 1 to %d is %d ", n , sum );
    return 0 ;
}

Output

Explanation

The above-mentioned program is also an example of a Do-while loop. Here we want to sum up some numbers that are given by the user. As we are doing the same action, we just run the Do-while loop. It just sums the values repeatedly by executing the same statement multiple times

For Loop

Programming Example 7

#include<stdio.h>

int main()
{
    int i ;
    for ( i = 1 ; i<= 5 ; i++ )
    {
printf( " Avishek " ) ;
    }
    return 0 ;
}

Output

Explanation

In the for loop parenthesis, we are writing three steps that is Initialisation, condition & flow (increment or decrement). It is also called entry controlled loop. At first, it checked the condition. If the condition is true, then it allows for entry into the loop.

Programming Example 8

#include<stdio.h>

int main()
{
    int i = 0 , n ;
printf( " Enter a number " ) ;
scanf("%d",&n);
    for ( i = 0 ; i<= n ; i++)
    {
printf( " %d ", i );
    }
    return 0 ;
}

Output

Explanation

In this program, we want to print some values from the user some range with the help of for loop. For this, we use the scanf () function to take input from the user. Then we simply run a for loop to execute the program

Programming Example 9

#include<stdio.h>

int main()
{
    int i = 0 , n , sum = 0 ;
printf( " Enter a number " ) ;
scanf("%d",&n);
    for ( i = 0 ; i<= n ; i++)
    {
        sum = sum + i ;
    }
printf( " sum of 1 to %d is %d ", n , sum );
    return 0 ;
}

Output

Explanation

The above-mentioned program is also an example of for loop. Here we want to sum up some numbers that are given by the user. As we are doing the same action, we just run for a loop. It just sums the values repeatedly by executing the same statement multiple times.

Conclusion

We learn from the above concept that loops are an important concept of C language as it plays an important role in executing the same statement multiple times. Once we write the code of the loop one time, it executes multiple times according to the requirement of the program.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com