Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Switch
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 15 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 15 of 50CamboFreelanceJune 19, 2026

C++ Switch

Learn the C++ switch statement: match values with case labels, use break, default, and intentional fall-through with practical examples.


Tutorials › C++ › Switch
Beginner6 min readLesson 15 of 50

In This Lesson

  • Switch Syntax
  • break and default
  • Fall-Through
  • Quiz

The switch Statement

switch is an alternative to a long chain of if-else if. It matches an expression against a list of constant values (case labels).

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code if no case matches
}
#include <iostream>
using namespace std;

int main() {
    int day;
    cout << "Enter day (1-7): "; cin >> day;

    switch (day) {
        case 1: cout << "Monday";    break;
        case 2: cout << "Tuesday";   break;
        case 3: cout << "Wednesday"; break;
        case 4: cout << "Thursday";  break;
        case 5: cout << "Friday";    break;
        case 6: cout << "Saturday";  break;
        case 7: cout << "Sunday";    break;
        default: cout << "Invalid day";
    }
    cout << endl;
    return 0;
}

Sample Run:

Enter day (1-7): 5
Friday

break and default

  • break — exits the switch block. Without it, execution "falls through" to the next case.
  • default — optional, runs when no case matches (like else).

Fall-Through Example

Intentional fall-through lets multiple cases share code:

int month = 4;
switch (month) {
    case 4:
    case 6:
    case 9:
    case 11:
        cout << "30 days" << endl;   // April falls through to here
        break;
    case 2:
        cout << "28 or 29 days" << endl;
        break;
    default:
        cout << "31 days" << endl;
}

Output:

30 days
Note: The switch expression must evaluate to an integer or char type. You cannot use string in a switch statement in C++.

Exercise

Write a switch-based calculator: read two numbers and an operator (+, -, *, /), then print the result.

Show Solution
#include <iostream>
using namespace std;

int main() {
    double a, b;
    char op;
    cout << "Enter: num op num (e.g. 5 + 3): ";
    cin >> a >> op >> b;

    switch (op) {
        case '+': cout << a + b; break;
        case '-': cout << a - b; break;
        case '*': cout << a * b; break;
        case '/':
            if (b != 0) cout << a / b;
            else cout << "Error: divide by zero";
            break;
        default: cout << "Unknown operator";
    }
    cout << endl;
    return 0;
}

Quiz

  1. What does break do inside a switch?

    • A) Ends the entire program
    • B) Skips the current case
    • C) Exits the switch block
    • D) Goes to the default case
    Answer

    C) break exits the switch block, preventing fall-through to the next case.

  2. Can you use a string in a C++ switch expression?

    • A) Yes, always
    • B) Only with C++20
    • C) No, only integer and char types
    • D) Only with a compiler flag
    Answer

    C) C++ switch only works with integral types (int, char, enum).

Summary

  • switch matches an integer/char expression against case values.
  • Always use break to prevent unintended fall-through.
  • default handles values that match no case.
  • Multiple cases can share code by stacking them without break.
← Previous: Conditions Next: C++ Loops →
c++switchconditionsbeginner
PreviousLesson 14: C++ ConditionsNextLesson 16: C++ Loops
Back to All Tutorials