Home arrow Java & J2EE arrow Page 4 - Conditionals, Expressions and Other Java Operators

The Switch Statement - Java

In our last article we left off with Operators. Since then a strange time fluctuation has occurred, and that is exactly where we pick up. So keep reading to learn about exotic things like conditionals, expressions, and more.

TABLE OF CONTENTS:
  1. Conditionals, Expressions and Other Java Operators
  2. Expressions
  3. Climbing the Old If Else If Ladder
  4. The Switch Statement
By: James Payne
Rating: starstarstarstarstar / 3
October 23, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Sometimes you feel like some nuts. Sometimes you don't. Sometimes you also don't feel like writing a bunch of If-Elses all over the place. Well slacker, meet my little friend the Switch Statement.


Public static void main(String args[]) {

for(int launch_time = 0; launch_time < 11; launch_time++)

switch(launch_time) {

case 0:

case 1:

case 2:

case 3:

case 4:

System.out.println("Shuttle launch in less than five seconds");

break;

case 5:

case 6:

case 7:

case 8:

case 9:

System.out.println("Shuttle launch in less than ten seconds");

break;

default:

System.out.println("Shuttle launch in less than 20 seconds");

 

}

}

}


The above code (while it technically counts down backwards) will print the following to the screen:

  Shuttle launch in less than five seconds

  Shuttle launch in less than five seconds

  Shuttle launch in less than five seconds

  Shuttle launch in less than five seconds

  Shuttle launch in less than five seconds

  Shuttle launch in less than ten seconds

  Shuttle launch in less than ten seconds

  Shuttle launch in less than ten seconds

  Shuttle launch in less than ten seconds

  Shuttle launch in less than ten seconds

  Shuttle launch in less than 20 seconds

  Shuttle launch in less than 20 seconds

If you had used the If-else statements you would have had to compare the value of launch_time each time to print out the result (ie; is launch_time greater than 1? is launch_time greater than 2, etc).

It is possible to nest Switch statements as well, but that is beyond the scope of this tutorial. In fact, anything more is beyond the scope of this tutorial, as we have come to the end of it.

Thanks for sticking around. In the next tutorial, I will go over the remaining statements (the Iteration and the Jump). So, hope to see you then.

Till next time...



 
 
>>> More Java & J2EE Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: