Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation 1 Session 5 + handout Ishani Chakraborty.

Similar presentations


Presentation on theme: "Recitation 1 Session 5 + handout Ishani Chakraborty."— Presentation transcript:

1 Recitation 1 Session 5 + handout Ishani Chakraborty

2 Switch statement Multi-way branching. Implemented through data structure called Jump Table. A Jump Table is an array where entry i is the address of the code implemented if switch index = i.

3 Preferred over if-else if Number of cases > 4. Range of values is small.

4 switch(n) { case 0: P1printf("You typed zero\n"); break; case 1: case 9: P2printf("n is a perfect square\n"); break; case 2: P3printf("n is an even number\n"); default: P4printf("Only single-digit numbers are allowed\n"); break; } Addr of instruction P1 Addr of instruction P2 Addr of ……… instruction P3 Addr of instruction P2 Addr of instruction P4 Jump Table: Resides somewhere in memory, address obtained from dissembled code 019Def2 switch_logical(n) { if (n>9) goto instruction for default goto jt[n]); …… } jt[10] = { Addr(P1), Addr(P2), Addr(P3), Addr(default), Addr(default), Addr(P4) }

5 switch(n) { case 100: P1printf("You typed zero\n"); break; case 101: case 109: P2printf("n is a perfect square\n"); break; case 102: P3printf("n is an even number\n"); default: P4printf("Only single-digit numbers are allowed\n"); break; } Addr of instruction P1 Addr of instruction P2 Addr of ……… instruction P3 Addr of instruction P2 Addr of instruction P4 Jump Table: Resides somewhere in memory, address obtained from dissembled code 019Def2 switch_logical(n) { n_i = n-100; if (n_i>9) goto instruction for default goto jt[n_i]); …… } jt[10] = { Addr(P1), Addr(P2), Addr(P3), Addr(default), Addr(default), Addr(P4) }

6


Download ppt "Recitation 1 Session 5 + handout Ishani Chakraborty."

Similar presentations


Ads by Google