C

Looping Statements In C – The Most Detailed

During programming, we may need to execute a specific code statement multiple times. We can write the code statement as many times as we need it to execute, but this is inefficient because what if you need a code statement to execute 100 times? This is why loops are used.
Loops are used in any programming language, including C, to execute a single statement or a set of statements repeatedly until a specific condition is met. The loop statements in C are shared by us as below.

What exactly is a loop in C?

Looping C statements repeat the sequence of statements until the stated condition is false. A loop in C is made up of two parts: the loop body and the control statement. The control statement is a set of conditions that instructs the body of the loop to execute until the specified condition is met. The C loop’s purpose is to repeatedly execute the same code.

While Loop in C

The most basic looping structure is the while loop. The syntax for loops in the C programming language is as follows:

While Loop Syntax in C:

while (condition) {
             statements;
}

It is a loop with an entry control. Before processing the loop’s body, a condition is evaluated in a while loop. The body of a loop is executed only if a condition is true. After the body of a loop is completed, control returns to the beginning and the condition is checked; if true, the process is repeated until the condition becomes false. When the condition is false, the control exits the loop.
After exiting the loop, control is passed to the statements following the loop. A loop’s body can contain multiple statements. If it only contains one statement, the curly braces are optional. Even if we only have one statement in the body, it is a good practice to use curly braces.
If the condition in a while loop is not true, the body of the loop will not be executed, not even once. It differs in the do while loop, as we will see shortly.
The following program demonstrates a while loop in C programming:

#include<stdio.h>
#include<conio.h>
int main()
{
	int num=1;	//initializing the variable
	while(num<=10)	//while loop with condition
	{
		printf("%d\n",num);
		num++;		//incrementing operation
	}
	return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

Do-While loop in C

A do…while loop in C is similar to a while loop in that the condition is always executed after the body of the loop. It’s also known as an exit-controlled loop.
In the C programming language, the syntax of a do while loop is as follows:
Do-While Loop Syntax in C:

do {
  statements
} while (expression);

The body is executed if and only if the condition is true, as we saw in a while loop. In some cases, even if the condition is false, we must execute the body of the loop at least once. A do-while loop can be used to perform this type of operation.
The body of a loop is always executed at least once in the do-while loop. It checks the condition after the body has been executed. If the condition is true, the body of the loop is executed again; otherwise, control is transferred out of the loop.
Similarly to the while loop, once control is removed from the loop, the statements immediately following the loop are executed.
The key distinction between the while and do-while loops is that the while is written at the beginning of the while loop. The while condition is written at the end of the do-while loop and is followed by a semi-colon (;)
The following C loop program demonstrates the operation of a do-while loop:
A do-while loop in C is shown below to print a table of number 2:

#include<stdio.h>
#include<conio.h>
int main()
{
	int num=1;	//initializing the variable
	do	//do-while loop 
	{
		printf("%d\n",2*num);
		num++;		//incrementing operation
	}while(num<=10);
	return 0;
}

Output:

2
4
6
8
10
12
14
16
18
20

We printed a multiplication table of 2 using a do-while loop in the preceding example. Let’s take a look at how the program printed the series.

Continue after a brief break

Break and continue are two very important looping keywords. The break command will exit the loop that is immediately surrounding it, regardless of its conditions. Break is useful when we need to exit a loop for some reason. Assume the program we’re working on is a two-player checkers game. The program’s basic structure could look like this:

while (true)
{
    take_turn(player1);
    take_turn(player2);
}
This will cause the game to alternate between player 1 and player 2. The only issue with this logic is that there is no way to exit the game; the loop will continue indefinitely! Instead, consider the following:
while(true)
{
    if (someone_has_won() || someone_wants_to_quit() == TRUE)
    {break;}
    take_turn(player1);
    if (someone_has_won() || someone_wants_to_quit() == TRUE)
    {break;}
    take_turn(player2);
}
This code achieves our goal: the main loop of the game will continue under normal conditions, but if a special condition (winning or exiting) occurs, the flow will stop and our program will do something else.
Another keyword that controls the flow of loops is continue. If you’re in a loop and hit a continue statement, the loop will stop its current iteration, update itself (in the case of for loops), and restart from the beginning. The continue statement essentially says, “Now that this iteration of the loop is complete, let’s continue with the loop without executing whatever code comes after me.” Assume we’re implementing a Monopoly game. We want to use a loop to control whose turn it is, but controlling turns in Monopoly is a little more complicated than in checkers. The basic structure of our code could then look like this:
for (player = 1; someone_has_won == FALSE; player++)
    {
        if (player > total_number_of_players)
        {player = 1;}
        if (is_bankrupt(player))
        {continue;}
        take_turn(player);
    }
This way, if one player is unable to take her turn, the game does not stop for everyone; we simply skip her and continue with the next player’s turn.

Conclusion

We learned about for, while, and do-while loops in C and why they are important, as well as seeing them in action with multiple code examples. We also studied break and continue statements.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button