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

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
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
}
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
#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;
}
}
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
#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;
}
}
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
#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;
}
}
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; } }
Tehtävä

Swipe to start coding

  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.

Ratkaisu

cpp

solution

#include <iostream>

int main()
{
int day_of_week = 3;
switch (day_of_week)
{
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
case 4:
std::cout << "Thursday" << std::endl;
break;
case 5:
std::cout << "Friday" << std::endl;
break;
case 6:
std::cout << "Saturday" << std::endl;
break;
case 7:
std::cout << "Sunday" << std::endl;
break;
default:
std::cout << "This day doesn't exist" << std::endl;
}
}

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 6
#include <iostream>

int main()
{
int day_of_week = 9;
switch (___)
{
case _:
std::cout << "Monday" << std::endl;
___;
// your code here

case 6:
std::cout << "Saturday" << std::endl;
break;
case 7:
std::cout << "Sunday" << std::endl;
___;
default:
___
break;
}
}

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

We use cookies to make your experience better!
some-alt