Decision Statements
The if Statement
So you’ve learned how to create methods that perform certain activities. You’ve even learned how to call those methods at the appropriate times during your program. Now we will learn how to teach your program to decide which methods to call at which times. In any programming language, you will find decision statements are a necessary part of even the simplest programs.
If statements consist of two major parts - a condition and a body.
- Condition -
A condition statement returns a boolean value; that is, it returns either true or false. If and only if the condition is true, Java will execute the statements contained in the body.if (amount <= balance)
- Body -
The body contains any statements that should be executed if the condition is met. If the body of theifstatement consists of multiple statements that must be executed, then these statements must be grouped together to form a block statement by enclosing them in braces { }.{
balance = balance - amount;
System.out.println(”New Balance: ” + balance);
}
Relational Operators
Operators to Compare Two Values
When we covered variables and assignment statements, we learned the assignment operator: =. We will now learn about relational operators, mathematical symbols that test the relationship between two values.
- Greater than
( > )
if (a > 5) // if a is greater than 5
- Greater than or equal to
( >= )
if (a >= 5) // if a is greater than or equal to 5
- Less than
( < )
if (a < 5) // if a is less than 5
- Less than or equal to
( <= )
if (a <= 5) // if a is less than or equal to 5
- Equal
( == )
if (a == 5) // if a is equal to 5
- Not equal
( != )
if (a != 5) // if a is not equal to 5
Comparing Strings
While we can use the relational operators for comparing boolean and numerical values, we cannot use them for comparing Objects in Java. One commong mistake is trying to use the == operator for comparing two Strings. For example:
// WRONGif (myFirstString == mySecondString)
{
…
}
This has unpredictable nature. While we expect Java to compare the contents of the two Strings, Java only compares the two references. If these two references point to the same Object, it will return true; else, it will return false.
Rather, Java’s Object Class has provided other means for comparing two Objects:
- .equals() - This method is inherent in all String objects and actually allows Java to compare the contents of the Strings rather than just their references.
if (myFirstString.equals(mySecondString))
{
…
} - .compareTo() - This method is inherent in all classes that implement the Comparable interface and actually allows Java to compare two classes together, it returns a NEGATIVE value if Object A is of a lesser value than Object B, ZERO if the two values of Objects A and B are equal, and POSITIVE if Object A is of greater value than Object A.
if (objectA.compareTo(objectB) > 1)
{
…
}
Else and Else If Statements
Because There’s Never Just One Possible Condition
Often times we want to have several different conditions, each allowing for a separate block of code to be executed. Rather than creating several unrelated if statements, Java provides us with a mechanism called else statements. If and else statements go together. If gives one condition; else represents the block of code to be executed if that condition is not met.
Else statement
else
{
…
}
In between these two, we also have else if statements. Else if statements are secondary conditions that are tested if the original condition is not met. The number of additional else if statements is unlimited.
Else if statement
else if (myInteger < 5)
{
…
}
All Together - When strung together, the usual if statement would look a lot like this…
if (myName.equals(”Peter”))
{System.out.println(”Welcome Peter!”)
}else if (myName.equals(”Hiro”))
{
System.out.println(”Welcome Hiro!”)}
else
{
System.out.println(”Error: Unknown User!”)
}
Switch Statements
To Make Life Easier When Working With Integers
Steps and steps of continuous else if blocks can be somewhat tedious to write. Furthermore, it takes up a lot of room in your code. That is why many programming languages like Java come with a feature called switch(). This allows you to take one particular variable and provide certain directions according to specific values it may have. These are called CASES. The switch statement is very helpful at times; however, there is one down-side: it can only be used with integers.
Switch statement
switch(myInteger)
{
case 1:
System.out.println(”one”);
break;
case 2:
System.out.println(”two”);break;
case 3:
System.out.println(”three”);
break;
case 4:
System.out.println(”four”);break;
default:
System.out.println(”error”);
break;
}
Details
- case x: - cases are written as
case x:, where x is the test value of the switch parameter. - break; - all cases must have a break at the end to signal separation between two different cases.
- default: - the default value is similar to the
elsestatement; it signifies what to do in case none of the provided cases are true.