Consider the following C switch statement: switch (i) { case 15: week = 3; day =
ID: 3766270 • Letter: C
Question
Consider the following C switch statement:
switch (i) {
case 15: week = 3; day = 1; break;
case 16: week = 3; day = 2; break;
case 17: week = 3; day = 3; break;
case 18: week = 3; day = 4; break;
case 19: week = 3; day = 5; break;
case 20: week = 3; day = 6; break;
case 21: week = 3; day = 7; break;
case 22: week = 4; day = 1; break;
case 23: week = 4; day = 2; break;
case 24: week = 4; day = 3; break;
case 25: week = 4; day = 4; break;
case 26: week = 4; day = 5; break;
case 27: week = 4; day = 6; break;
case 28: week = 4; day = 7; break;
case 29: week = 5; day = 1; break;
case 30: week = 5; day = 2; break;
case 31: week = 5; day = 3; break;
}
Would it make sense to use a jump table to implement this switch statement? Why
or why not?
Explanation / Answer
Would it make sense to use a jump table to implement this switch statement? Why
or why not?
Answer:
Using a jump table to implement the switch statement is recomended.
If you think about it, it should become apparent that a jump table approach is likely to give a highly consistent time of execution through the decision tree, whereas the if-else -if chain has a highly variable time of execution depending upon the particular value of the switched variable.
Actually I am giving here what is a jump table.
A jump table is a branch table or jump table is a method of transferring program control (branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch or jump instructions. It is a form of multiway branch. The branch table construction is commonly used when programming in assembly language but may also be generated by a compiler, especially when implementing an optimized switch statement where known, small ranges are involved with few gaps.
So you can use switch statement.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.