Some questions on switch statements

  1. What is the nested if statement which is equivalent to each of the following switch statements?

    1.          switch (i) {
          case 1:    j =i; 
          			 break;
          			 
          case 2:    j = i*2; 
                       break;
                       
          case 3:    j = i*3; 
                       break;
                       
          default:    j = 0; 
                       break;
                  }
      

    2.          switch (i) {
          case 3:    j = i; 
          			 
          case 2:    j = j*i; 
                       break;
                  }
      

  2. 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.

  3. 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.