Curriculum
Programming statements are typically executed in order: the first statement in a function is executed first, then the second, and so on. However, you might run into circumstances where a block of code needs to be run multiple times. In that case, you need to make use of what the programming world calls loop statements.
Looping statements in C/C++ are control statements that repeat the sequence of statements until the stated condition is met, without having to write the code repeatedly.
The loop body and the control statement are the two components that make up a loop. The body of the loop is told to continue running until the specified condition is met by the control statement, which is a set of conditions. A block of code or a string of logical statements that will be executed repeatedly make up the body of a loop. To put it simply, the loop’s goal is to repeatedly run the same code up until the control statement’s specified condition is met.
The following is the general form of a loop statement in most programming languages.

Note: The loop will run indefinitely if the control conditions are not clearly defined and specified. An infinite loop is one that keeps running and repeats the statements it is processing indefinitely.Â
Â
Looping statements in C are classified into two types based on where they appear in a program:
An entry control loop verifies the entry-level condition (at the beginning). It is called an entry control loop because of this. We must first verify the provided conditions before launching the loop. For this loop to work, the test condition has to be true all the time.
To help you better understand how an entry-controlled loop functions, here is a diagrammatic representation of one:

For and while loops are examples of entry-controlled loops, and the following section delves deeper into each to better understand them.
The while loop can be viewed as an iterative version of the if statement it is a control flow statement that enables code to be executed repeatedly based on a specified Boolean condition.
A while loop first verifies that the Boolean condition is satisfied before it executes the code. A Boolean condition is an expression that only has two possible outcomes: true or false. The loop will repeatedly loop if the condition’s outcome is true. However, the while loop will come to an end if the outcome of the condition is false.
The while statement, on the other hand, is what is contained in the while loop’s body. If the condition is satisfied, the code in the while statement will be run.
The while Loop’s general syntax is given below.

When using a while loop, a condition is assessed before processing the loop’s body. The body of a loop is only executed if and only if a condition is met. Control returns to the beginning of a loop after the body of the loop has been completed, where the condition is checked to see if it is true. If it is, the process is repeated until the condition is no longer true. The control exits the loop when the condition is false.
The example that follows checks to see if “Variable” is less than 200 and, if it is, then it executes the statements that are enclosed in brackets. It then loops until “Variable” is no longer less than 200.
Â
A for-loop is a control flow statement, similar to a while loop, that specifies iteration and allows code to be executed repeatedly. The for loop differs from other looping statements by using an explicit loop counter or loop variable that enables the loop’s body to understand the precise sequencing of each iteration.
A for-loop consists of two parts: a header that specifies the iteration and a body that is executed once for each iteration. The body can determine which iteration is being executed by the header’s frequent declaration of a loop counter or loop variable.
Note: A for loop differs from a while loop in that it specifies how many times the loop will run. In a while loop, the number of iterations is unknown until the program ends the loop when the condition is false.


This kind of looping, as opposed to entry-controlled loops, enables the user to execute the loop at least once before checking the condition, the control statement is written at the end of the Loop structure. In this instance, the loop can be executed even if the test condition is false.
To help you understand how it functions, here is a diagrammatic representation of an exit-controlled loop:

A do-while loop, like other types of loops, is a control flow statement that runs a block of code at least once and then, depending on a given Boolean condition, either executes the block repeatedly or stops it from running.
Do while loops differ fundamentally from for loops, while loops, and other types of loops in that the condition is checked at the conclusion of the loop. The body of the loop will be executed in a do-while loop first, and only then will the condition be assessed. Because of this, even if the condition is not met, the statement will always be executed at least once.

testExpression: a Boolean expression that evaluates to true or false.
The following example assigns the function readSensors () to the variable “x,” waits 50 ms, and then loops indefinitely until “x” is no longer less than 70: This is useful in applications where sensor readings such as humidity and temperature must be constantly checked in order to perform a specific reading.
In this lesson, we explored the essential concept of repetition statements in C++, covering the for, while, and do-while loops. These loops allow you to execute a block of code repeatedly based on specific conditions, enhancing code efficiency and readability. We discussed when to use each loop type, from situations where you know the exact number of repetitions to cases that require at least one execution before a condition check. With this foundational knowledge, you’re now equipped to handle repetitive tasks effectively in your code.
Mastering loops is a significant step in programming as it enables you to manage complex workflows, optimize your code, and tackle tasks that require repeated actions. As you practice using loops, consider how they interact with other programming elements and how different loop types can be used to achieve specific goals in your programs.
In our next lesson, we will transition to selection statements, which are another crucial aspect of control flow in programming. Unlike loops, selection statements allow you to choose different paths of execution based on specific conditions. We’ll delve into if, else, and switch statements, which enable decision-making in your code, allowing your programs to respond dynamically to different inputs and scenarios. This will further strengthen your control over program flow and bring you closer to creating responsive and flexible applications.