switch: looking at one variable

The switch construct offers an alternative to nested if statements. Switch can be used when the test involves a single variable with a small number of possible values.

The syntax is:

switch(variable) {
  case value: statement; statement; statement; break;
  case value: statement; statement; break;
  case value: statement; break;
  default: statement;
}

When a match is found, every statement is executed until there is a break.

Please study the material at each of the links below.

  1. Calculate tip based on service: Y or N
  2. Print season from month
Like us on Facebook:


OER: Open Educational Resource
Creative Commons License Author: Janet E. Joy; Publisher: Zebra0.com
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Creative Commons Attribution-NonCommercial 4.0 International License
Last modified: May 18 2017 14:08:23.