What You Will Learn |
---|
|
In Jeroo, you learned that an if structure is used to make decisions. These decisions determined how the jeroo interacted with the island environment. For example,
if(bob.isNet(AHEAD)) { bob.toss(); }
allowed the jeroo to make the decision to toss a flower if there was a net directly in front of him.
The Jeroo if structure consisted of two parts: a condition and a body.
if() {condition}body
Recall that a condition is an expression that evaluates to either true or false. The body is the code that will be executed if the condition is true.
This same structure is used to form if statements in Java. However, there is one significant difference between a Jeroo if structure and a Java if structure. The condition of a Jeroo if structure is limited to a call to one of the Jeroo sensor methods such as isNet, isWater, isFacing, hasFlower, etc. On the other hand, the condition of a Java if structure does not have this limitation therefore it can contain expressions that include literals, variables, methods, and relational operators.
Here are some examples of Java if statements.
if(age == 18){ System.out.println("I can vote"); }// condition contains a variable and int literal
if(cost > moneyInPocket){ System.out.println("I can't buy it"); }// condition contains two variables
if(answer.equals("A")){ score++; }// condition contains a method call
if(isVowel(letter)){ System.out.println("The letter is a vowel"); }// condition contains a method call
If the body of an if structure contains more than one statement the statements must by enclosed in curly braces { }.
if(amount <= balance) { double newBalance = balance - amount; balance = newBalance; }
If the body contains only one statement the braces are optional. This if structure
if(average == 100) { grade = "A+"; }
can also be written as
if(average == 100) grade = "A+";
Like Jeroo, Java provides an if-else structure. An if-else is used when you want to do one thing if a condition is true and another thing if that condition is false. An if-else will always execute either the if section or the else section, but never both.
if(condition) { body1}// if section{ body2else}// else section
The following is an example of an if-else structure.
if(amount <= balance) { balance = balance - amount; } else { System.out.println("You don't have enough money"); }
If the condition is true, the first assignment statement is executed; if the condition is false, the println statement is executed. Only one or the other will be executed.
Alternatively, the above code could have written like the following.
if(amount <= balance) balance = balance - amount; else System.out.println("You don't have enough money");
Notice the use of indentation to show which section each statement belongs.
The compiler doesn't care where you place braces. Some Java programmers prefer to place braces like the following.
if(amount <= balance) { balance = balance - amount; } else { System.out.println("You don't have enough money"); }
My preference is line the curly braces vertically underneath each like the first example.