Data Structure
Contents:
Case Structure
Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. A case structure allows you to perform certain actions when a condition is met.
Example Code of Case Structure
Think of a case structure being similar to an elevator:
Michelle gets off on Floor 3
Michelle gets on an elevator that can only visit 1 floor before it has to be called from the main floor.
Michelle presses 3. Thereby setting the destination variable to 3 (within the elevator)
The elevator travels to the 1st floor which does not match with 3 the elevator continues
The elevator travels to the 2nd floor which does not match 3 the elevator continues
The elevator reaches the 3rd floor which does match 3. Therefore the elevator performs the actions required for the 3rd floor which is to open the front door and announce "Level 3".
switch(buttonpress)
{
case 1:
open_front_door();
announce_floor();
break; //return to the main floor
case 2:
open_rear_door();
announce_floor();
break;
case 3:
open_front_door();
announce_floor();
break;
case 4:
open_rear_door();
announce_floor();
break;
}
So if Michelle wanted to go level 4 by pushing the 4 button. The rear doors will open instead of the front doors.
"How might this look in an if/else statement?"
if(button_press == 1)
{
open_front_door();
announce_floor();
}
else if (button_press == 2)
{
open_rear_door();
announce_floor();
}
else if (button_press == 3)
{
open_front_door();
announce_floor();
}
else if (button_press == 4)
{
open_rear_door();
announce_floor();
}
"If so, why use a case structure?"
Case structures easily support multiple simple conditions that have similar actions compared to If/else statements.
So if wanted to just open up the front doors on all 4 floors we can do this
switch(buttonpress)
{
case 1:
case 2:
case 3:
case 4:
open_front_door();
announce_floor();
}
"what does "null" mean when it is put in a case statement?"
null is when a variable contains nothing.
So if the button was never pressed we could have our elevator say something to the user to press a button or just re-open the doors
Think of a case structure being similar to an elevator:
Michelle gets off on Floor 3
Michelle gets on an elevator that can only visit 1 floor before it has to be called from the main floor.
Michelle presses 3. Thereby setting the destination variable to 3 (within the elevator)
The elevator travels to the 1st floor which does not match with 3 the elevator continues
The elevator travels to the 2nd floor which does not match 3 the elevator continues
The elevator reaches the 3rd floor which does match 3. Therefore the elevator performs the actions required for the 3rd floor which is to open the front door and announce "Level 3".
switch(buttonpress)
{
case 1:
open_front_door();
announce_floor();
break; //return to the main floor
case 2:
open_rear_door();
announce_floor();
break;
case 3:
open_front_door();
announce_floor();
break;
case 4:
open_rear_door();
announce_floor();
break;
}
So if Michelle wanted to go level 4 by pushing the 4 button. The rear doors will open instead of the front doors.
"How might this look in an if/else statement?"
if(button_press == 1)
{
open_front_door();
announce_floor();
}
else if (button_press == 2)
{
open_rear_door();
announce_floor();
}
else if (button_press == 3)
{
open_front_door();
announce_floor();
}
else if (button_press == 4)
{
open_rear_door();
announce_floor();
}
"If so, why use a case structure?"
Case structures easily support multiple simple conditions that have similar actions compared to If/else statements.
So if wanted to just open up the front doors on all 4 floors we can do this
switch(buttonpress)
{
case 1:
case 2:
case 3:
case 4:
open_front_door();
announce_floor();
}
"what does "null" mean when it is put in a case statement?"
null is when a variable contains nothing.
So if the button was never pressed we could have our elevator say something to the user to press a button or just re-open the doors