×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

C Loops Statements

By Sneha Dujaniya Last updated : November 25, 2023

C Loops

C Loops (or, C looping statements) are also known as C language control statements and they are used to repeat a part of the program (code statements) a specified number of times or until a specific condition is true.

Loops are required when you have to execute a set of statements multiple times.

Loops are a very common and basic but important concept of any programming language.

Types of C Loops

To handle various such requirements where a set of statements to be executed multiple times, C programming languages provides the following types loops:

  1. The for loop
  2. The while loop, and
  3. The do...while loop

These loops are explained in detail as under.

1. The for loop

The for loop is the most commonly used loop by the programmers and requires us to initialize three conditions in a single line at the start of the loop.

Syntax

Below is the syntax of the for loop -

for (initialize ; condition ; increment)
{
    //body of the loop
    //Code to be repeated till the test condition;
}

Flow chart

Below is the flow chart of the for loop -

C | for loop flow chart

C for Loop: Example 1

Input two integers and find their average using for loop.

#include <stdio.h>

int main()
{
	int  a, b, avg, count ;
	
	for(count = 1; count<=3; count++)
	{
		printf("Enter values of a and b: ");
		scanf("%d %d",&a,&b);
		avg=(a+b)/2;
		printf("Average = %d" , avg);
	}
	
	return 0;
}

Output

    Enter the value of a and b: 2 4
    Average = 3
    Enter the value of a and b: 2 3
    Average = 2 
    Enter the value of a and b: 2 6
    Average = 4

Explanation:

  • When the loop begins for the first time, the value of count is set to be 1.
  • Now, the condition count<=3 is tested. If the condition is true, the control enters into the loop and the body is executed once.
  • After reaching the closing brace of the loop, the control is sent back to the for statement where the value of count is incremented by 1.
  • After this, the condition count<=3 is again checked to see if the value of count is still within the range 1 to 3.
  • If the condition stands true, the loop is executed again. This happens till the value of count becomes 3.
  • When the value of count becomes 4, the loop terminates and the control is transferred to the statement after for (if any).

Now, it is not necessary that we always write the initial, test condition and increment in the same as stated above. There are many other ways to do so. Let us see some of them:

C for Loop: Example 2

Print integers from 1 to 5 using for loop.

#include <stdio.h>

int main()
{
	int i; //loop counter
	
	//method 1
	printf("Method 1...\n");
	for(i=1; i<=5; i++)
		printf("%d ",i);
	printf("\n");
	
	//method 2 (increment is in printf statement)
	printf("Method 2...\n");
	for(i=1; i<=5; )
		printf("%d ",i++);
	printf("\n");
	
	//method 3 (printf statement with ++ in third section)
	printf("Method 3...\n");
	for(i=1; i<=5; printf("%d ",i++));
	printf("\n");
	
	//method 4 
	//initialization of loop before for statement
	//increment is with the printf statement
	printf("Method 4...\n");
	i=1;
	for( ; i<=5; )
		printf("%d ",i++);
	
	return 0;
}

Output

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

2. The while loop

The while loop is mostly suitable for conditions where we have to perform a task a fixed number of times. For example, writing the data of 10 students of a class.

Syntax

Below is the syntax of the while loop -

while (test_condition)
{
    //body
    //Code to be repeated till the test condition;
    //other statement like ++/--
}

Flow chart

Below is the flow chart of the while loop -

C | while loop flow chart

C while Loop: Example 1

Input two integers and find their average using while loop.

#include <stdio.h>

int main()
{
	int  a, b, avg, count ;
	
	count =1;
	
	while( count<=3 )
	{
		printf("Enter values of a and b: ");
		scanf("%d %d",&a,&b);
		avg=(a+b)/2;
		printf("Average = %d" , avg);
	}
	
	return 0;
}

Output

    Enter the value of a and b: 2 4
    Average = 3
    Enter the value of a and b: 2 3
    Average = 2 
    Enter the value of a and b: 2 6
    Average = 4

Explanation:

  • Here, as long as the condition within the parenthesis stands true, all the statements inside the loop are executed.
  • Which means that the while loop will be executed thrice.
  • To be noted, if we do not write the condition in which we increment the value of count, the loop will become an infinite loop. This happens because the condition count<=3 stands true forever.
  • Another important point is, there should be no semicolon after the while statement otherwise the loop will become indefinite.

Now, just as we saw in the for loop, there can be various ways in which we can achieve this result.

C while Loop: Example 2

Print integers from 1 to 5 using the while loop.

#include <stdio.h>

int main()
{
	int i; //loop counter
	
	//method 1
	printf("Method 1...\n");
	i=1;
	while(i<=5)
	{
		printf("%d ",i);
		i++;
	}
	printf("\n");
	
	//method 2 (increment is in printf statement)
	printf("Method 2...\n");
	i=1;
	while(i<=5)
	{
		printf("%d ",i++);
	}
	printf("\n");
	
	
	return 0;
}

Output

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

3. The do...while loop

The do...while loop is almost the same as while loop except for one difference.

As an important point, the do...while loop is always executed at least once, unlike the other loops. This happens because the loop runs for the first time and then at the end, the condition is checked to be true or not. If it stands true, the loop runs again and if false, the control comes out.

Syntax

Below is the syntax of the do...while loop -

do
{
    //body
    //Code to be repeated till the test condition;
    //other statement like ++/--
}while(test_condition);

Flow chart

Below is the flow chart of the do...while loop -

C | do while loop flow chart

C do...while Loop: Example 1

Input two integers and find their average using do while loop.

#include <stdio.h>

int main()
{
	int  a, b, avg, count ;
	
	count =1;
	
	do
	{
		printf("Enter values of a and b: ");
		scanf("%d %d",&a,&b);
		avg=(a+b)/2;
		printf("Average = %d" , avg);
	}while( count<=3 );
	
	return 0;
}

Output

    Enter the value of a and b: 2 4
    Average = 3
    Enter the value of a and b: 2 3
    Average = 2 
    Enter the value of a and b: 2 6
    Average = 4

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.