Contenido del Curso
C++ Conditional Statements
C++ Conditional Statements
Switch Statement
The switch
statement is also one of the control structure that simplifies decision-making in your programs. It provides an efficient way to evaluate an expression and execute different blocks of code based on the value of that expression. This can be especially handy when dealing with a large number of branching conditions.
It is primarily designed to work with numeric values, such as integers (int
) and characters (char
).
switch
switch (expression) { case value1: // Code to execute when expression matches value1 break; case value2: // Code to execute when expression matches value2 break; // More cases can be added as needed // Code to execute when expression matches valueN default: // Code to execute when none of the case labels match the expression }
-
The expression is evaluated once, and the program jumps to the appropriate
case
based on the value of the expression; -
Each
case
represents a specific value or set of values that the expression might match; -
The
break
statement is used to exit the switch block after the code for a particular case has been executed; -
The
default
is optional and is used when none of the specified case match the expression. It acts as a catch-all option.
Suppose you own a restaurant and want to provide a menu based on numerical codes listed. Here's the code representation of the menu items.
main
#include <iostream> int main() { // try to change the value int menu_number = 1; switch (menu_number) { case 1: std::cout << "Pizza" << std::endl; break; case 2: std::cout << "Burger with fries" << std::endl; break; case 3: std::cout << "Pasta with meatballs" << std::endl; break; default: std::cout << "We don`t have this in our menu" << std::endl; break; } }
Yes, you can achieve this with a simple if
statement, and it would look something like this:
main
#include <iostream> int main() { // try to change the value int menu_number = 1; if (menu_number == 1) { std::cout << "Pizza" << std::endl; } else if (menu_number == 2) { std::cout << "Burger with fries" << std::endl; } else if (menu_number == 3) { std::cout << "Pasta with meatballs" << std::endl; } else { std::cout << "We don`t have this in our menu" << std::endl; } }
It will work the same and as expected, but it is generally better to use a switch
statement because it offers a clearer, more readable, and, most importantly, easier-to-maintain and scalable way to manage this kind of control flow.
Omitting the break
keyword in a switch statement can result in unexpected program behavior, as the code will continue executing into subsequent cases. However, intentionally omitting break
can be a used to manage multiple cases within the same block of code. This approach, known as fall-through, it allows you to handle related cases together.
main
#include <iostream> int main() { // change the number and look how it affect the output int number = 3; switch (number) { case 1: std::cout << '1' << std::endl; case 2: std::cout << '2' << std::endl; case 3: std::cout << '3' << std::endl; case 4: std::cout << '4' << std::endl; case 5: std::cout << '5' << std::endl; break; } }
Swipe to show code editor
- Write a
switch
statement that prints the corresponding day of the week based on the value ofday_of_week
. - If the value is not a valid day, output
The day doesn't exist
in the console.
¡Gracias por tus comentarios!
Switch Statement
The switch
statement is also one of the control structure that simplifies decision-making in your programs. It provides an efficient way to evaluate an expression and execute different blocks of code based on the value of that expression. This can be especially handy when dealing with a large number of branching conditions.
It is primarily designed to work with numeric values, such as integers (int
) and characters (char
).
switch
switch (expression) { case value1: // Code to execute when expression matches value1 break; case value2: // Code to execute when expression matches value2 break; // More cases can be added as needed // Code to execute when expression matches valueN default: // Code to execute when none of the case labels match the expression }
-
The expression is evaluated once, and the program jumps to the appropriate
case
based on the value of the expression; -
Each
case
represents a specific value or set of values that the expression might match; -
The
break
statement is used to exit the switch block after the code for a particular case has been executed; -
The
default
is optional and is used when none of the specified case match the expression. It acts as a catch-all option.
Suppose you own a restaurant and want to provide a menu based on numerical codes listed. Here's the code representation of the menu items.
main
#include <iostream> int main() { // try to change the value int menu_number = 1; switch (menu_number) { case 1: std::cout << "Pizza" << std::endl; break; case 2: std::cout << "Burger with fries" << std::endl; break; case 3: std::cout << "Pasta with meatballs" << std::endl; break; default: std::cout << "We don`t have this in our menu" << std::endl; break; } }
Yes, you can achieve this with a simple if
statement, and it would look something like this:
main
#include <iostream> int main() { // try to change the value int menu_number = 1; if (menu_number == 1) { std::cout << "Pizza" << std::endl; } else if (menu_number == 2) { std::cout << "Burger with fries" << std::endl; } else if (menu_number == 3) { std::cout << "Pasta with meatballs" << std::endl; } else { std::cout << "We don`t have this in our menu" << std::endl; } }
It will work the same and as expected, but it is generally better to use a switch
statement because it offers a clearer, more readable, and, most importantly, easier-to-maintain and scalable way to manage this kind of control flow.
Omitting the break
keyword in a switch statement can result in unexpected program behavior, as the code will continue executing into subsequent cases. However, intentionally omitting break
can be a used to manage multiple cases within the same block of code. This approach, known as fall-through, it allows you to handle related cases together.
main
#include <iostream> int main() { // change the number and look how it affect the output int number = 3; switch (number) { case 1: std::cout << '1' << std::endl; case 2: std::cout << '2' << std::endl; case 3: std::cout << '3' << std::endl; case 4: std::cout << '4' << std::endl; case 5: std::cout << '5' << std::endl; break; } }
Swipe to show code editor
- Write a
switch
statement that prints the corresponding day of the week based on the value ofday_of_week
. - If the value is not a valid day, output
The day doesn't exist
in the console.
¡Gracias por tus comentarios!