Control Structures in C++
C++ has a variety of control structures that can be used to control the flow of a program. These structures include if statements, switch statements, while loops, do-while loops, and for loops.
Let’s look at an example of a while loop:
#include <iostream>
int main()
{
int x = 0;
while (x < 10)
{
std::cout << "x = " << x << std::endl;
x++;
}
return 0;
}
Output
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
In this example, we have used a while loop to print out the value of x from 0 to 9. We have also used the increment operator (++) to increase the value of x each time the loop is executed.