C++ Break and continue statements
In C++, the break
and continue
statements are used to control the flow of loops.
The break
statement is used to exit a loop, regardless of whether or not the loop's condition is true. When a break
statement is encountered, the loop is immediately exited and the program continues executing after the loop. The general syntax of a break
statement is as follows:
kotlinbreak;
For example:
cssfor (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
In this example, when the value of i
is equal to 5, the break
statement is executed, and the loop is exited. As a result, only the values of i
from 0 to 4 will be displayed.
The continue
statement is used to skip the current iteration of a loop and move on to the next iteration. When a continue
statement is encountered, the current iteration of the loop is skipped, and the program continues with the next iteration of the loop. The general syntax of a continue
statement is as follows:
kotlincontinue;
For example:
cssfor (int i = 0; i < 10; i++)
{
if (i == 5)
{
continue;
}
cout << i << endl;
}
In this example, when the value of i
is equal to 5, the continue
statement is executed, and the current iteration of the loop is skipped. As a result, the value of i
5 will not be displayed, but the loop will continue with the next iteration and display the values of i
from 0 to 9.
Comments
Post a Comment