Java Statements

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.

As a bratty kid, do you remember you parents ever saying, “Don’t make me repeat myself!” Their faces would be all red and sweaty and their eyes would spin in circles. Usually they would be pointing one enormous finger at you and you could tell by the look on their faces that they were dead serious and you had best do what they say.

Then do you remember that about five minutes later, you would completely forget what they said and suddenly they would run around the house ramming their heads into the wall and you would think: Man, my parents are crazy.

You were what Java refers to as an Iteration Statement. That’s right — a loop. You would loop that same bad behavior over every five minutes. And you even had different types of loops. Maybe you would keep asking questions, endlessly, until your parents shouted “SHUT UP!” And you probably would ask for a piece of candy and while your mom kept saying okay, you would grab more and more. Those are examples of loops.

And when your parents, after hours of struggling to be patient, finally sent you to your room without any supper, that was a break statement. Or a way to stop that cursed loop.

As you may recall, in our last article we left off discussing Selection Statements. Now, we are going to cover our other two types of statements, the Iteration and the Jump.

{mospagebreak title=While Loop}

The first iteration we will look at is called the While Loop. It is your standard loop that can best be described as: while this is true, keep doing this.


class CountDown {

public static void main(String args[]) {

int timer = 10; // Sets initial value of timer to 10


while(timer > 0) { // Says do this while timer > 0

System.out.println(“T minus “ + timer);

timer–; // Decrements timer by -1 each loop

}

}

}

The above code is a timer, exactly like the one used by NASA. If you ran the program, the following would print to your screen:

  T minus 10

  T minus 9

  T minus 8

  T minus 7

  T minus 6

  T minus 5

  T minus 4

  T minus 3

  T minus 2

  T minus 1

The code gives the variable timer an initial value of ten and then as it passes through each loop, it decrements the timer’s value by one, prints the string “T minus" and appends the current value of timer to it. It does this until the value of timer is no longer greater than 0.

Just as a side note, if the value of timer was not greater than 0 to begin with, the program would have skipped the loop altogether.

{mospagebreak title=Do-While}

Part of being a bratty kid is testing your limits. How many times can I get a cookie out of the cookie jar before my parents notice; how many times can I put the dog in a pillow case and swing it in the air before it bites me?

As I said previously, if the value of the timer was not greater than 0, the program would have skipped the loop entirely. Sometimes however, we want the program to execute the loop at least once. For that, we use the Do-While.


Class CountDown {

public static void main(String args[]) {

int timer = 10;


do {

System.out.println(“T minus “ + timer);

timer–;

} while(timer > 0);

}

}

The sample above will once again result in the following:

  T minus 10

  T minus 9

  T minus 8

  T minus 7

  T minus 6

  T minus 5

  T minus 4

  T minus 3

  T minus 2

  T minus 1

The difference however is that even if timer had not been greater than 0, it still would have printed the following:

  T minus 10

Let’s make a program where you get to select your own punishment for being bad.


class YourBeating {

public static void main(String args[])

char punishment;


do {

System.out.println(“You’ve been very bad.”);

System.out.println(“Please select your punishment:”);

System.out.println(“1: A firm talking to.”);

System.out.println(“2: A spanking.”);

System.out.println(“3: Forced to watch the View all day.”);

System.out.println(“4: Be pitied by the poet Mr. T.n”);

favorite = (char) System.in.read();

} while( favorite < ‘1’ || favorite > ‘4’);


System.out.println(“n”);


switch(favorite) {

case ‘1’:

System.out.println(“Prepare to be bored! Let the talking commence!”);

break;

case ‘2’:

System.out.println(“I’ll spank you so hard your He-Man underoos will turn into Strawberry Shortcake!”);

break;

case: ‘3’:

System.out.println(“Forced to watch the View…A worse punishment I could not imagine.”);

break;

case ‘4’:

System.out.println(“Mr. T has pitied your greatly!”);

break;


}

}

}

The above code lists a bunch of possible punishments (four in total). If the user presses any key other than 1,2,3, or 4, the program will simply loop again, giving them their four options. If they chose say punishment four, the following would print out to the screen:

  Mr. T has pitied you greatly!

{mospagebreak title=For Loop}

The For Loop allows you to condense the code used in While loops. If we wanted to rewrite our NASA launch code with a For loop, it would look something like this (hit it!):

(Note: the writer of this article has listened to the Beastie Boys one too many times)


class LaunchCode {

public static void main(String args[]) {

int timer;


for(timer = 10; timer > 0; timer—)

System.out.println(“T minus “ + timer);

}

}

This would result in the same countdown as our previous NASA program. As a side note, you could technically initialize the timer variable inside the For loop. If you had done so, you would not be able to call it outside of the loop. Placing the variable outside the loop allows the variable to be called again later on.

Jump Statements

There are three types of Jump statements in Java: the break, the continue, and the return. We have already used the break statement in some of our previous code. It’s function is pretty simple: it terminates a statement, exits a loop, or can be used as a goto statement. Since we have already used it several times, I won’t discuss it any further. 

{mospagebreak title=To Be Continued…}

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…

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye