For loop
Syntax
{
// code block to be executed
}
Statement 1: (initialization) is executed (one time) before the execution of the code block.
Statement 2: defines the condition for
executing the code block.
Statement 3: (increment/decrement) is
executed (every time) after the code block has been executed.
while
loop:
Initialization;
while (condition)
{
// code block to be executed
Increment/decrement;
}
Example 02:
do…while loop:
Syntax
Initialization;
do
{
// code block to be executed
Increment/decrement;
}
while (condition);
Example 03:
In C++ since break and continue are control flow statements used to alter the execution of loops (for, while and do-while). Here’s how each works:
break
Reason: To Break out of a Loop Extremely useful when it comes to escaping from a loop, break and pass keywords would basically allow you to control when and where respectably, the loop will end or continue.
continue
Used for: To escape from the further code execution in the same iteration of loop & go to the next iteration. It is used to skip the remaining code inside the loop if a specific condition is met.
Example 04:
Goto
The goto Statement in C++, the goto statement provides an unconditional jump from the goto statement to a labeled statement within the same function. It is often viewed as a last resort for control flow since it can make the code harder to read and maintain.
label: an identifier, followed by a colon (:) This is the label to which we want our control flow to jump when the goto statement is encountered.
Example 05:
0 Comments