CD
CD
Statement
Translation
Technique
Group - 8
Introduction to
Control Statements
What are Control Statements? Importance of Control Statements:
Control statements manage the
flow of execution in a program,
deciding which code to run, when, •Control statements guide program
and how often. flow based on conditions or
Types of Control Statements: repetition.
1. Conditional Statements •They are essential for making
-if, if-else, if-else-if decisions and implementing logic.
-Used for decision making
2. Looping Statements
-for, while, do-while
-Repeat a block of code
3. Jump Statements
-break, continue, return
-Jump in control flow
Switch Case Statement
•What is a Switch Case?
•Used for multi-way decision making, as a cleaner alternative to multiple if-else statements.
•Syntax: - switch(expression) { Example: -
case value1:
// code int day = 3;
break; switch(day) {
case value2: case 1: printf("Monday"); break;
// code case 2: printf("Tuesday"); break;
break; case 3: printf("Wednesday"); break;
default: default: printf("Invalid day");
// code }
}
•Unlike if-else, switch/case can have many branches, so compilers use efficient
translation techniques like jump tables or binary search to improve execution speed and
avoid checking each case linearly
Why Not Simple if-else?
When using an if-else chain, the program checks each condition one by
one.
• For example:
if (day == 1) → Sunday
else if (day == 2) → Monday
else if (day == 3) → Tuesday ...
else if (day == 7) → Saturday
If the day is 1, it’s found immediately. But if the day is 7, the program has
to check all the previous conditions (day == 1, day == 2, ..., day == 6) before
matching the correct one at the end.
• This process of checking each condition sequentially is slow, especially
when there are many cases. For n cases, the worst-case scenario requires n
comparisons.
• This is called O(n) time complexity, which means that the more cases you
have, the longer it will take to find the match. If there are 100 cases, the
program might check 100 times!
This is exactly why switch/case is often preferred over if-else. In a
switch/case statement, instead of checking each condition one by one
like in if-else, the program can quickly jump to the correct case based on
the value of the variable. It’s much faster, especially for large numbers of
cases.
• Example:
switch(day) {
case 1: // Sunday
break;
case 2: // Monday
break; ...
case 7: // Saturday
break;
default:
// default case }
• In this way, a switch/case allows a faster lookup, especially when there
are many cases, making it much more efficient than if-else.
Methods for Translating switch/case
1. Jump Table (Lookup Table)
2. Linear Search Translation
3. Binary Search Translation
Each method has different performance and space characteristics.
A jump table is an array of branch targets (addresses)
Jump Table that allows direct indexing based on the case expression
value.
Structure:
Pros:
Extremely fast.
switch(x) { Ideal when values are
case 1: A; break; sequential or within a
case 2: B; break; small, dense range.
case 3: C; break;
} Cons:
High memory overhead
if case values are sparse.
Translated to: JMP Cannot handle non-
table[x - base] integer or far-apart
table = [addr_A, labels efficiently.
addr_B, addr_C]
Linear Search (Sequential if-else)
01 02
Working Principle:
The compiler sorts the case labels and generates a tree
of comparisons.
The switch expression is compared against the middle
case value.
Based on the result, the search continues on left or right
half.
Example Pros:
O(log n) time complexity.
More efficient than linear
search for many sparse cases.
case values: [5, 15, 25, 35, 45] Cons:
First compare x < 25 More complex to generate.
→ if yes: x < 15? Slightly more instructions than
→ if yes: x == 5? jump table.
→ else: x == 15? Use Cases:
→ if no: x < 35? Moderate to large number of
→ if yes: x == 25? sparse, sorted case values.
→ else: x == 35 or 45? When jump tables are
inefficient but linear search is
too slow.
Code Generation Example
What is code generation?
Final phase of compiler, converts intermediate code to assembly or
machine code while preserving original program meaning.
• Modern compilers like GCC and LLVM use hybrid strategies to balance
performance and space.
Thank You
Presented by :
Rishabh Sharma (2K22/SE/137)
Ritik Pal (2K22/SE/138)
Rohan Sahu (2K22/SE/139)
Rohan Sejwal (2K22/SE/140)
Rohan Singh (2K22/SE/141)