Example

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Give me a break.

Example

We will get to those other rules shortly. Here is the previous example, with more explanation:

double discount;
// Usually code would be read in
char   code = 'B' ;   

switch ( code )
{
  case 'A':
    discount = 0.0;
    break;

  case 'B':
    discount = 0.1;
    break;

  case 'C':
    discount = 0.2;
    break;

  default:
    discount = 0.3;
}

System.out.println( "discount is: " 
  + discount );
  1. The integerExpression is evaluated.
    • In this example, the expression is the variable, code, which evaluates to the character 'B'.
  2. The case labels are inspected starting with the first.
  3. The first one that matches is case 'B'
  4. The corresponding statementList starts executing.
    • In this example, there is just one statment.
    • The statement assigns 0.1 to discount.
  5. The break statement is encountered.
  6. The statement after the switch statment is executed.
    • In this example, the println() statement

QUESTION 7:

If code is 'W' what is discount?