# The if statement

• Introduction

• So far, we have only seen a few simple statements:

• Assignment statement:

 varName = expression; // compute expression and // store the result in varName

• Method invocation:

 objName.methodName( parameter ); // Used to invoke a void type method OR: varName = objName.methodName( parameter ); // Used to invokes a method that returns a value

• We will learn 2 more statements in this chapter:

• The if   statement
• The if-else   statement

• General information on if and if-else

• The if   statement and the if-else   statement are also known as conditional statements

 The conditional statements lets a computer program carry out different actions depending on a condition

IMPORTANT

• Syntax of the if statement

• The syntax structure (the way to tell the computer) of the if   statement is as follows:

 if ( Condition ) ONE-single-statement

• This may be too abstract for now, but the key to understanding it is by working with many examples.

When you have written enough if   statements, I am sure that you will not only memorize this syntax, but can use it effectively.

IMPORTANT

• The meaning of the if   statement is:

 The Condition clause specifies a condition. A condition is very similar to a Mathematical relation, e.g.: x > 0 or x + y < 0 , etc. If the Condition clause is satisfied , then the statement part is executed . But if the Condition clause is NOT satisfied , then the statement part is SKIPPED OVER.

• (1) Example of if statement

• Consider the following program:

 public class If1 { public static void main(String[] args) { int x = -4; // What if we use x = 4 ?? if ( x < 0 ) // if ( Condition ) x = -x; // ONE-single-statement } }

• If x is a negative value (such as -4), then the condition x < 0 is satisfied, and the statement x = -x; is executed.

The statement x = -x; inverts the negative value to a positive value and assigned it to x

• If x is a positive value (such as 4), then the condition x < 0 is NOT satisfied, and the statement x = -x; is NOT executed.

Therefore, the value of x will be unchanged (and remains positive).

• In other words:

No matter what sign the variable x has before the if statement, after the if statement, the value of x is positive (or 0).

• The relational operators in Java

• A relational operator compares 2 values with each other and determines if the given relation is true or false

• These are the relational operator of Java:

Symbol Operation Result
x < y    less than     true if x is less than y, and otherwise false
x <= y    less than or equal     true if x is less than or equal to y, and otherwise false
x > y    greater than     true if x is greater than y, and otherwise false
x >= y    greater than or equal     true if x is greater than or equal to y, and otherwise false
x == y    equal     true if x is equal to y, and otherwise false
x != y    not equal     true if x is not equal to y, and otherwise false

• (2) Example of if statement

• Consider the following modified withdraw() method in BankAccount class:

 public class BankAccount { // Constructors public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } // Methods public void deposit(double amount) { balance = balance + amount; } // ===================================== // NEW // ===================================== public void withdraw(double amount) { if ( amount <= balance ) // Prevents overdraft balance = balance - amount; } public double getBalance() { return(balance); } // Instance field(s) private double balance; }

• With the if   statement inside the withdraw() method, the withdraw() method operates as follows:

• If the amount to withdraw is less than the balance (amount <= balance), then the withdraw can take place, i.e., deduct the withdrawed amount from the balance (balance = balance - amount).

• Otherwise, the withdraw CANNOT take place, i.e., do not deduct the withdrawed amount from the balance (and in reality, we would also not dispense money).

• The following diagram is a "flow chart" (a graphical representation of the direction taken by the program) of the if   statement:

• Follow the arrow for the 2 cases:

• When amount <= balance is true
• and when amount <= balance is false