Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch Statement. Nested IF-ELSE Statement if (month == 1) { mString="January"; } else if (month == 2) { mString=“February”; } else if (month == 3) {

Similar presentations


Presentation on theme: "Switch Statement. Nested IF-ELSE Statement if (month == 1) { mString="January"; } else if (month == 2) { mString=“February”; } else if (month == 3) {"— Presentation transcript:

1 Switch Statement

2 Nested IF-ELSE Statement if (month == 1) { mString="January"; } else if (month == 2) { mString=“February”; } else if (month == 3) { mString=“March”; } else if (month == 4) { mString=“April” } else mString=“Some other month”; This statement is only checking for DISCRETE values (not upper and lower limits) If this is what you need to do, you can use another kind of statement which may be more easier to understand and/or use

3 Switch Statement General form switch (checkVariable) { case 0: commands break; case 1: commands break; case 2: commands break; default: command } the variable you’re checking Opening Brace to start switch statement if the checkVariable equals 0 then do the following commands this tells Java to get out of switch statement if none of the cases exist do these default commands Closing brace to end switch statement

4 Example switch (month) { case 1: mString="January"; break; case 2: mString=“February”; break; case 3: mString=“March”; break; case 4: mString=“April” break; default: mString=“Some other month”; }


Download ppt "Switch Statement. Nested IF-ELSE Statement if (month == 1) { mString="January"; } else if (month == 2) { mString=“February”; } else if (month == 3) {"

Similar presentations


Ads by Google