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
course content

Contenido del Curso

C++ Conditional Statements

Switch StatementSwitch Statement

The switch statement is also one of the control structure that simplifies decision-making in your C++ 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). And its syntax looks like this:

  • 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.cpp

Yes, you can achieve this with a simple if statement, and it would look something like this:

cpp

main.cpp

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.cpp

Tarea

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

¿Todo estuvo claro?

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

Contenido del Curso

C++ Conditional Statements

Switch StatementSwitch Statement

The switch statement is also one of the control structure that simplifies decision-making in your C++ 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). And its syntax looks like this:

  • 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.cpp

Yes, you can achieve this with a simple if statement, and it would look something like this:

cpp

main.cpp

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.cpp

Tarea

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

¿Todo estuvo claro?

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