Loops in C: For, While, Do While looping Statements with Examples

C programming language provides the following types of loops to handle looping requirements.

The first statement in a function is executed first, then the second, and so on. In general, statements are executed in order.

Different control structures offered by programming languages enable more intricate execution routes.

We can run a statement or set of statements repeatedly by using a loop statement.

What is Loop in C?

In C, looping statements repeatedly execute the set of statements until the condition is met.

The body of a loop and a control statement make up a loop in the C programming language.

The body of the loop is instructed to run until the provided condition becomes false by the control statement, which is a combination of a few conditions.

The C loop’s main function is to repeatedly run the same code.

Types of Loops in C

The two forms of looping statements in C are categorized according to the position of a control statement in a program:

  • Entrance-controlled loop
  • Exit the controlled loop.

In a C entry control loop, a condition is verified before the loop’s body is executed. Another name for it is a pre-checking loop.

A condition is checked following the execution of a loop’s body in an exit-controlled loop. Another name for it is a post-checking loop.

An infinite loop has the qualities listed below:

  1. There is no mention of a termination condition.
  2. The prerequisites are never satisfied.

Whether to run the body of the loop or not depends on the provided condition.

Three different loop constructs are available in the “C” programming language:

  • The while loop
  • The do-while loop
  • The for loop
Loop Type Description
While LoopRepeats a sentence or set of sentences as long as a certain condition is true. Prior to starting the body of the loop, the condition is tested.
For loopShortens the code that controls the loop variable and repeatedly executes a series of statements.
Do while loopWith the exception of testing the condition at the end of the loop body, it is more like to a while statement.
Nested LoopsAny other while, for, or do while loop may include one or more loops.

Loop Control Statements

Statements used to control loops divert execution from the usual path. All automatic objects produced within a scope are deleted when execution exits it.

The following control statements are supported by C.

Control Statement Description
Break statementTerminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
Continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
Goto statementTransfers control to the labeled statement.

While Loop in C

The most simple looping structure is a while loop. The following is the while loop syntax in the C programming language:

Syntax of While Loop in C:

while (condition) {
             statements;
}

This loop is entry-controlled. When using a while loop, a condition is assessed before processing the loop’s body.

The body of a loop is only run if and only if a condition is met.

The while loop in C programming is demonstrated in the following program:

#include<stdio.h>

int main()
{
	int number=1;	
	while(number<=10)
	{
		printf("%d\n",number);
		number++;
	}
	return 0;
}

Output of the Example code above:

1
2
3
4
5
6
7
8
9
10

Explanation:

The while loop is demonstrated in the program above. Using a while loop, we printed a succession of integers in the above program, ranging from 1 to 10.

  • A variable called num has been initialized with the number 1. The variable is first set to 1 because we will be printing from 1 to 10. If you want to start printing at 0 during initialization, supply the value 0.

  • The while loop will execute the body until the value of num equals 10 because we have specified a condition (num=10). The loop will then be broken, and control will move outside of it.

  • We have a print function to print our number and an increment operation to increase the value for each loop execution in the body of a loop. Num’s initial value is 1, and during execution, it changes to 2, and following that, it changes to 3. When the value reaches 10, the process will stop and print the series on the console, breaking the loop.

  • The value will be written on a new line because the formatting character “n” is used.

You can test the above example here! ➡ C Online Compiler

Do-While loop in C

Similar to the while loop, a do…while loop in C always executes the condition after the body of the loop. Another name for it is an exit-controlled loop.

The C programming language’s syntax for a do while loop is as follows:

Syntax of Do-While Loop in C:

 do {
  statements
} while (expression);

The body is only run if and only if the condition is true, as we observed with a while loop.

In some circumstances, even if the condition is false, we must run the loop’s body at least once.

The use of a do-while loop can do this kind of action.

Here is a C example of a do-while loop that prints a table of number 2:

#include<stdio.h>
int main()
{
	int number=1;
	do	
	{
		printf("%d\n",2*number);
		number++;	
	}while(number<=10);
	return 0;
}

Output of the Example code above:

2
4
6
8
10
12
14
16
18
20

In the example above, a do-while loop was used to print the multiplication table for two. Let’s examine how the series was printed by the program.

  • First, we’ve assigned the starting value of the variable “number” to be 1. After that, we created a do-while loop.

  • We have a print function in a loop that will output the series by multiplying the number value by 2.

  • The value of number will rise by 1 and be displayed on the screen after each increment.

  • number has a value of 1 at the beginning. The print function will be used in the loop’s body in the following manner: number will display the value two if number=1, since 21=2.

    This will continue until the number number equals 10. A statement that comes just after the loop will then be performed after the loop has ended. Return 0 in this instance.

For loop in C

Using a for loop, which is a repetition control structure, you can quickly create a loop that has to run a certain number of times.

In “C” programming, a for loop is a more effective loop structure. For loop syntax in C, the overall structure is as follows:

for ( init; condition; increment ) {
statement(s);
}

Here is an example of a “for” loop’s control flow:

  • The first and only execution of the init step occurs. Any loop control variables can be declared and initialized at this phase. As long as a semicolon appears, no statement is necessary here.

  • The condition is then assessed. The body of the loop is executed if the condition is true. If it is false, the body of the loop is skipped, and the statement immediately following the “for” loop takes control.

  • The increment statement is once again in control of the flow of control when the for loop’s body has finished running. You can update any loop control variables using this statement. As long as a semicolon follows the condition, this statement may be omitted.

  • Now a new assessment of the state is made. If so, the loop is activated, and the operation is repeated (body of loop, then increment and decrement step, and then again condition). The “for” loop comes to an end when the condition is false.

The C programming example below shows how to use a for loop:

#include<stdio.h>
int main()
{
	int num;
	for(num=1;num<=10;num++)	//for loop to print 1-10 numbers
	{
		printf("%d\n",num);		//to print the number
	}
	return 0;
}

Output of the Example code above:

1
2
3
4
5
6
7
8
9
10

The for loop is used in the program above to print a series of numbers from 1 to 10.
To store values, we have declared a variable using the int data type.

  • In the startup phase of the for loop, we gave the variable number the value 1. We have outlined our condition in the condition part, followed by the increment component.

  • We have a print function in the body of a loop that prints the numbers in the console on a new line. After the first repetition, the value will be increased by one and become two.

  • The value one is stored as number. The variable number currently has a value of 2. Since the condition is true, the condition will be rechecked, and the loop will be run.

Which loop to Select?

The selection of a loop is usually a difficult issue for a programmer. To select a loop, follow these steps:

  • Analyze the issue to see if it requires a pre-test or post-test loop.
  • Use a while or for loop if a pre-test is required.
  • If a post-test is required, a do-while loop should be used.

Summary

  • In C, define the loop: A A loop is a fundamental concept in any programming language. Conditional statements are used to implement loops C.

  • In C, a block of loop control statements is performed until the condition turns false.

  • In C programming, loops are classified as either entry-controlled or exit-controlled.

  • List the following C loop control instructions: While, do-while, and loop control instructions are all available in C programming.

  • For and while loops are entry-controlled loops C programming.

  • In C, a do-while loop is an exit control loop.

Leave a Comment