MCS-201 · Unit 3

Decision and Loop Control Statements

Section 3 of 7

Block 1 · Unit 3Decision and Loop Control Statements11 min · Section 3 of 7

3.3The switch Statement

Covers IGNOU MCS-201 §3.2.2

When a decision depends on one variable taking one of many fixed values, such as a menu choice 1, 2, 3 or 4, a long else if ladder becomes hard to read. The switch statement expresses this kind of choice directly.

How switch works

Syntax
switch (expression) {
    case constant1:
        statements
        break;
    case constant2:
        statements
        break;
    ...
    default:
        statements
}
  1. The expression is evaluated once.
  2. Control jumps to the case label whose constant equals that value.
  3. Statements run from there until a break or the end of the switch.
  4. If no case matches, control jumps to default (if present); otherwise the whole switch is skipped.

Rules for a switch statement:

RuleExample
The expression must be an integer type (int, char, long, enum)switch (choice), switch (grade)
Each case label must be a constant integer expressioncase 1:, case 'A':, case MAX:
Case values must be uniquetwo case 2: labels are an error
float, double and strings cannot be usedcase 2.5: is an error
default is optional and may appear anywhereusually written last

break and fall-through

Without break, execution continues into the next case’s statements. This is called fall-through. It is useful when several values share one action (stack the labels), and a bug when a break is forgotten.

switch versus else-if

Aspectswitchelse-if ladder
TestsEquality with constant integer valuesAny condition (ranges, &&, ||, floats)
Readability for many fixed valuesVery clearLong and repetitive
Ranges such as 60–75Not possible directlyNatural
Typical useMenus, command codes, character optionsGrades, slabs, ranges

Key takeaways

  • switch compares one integer expression with constant case labels.
  • Execution starts at the matching label and continues until break or the end.
  • Stacked labels share code; a forgotten break causes unwanted fall-through.
  • default handles all unmatched values and is optional.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which can be used as the controlling expression of a switch?

Show answer

Answer: C. char option

The expression must have an integer type, and char is an integer type. Floating values and strings are not allowed.

Question 2 · multiple choice

What does this program print?

C · fall.c
#include <stdio.h>

int main(void)
{
    int k = 2;
    switch (k) {
    case 1: printf("one ");
    case 2: printf("two ");
    case 3: printf("three ");
    default: printf("other ");
    }
    printf("\n");
    return 0;
}
Show answer

Answer: C. two three other

Control jumps to case 2 and, with no break anywhere, falls through every following case including default. case 1 is before the entry point, so it does not run.

Question 3 · multiple choice

What happens if no case matches and there is no default?

Show answer

Answer: B. The switch body is skipped entirely

With no match and no default, no statement in the switch runs and execution continues after it.

Question 4 · multiple choice

Which case label is valid?

Show answer

Answer: D. case 'Y':

A character constant is a constant integer expression. Variables, floating values and comma lists are not allowed as labels.

Question 5 · multiple choice

In which situation is an else if ladder better than switch?

Show answer

Answer: C. Assigning grades from mark ranges such as 60–75

switch tests equality with single constants and cannot express ranges; the other three are fixed-value choices that suit switch.

Question 6 · short answer

Write a switch that prints “Vowel” for the lower-case letters a, e, i, o and u, and “Consonant” for any other letter stored in ch.

Show worked answer

switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("Vowel\n"); break; default: printf("Consonant\n"); }. The five vowel labels are stacked so they share one action through intentional fall-through, the break stops execution before default, and every other character reaches default.

↑ ↓ move↵ openTab switch type