Introduction
Compiler design is the process of designing and implementing a compiler. The compilers convert program code written in different languages into machine-readable code that the computer can execute. Syntax analysis is one of the main steps during this process. We do handle different types of statements in this; one of the types of statements is the case statement in compiler design.

In this blog, we will discuss the case statement in compiler design. It is used to perform different statements based on the input value of the variable. We will also discuss one example to understand its syntax better.
Case Statement in Compiler Design
The case statement in compiler design is a conditional statement used to execute different blocks of code based on the evaluated value of the input expression. It is available in various programming languages, and the syntax varies depending on the language.
Syntax
Let's discuss the general syntax for a case statement in compiler design.
switch <expression> {
begin
first case value: statement_1
second case value: statement_2
third case value: statement_3
fourth case value: statement_4
.
.
(n-1)th case value: statement_(n-1)th
default: statement_nth
end
}
In the above syntax of a case statement in compiler design, the "switch" keyword initializes the case statement. An input "expression" is evaluated as the first step. The "case" keyword is used to express different values that the expression can take. The evaluated value is then matched with the following case values. For the case whose value matches the expression's value, its corresponding statement block is executed.
A "break" keyword is also present at the end of every statement in some languages used to exit the case statement if that block of statements is executed. The program might continue to run for the following cases if the break statement has not occurred.
Translation of the Case Statement
code to evaluate E into t
goto test
L1: code for S1
goto next
L2: code for S2
goto next
.....
Ln-1: code for Sn-1
goto next
Ln: code for Sn
goto next
test: if t = 1 goto L1
if t = 2 goto L2
.....
if t = Vn-1 goto Ln - 1
goto Ln
next:
When the "switch" keyword is encountered, the "test" and "next" and a temporary variable "t" are generated. A new label, "Li," is also created and used to store the value "Vi" of the constant "Li."
Related Article Switch Statements.