0% found this document useful (0 votes)
11 views17 pages

CD

The document discusses control statements in programming, focusing on the switch/case statement as an efficient alternative to multiple if-else statements for multi-way decision making. It outlines various translation techniques for switch/case, including jump tables, linear search, and binary search, each with distinct performance characteristics. The conclusion emphasizes the importance of selecting the appropriate translation technique to enhance execution speed, code size, and runtime efficiency.

Uploaded by

rohansahu02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

CD

The document discusses control statements in programming, focusing on the switch/case statement as an efficient alternative to multiple if-else statements for multi-way decision making. It outlines various translation techniques for switch/case, including jump tables, linear search, and binary search, each with distinct performance characteristics. The conclusion emphasizes the importance of selecting the appropriate translation technique to enhance execution speed, code size, and runtime efficiency.

Uploaded by

rohansahu02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Switch/Case

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.

(Direct Working Principle:

Used when case labels are contiguous or densely


Addressing) packed.
Compiler generates an array of pointers to code
blocks for each case.
The switch expression is used to compute an offset,
which directly jumps to the correct block.

Structure:

Requires minimal computation: only bounds


checking and array indexing.

Efficient: Constant-time branching — O(1) performance


Example:

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

Definition Working Principle:


Translates switch/case into a Control is transferred to the
chain of conditional comparisons,
similar to if-else statements. matching case block.

The compiler compares the


switch expression sequentially
against each case.
Pros:
Simple to implement.
Example Works with sparse or non-
sequential values.
No memory overhead like
jump tables.
if (x == 10) goto L1; Cons:
else if (x == 20) goto L2; Time complexity is O(n) — not
else if (x == 30) goto L3; efficient for large switch
else goto default; blocks.
Worst-case performance if
match is near the end or
absent.
Use Cases:
Small number of case labels.
Widely scattered or non-integer
case values
Binary Search Method (Decision
Tree)
Definition:
An optimized decision tree structure that performs case
matching using a binary search pattern.

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.

Real-world compilers like GCC translate switch/case using


CMP + JNE or optimized jump tables based on case density.
Optimizations in
switch/case Translation
Compiler techniques that improve switch/case performance and
reduce code size during target code generation.
1. Constant Folding
Simplifies known expressions at compile time.
Example: switch(2 + 1) → switch(3)
2. Dead Code Elimination
Removes unreachable or duplicate cases.
Example:
case 1: a = 5; break;
case 1: a = 10; ← removed
3. Real Compiler Behavior (e.g., GCC)
Uses heuristics to select optimal method
gcc -O2 may auto-choose jump tables or binary search trees based
on case range.
4.Technique Selection (Space-Time Trade-off)
Conclusion
•Switch/case is a high-level control construct that must be translated into
efficient low-level instructions by the compiler.

• Translation techniques vary based on case value patterns:


- Jump Table – Fastest, used for dense case values.
- Linear Search – Simple, used for sparse/unordered values.
- Binary Search – Efficient for sorted but sparse values.

• Proper technique selection improves:


-Execution speed
-Code size
-Runtime efficiency

• 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)

You might also like