To alter normal flow of a Loop
Published by
sanya sanya
Break
"break" is a keyword used in programming languages like C, C++, Java, and Python to exit or terminate a loop prematurely. When the "break" statement is encountered within a loop structure (such as "for" or "while" loops), the execution of the loop is immediately terminated, and the control is transferred to the next statement following the loop.
Usage of "break":
- To exit a loop when a specific condition is met: You can use the "break" statement to exit a loop when a certain condition becomes true. This allows you to prematurely terminate the loop and continue with the rest of the program.
Points to remember when using "break":
- The "break" statement only terminates the innermost loop that encloses it. If you have nested loops, only the loop in which the "break" statement is encountered will be terminated, and the outer loops will continue their execution.
- The "break" statement is commonly used with conditional statements (such as "if" or "switch") to determine when to exit a loop.
- Be careful not to overuse "break" statements, as it can make the code harder to read and understand. Overusing "break" can also lead to unexpected program behavior.
Continue
"continue" is a keyword used to skip the remaining code within a loop iteration and move on to the next iteration. When the "continue" statement is encountered within a loop structure, it skips the remaining statements in the loop block and proceeds to the next iteration.
Usage of "continue":
- To skip specific iterations: You can use the "continue" statement to skip certain iterations of a loop based on a condition. It allows you to bypass a part of the loop code and move to the next iteration.
Points to remember when using "continue":
- The "continue" statement skips the remaining statements within the current iteration and proceeds to the next iteration of the loop.
- Like the "break" statement, the "continue" statement only affects the innermost loop that encloses it in the case of nested loops.
- Make sure to use "continue" judiciously, as improper usage can result in an infinite loop or unexpected behavior.
Example using "break":
#include
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Terminate the loop when i is equal to 3
}
cout << i << " ";
}
cout << "Loop ended." << endl;
return 0;
}
Output:
1 2 Loop ended.
Explanation: In this example, the loop starts from 1 and iterates up to 5. When the value of ‘i’ becomes 3, the "break" statement is encountered, causing the loop to terminate immediately. Therefore, only the values 1 and 2 are printed before the loop ends.
Example using "continue":
#include
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the rest of the loop when i is equal to 3
}
cout << i << " ";
}
cout << "Loop ended." << endl;
return 0;
}
Output:
1 2 4 5 Loop ended.
Explanation: In this example, the loop starts from 1 and iterates up to 5. When the value of ‘i’ becomes 3, the "continue" statement is encountered, causing the remaining code within the current iteration to be skipped. As a result, the value 3 is not printed, and the loop proceeds to the next iteration. The output shows the numbers 1, 2, 4, and 5 before the loop ends.
Library
WEB DEVELOPMENT
FAANG QUESTIONS