Conditionals, Expressions and Other Java Operators

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.

As I recall, by some fluke, you had ended up with more muscles than me (*cough* steroids *cough*) and I put a call into my buddy at the Steroid Usage is a No No Board. To determine if you were a liar, they would have to ask you a series of Boolean questions: True or False — did you do steroids? Yes or No — Are you Barry Bonds?

Booleans are black and white; for them, there is no in between. It’s either true or false, yes or no, on or off. We will get into how to use them in programming a bit later on. For now take comfort in the fact that they exist.

Conditional Operators

Sometimes you need to know if more than one criteria has been met, or if one of the two criteria has been met. To do that, use conditional operators.


class IamBetter {


public static void main(String[] args) {

int my_muscles = 10;

int your_muscles = 10;

int my_looks = 10;

int your_looks =10;

if((my_muscle > your_muscle) && (my_looks > your_looks)) System.out.println(“I really AM better than you in every way!”);

In the above example, we are asking the computer if my looks and muscles are better than yours. As you can see by the values, they are. Because both criteria are met, the program will print to the screen: I really AM better than you in every way!

If only one criteria had been met, it would have printed nothing.

Let’s say again, that because of your heavy steroid use and because my Pilates exercise builds lean muscles instead of those stupid looking ones that you have, that you appear to have more muscles than me. It wouldn’t matter; I can make the program say that I am still better than you.


class IamBetter {


public static void main(String[] args) {

int my_muscles = 10;

int your_muscles = 11;

int my_looks = 10;

int your_looks =10;

if((my_muscle > your_muscle) || (my_looks > your_looks)) System.out.println(“I really AM better than you in every way!”);

Now that I have changed the criteria so that only one match will suffice, it will still print that I really AM better than you in every way! Again, if neither criteria was true, it would have printed nothing.

{mospagebreak title=Expressions}

One last thing before we proceed: when it comes to operator precedence, there is a way to change it. Simply use parentheses(). When your program evaluates your statement, it will execute the part of your equation in parentheses() first. Like so…


a = 5 * (1 + 2);

This would result in the value of a being 15, whereas had we written the statement this way:

a = 5 * 1 + 2;

we would have received a result of 7. Because the brackets were not there, the program simply multiplied 5 times 1 (which equals 5), then added 2, for a total of seven. In the previous example, since the (1+2) was in brackets, it added those together first (equaling 3) and then multiplied that number by 5 (for a result of 15).

Statements

There are three types of statements in Java: Selection, Iteration, and Jump. Let’s take a look at each one.

Selection Statements

There are two types of Selection Statements in Java. The first, which we previewed in the prior tutorial, is the If Statement. In simple terms, it states that if this happens, do that.


int james = 10;

int you = 2;


if (james > you) you = 0;

else james = 0;

See what happens there? If the value of James is greater than the value of You, then your value becomes 0. If your value was greater than mine, my value would become 0.

Sometimes you need to do more than one if statement within an if statement. Consider the code below:


int james = 10;

int you = 2;

int your_daddy =3;


if (james > You) {

if (james > your_daddy) your_daddy = 0;

else james = 0; //refers to the closest if

}

else james = 0; //refers to the original if

In the code above, the program asks the question: Is James better than You? If so, it goes to the next if statement and asks if James is better than your daddy. If he is, it sets your daddy’s value at 0. If not, it sets James value at 0.

If James value had been less than yours, it would have skipped the comparison to your father, and changed James value to 0.

{mospagebreak title=Climbing the Old If Else If Ladder}

Since the beginning of time man has been asking these important questions: Can a homo sapien beat up a homo erectus? Can a man beat a saber-toothed tiger? Can a saber-toothed tiger defeat a giant sloth? Will Jessie Jackson ever become president? Did Bill Clinton ever have relations with that woman? 

When you have a lot of Ifs, you need to build yourself a good old fashioned If Else If statement. Look at the beautiful and pertinent one below:


class SuperHero {

public static void main(String[]) {

int hulk = 9;

int daredevil = 10;

int batman = 11;

int wolverine = 8;

String winner = “Whoever”

 

 

if(daredevil > hulk && daredevil > batman && daredevil > wolverine)

winner = “Daredevil is the winner!”;

else if(hulk > daredevil && hulk > batman && hulk > wolverine)

winner = “The Incredible Hulk is the winner!”;

else if(batman > daredevil && batman > hulk && batman > wolverine)

winner = “Batman is the winner!!”;

else if(wolverine > hulk && wolverine > daredevil && wolverine > batman)

winner = “Wolverine is the winner!”;


else winner = “Nobody wins!”;


System.out.println(winner);

 

}

}

The above code is for a battle royal amongst the super heroically inclined. Each hero is assigned a numeric value and then compared to each other hero. Whichever hero has the highest value gets stored in the winner category. In this case, the winner is Batman, and the following prints to the screen:

  Batman is the winner!

If no one met the criteria (had a higher value than the other three super heroes), then the following would have been printed to the screen:

  Nobody wins!

{mospagebreak title=The Switch Statement}

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…

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

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort