Related Topics

C++ Programing
#include
int main() {
int option = 2;
switch (option) {
case 1:
std::cout << "Option 1 selected." << std::endl;
break;
case 2:
std::cout << "Option 2 selected." << std::endl;
break;
case 3:
std::cout << "Option 3 selected." << std::endl;
break;
default:
std::cout << "Invalid option." << std::endl;
break;
}
return 0;
}
In this example, if the variable “option” is equal to 2, the output will be “Option 2 selected.” Once the code inside the “case 2” block is executed, the “break” statement is encountered, causing the program to exit the switch block immediately and continue with the code following the switch statement.
The use of the “break” statement in a “switch” statement is a fundamental mechanism to prevent fall-through behavior, where code execution flows through multiple cases even after a matching case is found. Instead, it allows you to handle each case independently and avoid unintended side effects.
#include
int main() {
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue; // Skips the rest of the current iteration and moves to the next iteration.
}
std::cout << "Iteration: " << i << std::endl;
}
return 0;
}
n this example, when the value of “i” becomes 3, the “continue” statement is executed, and the loop immediately moves to the next iteration without printing “Iteration: 3”.
For “for” loops, you can achieve the same effect as the “continue” statement using an “if” statement to skip certain iterations based on a condition, but you won’t have a direct “continue” statement available like in “while” loops.
#include
int main() {
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue; // Skips the rest of the current iteration and moves to the next iteration.
}
std::cout << "Iteration: " << i << std::endl;
}
return 0;
}
And the labeled statement is defined as:
label_name:
// Statement(s)
Here’s an example of how you can use the “goto” statement:
#include
int main() {
int option;
start:
std::cout <> option;
if (option == 1) {
std::cout << "Option 1 selected." << std::endl;
} else if (option == 2) {
std::cout << "Option 2 selected." << std::endl;
} else {
std::cout << "Invalid option. Please try again." << std::endl;
goto start; // Jump to the "start" label to re-enter the input.
}
return 0;
}
In this example, the “goto” statement is used to create a loop-like behavior that repeats the input prompt if the user enters an invalid option. However, this could have been achieved more elegantly with a “while” loop or by using structured constructs like “if-else” statements.
It is essential to be cautious while using “goto” statements, as their misuse can lead to code that is difficult to read and maintain. In most cases, there are better, more structured ways to achieve the same functionality using loops or conditional statements. As a best practice, you should avoid using “goto” statements and opt for structured programming constructs whenever possible.
#include
int main() {
for (int i = 0; i < 5; /* No update statement */) {
std::cout << "Iteration: " << i << std::endl;
// Without an update statement, the loop will run indefinitely, or it won't run at all.
}
return 0;
}
In this example, the loop has no update statement, so the value of “i” will never change within the loop. If the initial value of “i” is less than 5 (which is true for “i = 0”), the loop condition “i < 5” will always be true, resulting in an infinite loop. The program will keep printing “Iteration: 0” indefinitely, and the loop will never terminate.
On the other hand, if the initial value of “i” is greater than or equal to 5 (e.g., “i = 5” or any value greater than 5), the loop condition will be false from the beginning, and the loop body will never execute. The loop will not run at all, and nothing will be printed.
To avoid these issues, it is crucial to make sure the loop counter is properly updated within the loop body or after each iteration in the “for” loop’s update statement to ensure the loop terminates as expected and behaves as intended. For example:
#include
int main() {
for (int i = 0; i < 5; /* No update statement */) {
std::cout << "Iteration: " << i << std::endl;
// Without an update statement, the loop will run indefinitely, or it won't run at all.
}
return 0;
}
In this corrected example, the loop counter “i” is updated using “i++” after each iteration, and the loop will run as expected, printing “Iteration: 0”, “Iteration: 1”, “Iteration: 2”, “Iteration: 3”, and “Iteration: 4”.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.