Java Programming
Selection of Actions

The If Statement
Just as you have experienced with Karel's language, it is standard for the flow of program execution to go sequentially from one statement to the next, unless you divert the flow using a flow-of-control construct. The simplest such construct in Java is the if statement, which reads like "if something is true, then do this before continuing." It is written:

    if (<Boolean expression>) {
        <Embedded statements>;
    }
    

where the Boolean expression and the Embedded statements are blanks to be filled in. (We will continue this practice of putting placeholders between angle brackets in an italic typeface. The angle brackets are part of the placeholders, and not part of Java syntax.) A boolean expression represents a question that the programmer wants to ask. The embedded statements represent any number of statements that the programmer wants carried out if the answer to the question is yes.

What the blooming "boolean"?
In algebra you've used equalities and inequalities, which are statements that are true for some set of points. You've graphed the points where the equality or inequality condition holds when substituted into the statement. In Java, things are simpler because, as you know, Java doesn't do algebra . Java just substitutes the current values of the variables into the condition, which is then either true or false (like testing a single point on the graph). This kind of comparison operation is called a relational operation, because it has to do with how one value relates to another, and is also one of the things called a boolean condition. The relational operators in Java are: equal to (==), less than (<), greater than (>), not equal to (!=), greater than or equal to (>=), and less than or equal to (<=).

The other kind of operators that make up boolean expressions allow you to combine two boolean expressions with logical connectives, called logical operators. When you want the result to be true if the values of both expressions are true, then use the logical and (&&). When you want the result to be true if the value of either expression is true, then use the logical or (||). To invert the truth-value of an expression requires the use of the logical not (!).

Example
Consider the code sample below.

public class EuropeanUnion {
public int animal;    public int feed, madcow;     public euroUK (){    feed = 4;       if (animal == 5) {         feed = 7;       }       madCow = 3;    } }

Let's take this apart. Assume that animal has already been given a value. After setting feed to 4, we ask the question "Is the current value of animal equal to 5?" (Note: We use the relational operator "==", not the assignment operator "=".) If that condition is currently true, we then assign feed the value of 7, replacing the old value (4 in this case). We then go on to set madCow to 3. If animal happened to not be equal to 5, we would have just gone ahead to set madCow to 3, leaving feed alone (it would continue to hold 4).

The If/Else Statement
The if statement presented above makes a choice to either execute the embedded statements, or not, based entirely on whether the condition is true or false. When the condition is false, the program would simply continue on to whatever statements appear sequentially below the if. There is another form of the if construct that adds an else part and reads like "If something is true, then do this before continuing. Otherwise, if it's not true, do this other thing before continuing." It is written:

    if (<Boolean expression>){ 
        <then-statements>;
    }
    else {
        <else-statements>;
    }

where <Boolean expression>, <then-statements>, and <else-statements> are placeholders to be filled in. As before, the first placeholder represents a question that the programmer wants to ask, and the second and third placeholders are any number of statements the programmer wants carried out only if the answer to the question has the corresponding value (true for the then-statements and false for the else-statements).

Technical Note: In Java, when there is only one statement that needs to be executed when the boolean condition is true (or false) it is possible to write an if statement without the curly braces ({ }). This is the first of many instances of the principle "Just because you can doesn't mean you should." We strongly recommend always using the curly braces, because otherwise you may introduce bugs into your code if you ever modify the if and forget that the braces are not there.

Exercise 1:  Modifying the implementation of the euroUK method using an if/then/else statement. 

Project 1:  High School tutoring application: Finding the real roots of a quadratic, if they exist. 

Selection of Actions