Home arrow Java & J2EE arrow Page 5 - Java Statements

To Be Continued... - Java

In our last article, we finished our discussion of Java operators, and started to take a look at statements. In this article, we'll continue explaining Java statements. Statements aren't exactly complicated once you grasp the concept. Indeed, many statements have counterparts -- of a sort -- in real life.

TABLE OF CONTENTS:
  1. Java Statements
  2. While Loop
  3. Do-While
  4. For Loop
  5. To Be Continued...
By: James Payne
Rating: starstarstarstarstar / 13
October 24, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

No, it's not the end of the article just yet (grumble grumble). The Continue statement allows you to force an early iteration of the loop. Basically it runs a portion of the loop without executing the rest of the code in the body. Here is a sample:


class Counter {

public static void main(String args[]) {

for(int timer = 0; timer < 10; timer ++) {

System.out.print(timer + “ “);

if (i%5 == 0) continue;

System.out.println.(“”);

}

}

}

The above code will print five numbers on a line up to the number 10 like so:

  0 1 2 3 4 5

  6 7 8 9

The Return of the Jedi

I just wanted to throw that in there. The Return statement is used when you wish to return from a method (we will discuss methods later on). Here is how that looks in code:


class ChickenEgg {

public static void main(String args[]) {

boolean t = true;

System.out.println(“The chicken!”);


if(t) return;


System.out.println(“The egg!”);


}

}

The above code will print out: The Chicken! as it forces a return from the method prior to it, executing the second System.out.println command, thus solving the age old question: what came first, the chicken or the egg?

Incidentally, neither of those is right. The correct answer is a pre-evolved version of the chicken.

Well that's it for this tutorial. Be sure to join me for the next article, when I will help teach you to become a true Java ninja.

Till then...



 
 
>>> 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: