Beginner · C PROGRAMMING

Switch statements

What you will learn

Select one of several fixed cases.

Understand the concept

switch evaluates an integer expression and jumps to a matching case. break exits the switch. The default branch handles values without a matching case. Use a switch when cases are discrete values, rather than numeric ranges.

Read the example

The complete program appears below. Read it first, predict the output, then run it.

Expected output

Repair status

Practise

Add choice 3 for billing, then test an unsupported choice.

Common mistake

Without break, execution continues into the next case unless an earlier return exits the function.

Lesson discussion

Log in to participate.

#include <stdio.h>

int main(void) {
    int choice = 2;
    switch (choice) {
        case 1: puts("New repair"); break;
        case 2: puts("Repair status"); break;
        default: puts("Unknown option"); break;
    }
    return 0;
}
PRACTICE LAB

Read. Edit. Run.

C source · .c

Experiment with the program below. Your code and input are sent to the selected compiler only when you run it.

Tab indents · Shift+Tab unindents · Esc then Tab leaves the editor · Ctrl/Cmd+Enter runs · Ctrl/Cmd+S saves a draft

Changes save automatically to your account.

Input / output example

Example input
No input required.
Expected output for the original program
Repair status
Output will appear here.

Log in to save your progress

Ready when you are

Need help with your laptop or computer?

Send us the model and fault details. Start your repair request online or contact TCS Pune directly.

WA