Switch Statements

1 / 3
Switch statement code
2 / 3
Example of switch statement
2 / 3
Example of switch statement

Above are some java code samples using switch statements.

Instead of writing many if/else statements, you can use a switch statement. The switch statement will allow a certain block of code to be executed if there is a match with the case. If there is no match to a case the default case will be executed if the default case has been incuded. After executing the code within a case the break keyword is used to stop the execution of more code and case testing.



Quiz

Add space in condition ex. (x > 0) not (x>0).

  1. code example

    What will the output be for this program if caseChoice equals 2?

  2. code example

    What will the output be for this program if caseChoice equals 10?


  3. code example

    What will the output be for this program if caseChoice equals 4.