Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Switch Statement | Introduction to Conditional Statements
C++ Conditional Statements
course content

Contenido del Curso

C++ Conditional Statements

C++ Conditional Statements

1. Introduction to Conditional Statements
2. Conditional Control Flow Practice
3. Advanced Topics

book
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).

h

switch

copy
12345678910111213141516
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.

cpp

main

copy
1234567891011121314151617181920212223242526
#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:

cpp

main

copy
123456789101112131415161718192021222324
#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.

cpp

main

copy
12345678910111213141516171819202122
#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; } }
Tarea
test

Swipe to show code editor

  1. Write a switch statement that prints the corresponding day of the week based on the value of day_of_week.
  2. If the value is not a valid day, output The day doesn't exist in the console.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 6
toggle bottom row

book
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).

h

switch

copy
12345678910111213141516
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.

cpp

main

copy
1234567891011121314151617181920212223242526
#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:

cpp

main

copy
123456789101112131415161718192021222324
#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.

cpp

main

copy
12345678910111213141516171819202122
#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; } }
Tarea
test

Swipe to show code editor

  1. Write a switch statement that prints the corresponding day of the week based on the value of day_of_week.
  2. If the value is not a valid day, output The day doesn't exist in the console.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt