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

bookSwitch Statement

A switch statement is a control flow construct in programming used to execute one block of code out of multiple possible options, based on the value of a single variable or expression. It's a more structured and readable alternative to using multiple if-else statements when comparing the same value to several possible options.

main.cpp

main.cpp

switch.h

switch.h

copy
1234567891011121314151617181920212223242526272829
#include <iostream> int main() { int userOption = 1; // 1: Check, 2: Deposit, 3: Withdraw, 4: Exit switch (userOption) { case 1: std::cout << "Checking balance...\n"; break; case 2: std::cout << "Depositing money...\n"; break; case 3: std::cout << "Withdrawing money...\n"; break; case 4: std::cout << "Exiting. Thank you!\n"; break; default: std::cout << "Invalid option.\n"; break; } }

The userOption variable is checked, and if its value equals 1, the program displays the message for checking the account balance. The break statement then stops further execution within the switch-case block, preventing other cases from running.

  • break - statement means an exit from a block of code;

  • default - is an optional part but a useful one. This part will be executed if none of the cases doesn't fit.

The break keyword

However, there is an important aspect of the switch statement to keep in mind. If the break statement is intentionally removed from a case, the program will continue executing subsequent cases, even if their conditions do not match. This behavior, known as fall-through, can be useful in specific scenarios but may lead to unexpected results if not used carefully.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526
#include <iostream> int main() { // Example user choice: 1 for Check Balance, 2 for Deposit, etc. int userOption = 1; // Simulating a banking system menu using a switch statement switch (userOption) { case 1: // Check account balance std::cout << "Checking account balance..." << std::endl; case 2: // Deposit money std::cout << "Depositing money into your account..." << std::endl; case 3: // Withdraw money std::cout << "Withdrawing money from your account..." << std::endl; case 4: // Exit std::cout << "Exiting the system. Thank you for banking with us!" << std::endl; default: // Invalid option std::cout << "Invalid option. Please choose a valid menu option." << std::endl; } }

Without the break command, the program flow will ignore all the following checks and simply execute the commands of the following cases until it encounters the break statement or the end of the entire switch block.

question mark

What is the purpose of the break statement inside a switch block?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give an example of how fall-through works in a switch statement?

What are some scenarios where fall-through might be useful?

Can you explain the difference between using break and not using break in a switch statement?

Awesome!

Completion rate improved to 3.85

bookSwitch Statement

Swipe to show menu

A switch statement is a control flow construct in programming used to execute one block of code out of multiple possible options, based on the value of a single variable or expression. It's a more structured and readable alternative to using multiple if-else statements when comparing the same value to several possible options.

main.cpp

main.cpp

switch.h

switch.h

copy
1234567891011121314151617181920212223242526272829
#include <iostream> int main() { int userOption = 1; // 1: Check, 2: Deposit, 3: Withdraw, 4: Exit switch (userOption) { case 1: std::cout << "Checking balance...\n"; break; case 2: std::cout << "Depositing money...\n"; break; case 3: std::cout << "Withdrawing money...\n"; break; case 4: std::cout << "Exiting. Thank you!\n"; break; default: std::cout << "Invalid option.\n"; break; } }

The userOption variable is checked, and if its value equals 1, the program displays the message for checking the account balance. The break statement then stops further execution within the switch-case block, preventing other cases from running.

  • break - statement means an exit from a block of code;

  • default - is an optional part but a useful one. This part will be executed if none of the cases doesn't fit.

The break keyword

However, there is an important aspect of the switch statement to keep in mind. If the break statement is intentionally removed from a case, the program will continue executing subsequent cases, even if their conditions do not match. This behavior, known as fall-through, can be useful in specific scenarios but may lead to unexpected results if not used carefully.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526
#include <iostream> int main() { // Example user choice: 1 for Check Balance, 2 for Deposit, etc. int userOption = 1; // Simulating a banking system menu using a switch statement switch (userOption) { case 1: // Check account balance std::cout << "Checking account balance..." << std::endl; case 2: // Deposit money std::cout << "Depositing money into your account..." << std::endl; case 3: // Withdraw money std::cout << "Withdrawing money from your account..." << std::endl; case 4: // Exit std::cout << "Exiting the system. Thank you for banking with us!" << std::endl; default: // Invalid option std::cout << "Invalid option. Please choose a valid menu option." << std::endl; } }

Without the break command, the program flow will ignore all the following checks and simply execute the commands of the following cases until it encounters the break statement or the end of the entire switch block.

question mark

What is the purpose of the break statement inside a switch block?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
some-alt