Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Switch Statement | Introduction to Program Flow
C++ Introduction (copy) 1769617216468njii0h

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?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  3
some-alt