Some questions on switch statements
- What is the nested if statement which is equivalent to each of the
following switch statements?
-
switch (i) {
case 1: j =i;
break;
case 2: j = i*2;
break;
case 3: j = i*3;
break;
default: j = 0;
break;
}
-
switch (i) {
case 3: j = i;
case 2: j = j*i;
break;
}
- Add a percentage operator to the calculator, which takes the
current (left hand)
value and finds the percentage of it given by the next value input.
Compile and test the resulting program.
- Add a default case to the example, which behaves in a suitable way
to handle incorrect typing. Test this version also.
Answers to some of these questions.
Back to the notes on switch statements.