Select Case
As you have seen from the If…Elseif…Elseif…Else statement, when there are many choices, the structure can be hard to follow. Some programming languages have an alternative structure when this happens. The Select Case or Switch Case statement is also a decision structure that is sometimes preferred because code might be easier to read and understand, by people.
The Select Case structure takes a variable and then compares it to a list of expressions. The first expressions that is evaluated as, “True” is executed and the remaining of the select case structure is skipped over, just like an If…ElseIf… statement (but not in all languages!). There are several different ways to create your expression. You can just use a value (a single digit for example and then it does an equal comparison), several digits, a range or having a regular expression. Just like the If structure, there is an optional “Else” that can be placed at the end as a catch all. The general form of a Select…Case statement (in most computer programming languages), takes the generic form of:
In this example program, the user enters in a grade letter. The letter grades are A, B, C, D & F. The computer will tell you if you are doing well, average or poorly. If the user enters in a grade that is not A, B, C, D or F, the computer will tell you that you have entered in an invalid grade.
Top-Down Design for Select Case statement

Flowchart for Select Case statement

Pseudocode for Select Case statement
Code for Select Case statement
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program checks your grade
6
7#include <stdio.h>
8#include <ctype.h>
9
10int main() {
11 // this function checks your grade
12 char gradeLevel; // a single character
13
14 // input
15 printf("Enter grade mark as a single character(ex: A, B, ...): ");
16 scanf("%c", &gradeLevel);
17
18 // process and output
19 // Note you need the break in C or it will move to next
20 // line in switch statement if it is true again
21 switch (toupper(gradeLevel)) {
22 case 'A':
23 printf("Excellent!\n");
24 break;
25 case 'B':
26 printf("Good job!\n");
27 break;
28 case 'C':
29 printf("Average.\n");
30 break;
31 case 'D':
32 printf("Poor.\n");
33 break;
34 case 'F':
35 printf("Fail.\n");
36 break;
37 default:
38 printf("Invalid grade.\n");
39 }
40
41 printf("\nDone.\n");
42 return 0;
43}
1// Copyright (c) 2020 St. Mother Teresa HS All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program checks your grade
6
7#include <iostream>
8#include <cctype>
9
10int main() {
11 // this function checks your grade
12 char gradeLevel; // a single character
13
14 // input
15 std::cout << "Enter grade mark as a single character(ex: A, B, ...): ";
16 std::cin >> gradeLevel;
17
18 // process and output
19 // switch in C++ can not support strings, only numbers and char
20 // also note you need the break in C++ or it will move to next
21 // line in switch statement if it is true again
22 switch (toupper(gradeLevel)) {
23 case 'A':
24 std::cout << "Excellent!" << std::endl;
25 break;
26 case 'B':
27 std::cout << "Good job!" << std::endl;
28 break;
29 case 'C':
30 std::cout << "Average." << std::endl;
31 break;
32 case 'D':
33 std::cout << "Poor." << std::endl;
34 break;
35 case 'F':
36 std::cout << "Fail." << std::endl;
37 break;
38 default:
39 std::cout << "Invalid grade." << std::endl;
40 }
41
42 std::cout << "\nDone." << std::endl;
43}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program checks a student's grade
4*/
5
6using System;
7
8/*
9 * The Program class
10*/
11class Program {
12 static void Main() {
13 // this function checks a student's grade
14
15 // create Scanner object for user input
16 Console.Write("Enter your grade: ");
17 string grade = Console.ReadLine();
18
19 // process & output
20 switch (grade.ToUpper()) {
21 case "A":
22 Console.WriteLine("Excellent!");
23 break;
24 case "B":
25 Console.WriteLine("Good job!");
26 break;
27 case "C":
28 Console.WriteLine("Average.");
29 break;
30 case "D":
31 Console.WriteLine("Poor.");
32 break;
33 case "F":
34 Console.WriteLine("Fail.");
35 break;
36 default:
37 Console.WriteLine("Invalid grade.");
38 break;
39 }
40
41 Console.WriteLine("\nDone.");
42 }
43}
1/**
2 * Created by: Mr. Coxall
3 * Created on: Sep 2020
4 * This program checks a student's grade
5 */
6
7package main
8
9import (
10 "fmt"
11 "strings"
12)
13
14func main() {
15 // this function checks a student's grade
16
17 var grade string
18
19 // input
20 fmt.Print("Enter your grade: ")
21 fmt.Scanln(&grade)
22
23 // process and output
24 switch strings.ToUpper(grade) {
25 case "A":
26 fmt.Println("Excellent!")
27 case "B":
28 fmt.Println("Good job!")
29 case "C":
30 fmt.Println("Average.")
31 case "D":
32 fmt.Println("Poor.")
33 case "F":
34 fmt.Println("Fail.")
35 default:
36 fmt.Println("Invalid grade.")
37 }
38
39 fmt.Println("\nDone.")
40}
1/*
2 * This program checks a student's grade
3 *
4 * @author Mr Coxall
5 * @version 1.0
6 * @since 2020-09-01
7 */
8
9import java.util.Scanner;
10
11public class Main {
12 public static void main(String[] args) {
13 // this function checks a traffic light
14
15 // create Scanner object for user input
16 Scanner scanner = new Scanner(System.in);
17
18 // input
19 System.out.print("Enter your grade: ");
20 String grade = scanner.nextLine();
21
22 // process & output
23 switch (grade.toUpperCase()) {
24 case "A":
25 System.out.println("Excellent!");
26 break;
27 case "B":
28 System.out.println("Good job!");
29 break;
30 case "C":
31 System.out.println("Average.");
32 break;
33 case "D":
34 System.out.println("Poor.");
35 break;
36 case "F":
37 System.out.println("Fail.");
38 break;
39 default:
40 System.out.println("Invalid grade.");
41 break;
42 }
43
44 // close the Scanner object
45 scanner.close();
46 System.out.println("\nDone.");
47 }
48}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program checks a student's grade
4 */
5
6const prompt = require("prompt-sync")()
7
8// input
9const grade = prompt("Enter your grade: ")
10
11// process & output
12switch (grade.toUpperCase()) {
13 case "A":
14 console.log("Excellent!")
15 break
16 case "B":
17 console.log("Good job!")
18 break
19 case "C":
20 console.log("Average.")
21 break
22 case "D":
23 console.log("Poor.")
24 break
25 case "F":
26 console.log("Fail.")
27 break
28 default:
29 console.log("Invalid grade.")
30 break
31}
32
33console.log("\nDone.")
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module checks a student's grade
6"""
7
8
9def main() -> None:
10 """The main() function checks a student's grade, returns None."""
11
12 # input
13 grade = input("Enter grade mark as a single character(ex: A, B, ...): ")
14
15 # process & output
16 # NOTE: This will only work on >= Python 3.10
17 match grade.upper():
18 case "A":
19 print("Excellent!")
20 case "B":
21 print("Good job!")
22 case "C":
23 print("Average.")
24 case "D":
25 print("Poor.")
26 case "F":
27 print("Fail.")
28 case _:
29 print("Invalid grade.")
30
31 print("\nDone.")
32
33
34if __name__ == "__main__":
35 main()
Example Output
