In the realm of programming languages, loops are utilized to execute sets of statements repeatedly until a specific condition is satisfied. For an in-depth understanding of C++ loops, readers can refer to the following article by Mytour.
Exploring C++ Loops
Article Table of Contents
1. How C++ Loops Operate?
2. Types of Loops in C++
3. While Loop in C++
4. For Loop in C++
5. Nesting Loops in C++
6. Do...While Loop in C++
7. Skipping Loops in C++
7.1. Break Statement
7.2. Continue Statement
How C++ Loops Operate?
A sequence of statements is executed until a condition is evaluated as True. The sequence of statements executed, enclosed within curly braces {}, is called the body of the loop.
After each execution of the loop body, the condition is checked. If the condition evaluates to True, the loop body is executed again. Otherwise, if the condition evaluates to False, the loop body will not be executed.
Types of Loops in C++
In C++, there are three different types of loops, including:
- While Loop.
- For Loop.
- Do-While Loop.
While Loop in C++
The while loop is utilized to repeat one or a set of statements if a given condition is evaluated as true. This loop handles statements in 3 steps:
- Initialize variable (e.g., int x=0;).
- Condition (e.g., while(x <>).
- Increment or decrement variable (x++ or x-- or x=x+2).
The syntax of the While loop is:
Variable initialization;
while (condition)
{
Statements;
Increment or decrement variable;
}
For Loop in C++
The for loop is used to execute a set of statements continuously until a specific condition is satisfied. We can refer to it as an Open-Ended Loop.
The general format of the for loop in C++ is:
for(initialization; condition; increment/decrement)
{
Block of statements;
}
In a for loop, there are precisely 2 semicolons, one after initialization and one after the condition. In this loop, there can be multiple initializations or increments/decrements, separated by the comma operator. Additionally, the for loop has only one condition.
Nested Loops in C++
Furthermore, we can nest loops, such as nesting a for loop within another loop. The basic syntax for nested loops is:
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
Statement;
}
}
Do...while Loop in C++
In certain situations, we may need to execute the loop body before checking the condition. In such cases, we can handle it by using the do-while loop. The do statement evaluates the loop body before starting, and the while statement is used to check the condition.
The general structure of the do...while loop is:
do
{
// statements
}
while(condition);
Skip Loop in C++
Sometimes during loop execution, we may need to skip part of the loop or exit the loop immediately when a specific condition is evaluated as True. In the C programming language, it allows transitioning from one statement to another within the loop or breaking out of the loop.
1. Break Statement
If the break statement appears within a loop, the loop will exit immediately, and the program will continue executing the statements after the loop.
2. Continue Statement
This statement directly accesses the condition check, then proceeds with the iteration. When encountering the continue statement, the cursor exits the current cycle of the loop and starts the next cycle.
In this article, Mytour has just introduced you to loops in C++. In the upcoming articles, Mytour will continue presenting conditional statements in C++ and how to use these statements.
