Java: Switch Statements
A switch statement tests one variable and depending what that variable is it executes one of a certain number of choices.
Read through this example and try to grasp the concept (it will be explained further afterwards):
int RobotStrength = 7;
switch (RobotStrength){
case 1:
System.out.println("The robot is weak. They can only sit down..");
break;
case 2:
System.out.println("The robot can do simple tasks, for example clean up rubbish.");
break;
case 7:
System.out.println("The robot can run around at the park for hours on end");
break;
default:
System.out.println("Your robots strength has not got a case.");
}
Basically if you haven't figured it out, whatever is after the word "case" is the value the variable must be to execute the case.
To end the little case sections we use "break;".
The default case is used if the value given hasn't been specifically assigned to a certain case.
Switch statements are a lot like if statements but are much easier to use if only looking at the value of one variable..
Back to Java

