Conditions

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Control Stuctures


Introduction

Conditions are statements with a true or a false (better known as boolean) result. They use operators to make comparisons. In this section we will cover the different conditions that may be used along with if-statements and how they work.

Using Logical and Relative Operators to Join Conditions in a Single Statement

Recall from chapter 2 the logical operators AND, OR, and NOT. The AND is represented in code as &&. The OR is represented as ||. Lastly, the NOT is represented as !. You could nest if-statements, but this is messy and not considered to be good practice. Instead of nesting the statements, a logical operator can be used to combine condititions in a single statement such as if (x < y && !z), which reads: if x is less than y and not z(meaning z is not true). Logical operators are a powerful tool that can help simplify your work, making your code easier to debug (while also doing the marker a favour). Using logical operators is also less work for you: contrast writing a block of five nested if-statements to writing a single if-statement containing logical operators.

The following truth tables are review from chapter 2, they help explain how the logical operators work. In the truth table below, if the X condition is true AND the Y condition is true, then the result of the statement is true. However, if X is false, Y is false, or they are both false, then the result of the statement is false.

Truth Tables

Truth table for AND
X Y Result
true true true
true false false
false true false
false false false

Truth table for OR
X Y Result
true true true
true false true
false true true
false false false

Truth table for NOT
X Result
true false
false true


If you have not noticed already, the NOT operators work slightly different than AND and OR operators. The NOT operator works with a single condition statement, rather than two statements. If the boolean result of statement X is true, then a NOT will change the statement to false. Conversely, using NOT on a false statement will return true. Also note that the NOT operator is put in front of a condition statement. Some examples below show how this is done.

 int i = 1;

if (!(i > 0)) // i > 0 is true, but the ! reverses so the result is false
...

if (!i > 0) // Illegal statement - ! can ONLY be used for boolean value. i is an integer.
...

if (!true == !true) // !true is false, and false == false, so the result is true
... 

Relational Operators

Relational operators are nothing new. They were also cover in chapter 2. Relational operators are part of what makes up a conditional statement. They will determine whether a condition will be true or false. Below is a list of the different relational operators similar to that found in chapter 2's Relational Operators section.


Operator True if...
> Left operand is greater than right operand
>= Left operand is greater than or equal to right operand
< Left operand is less than right operand
<= Left operand is less than or equal to right operand
== Left operand is equal to right operand
!= Left operand is not equal to right operand


The operands are the numbers that are being compared. Integers are the only operands you should be comparing with these methods. You can compare different types of operands but it is not recommended like this. Examples were given in chapter 2.


Comparing Strings

Strings are a very large subject so we will only touch the basics about comparing Strings in this section. Relational operators such as == will work to an extent when comparing Strings. However, when using the == to compare Strings, it will compare them by reference instead of by their value. While we will not go into more detail of what that means exactly, just know there are certain instances, such as joining two Strings into a single String and then comparing it to another String, where you will not get the result you thought you would if you are using any of the relational operators.

There is a safer way to compare Strings. The Java String API contains dozens of useful methods, we will use the "equals" in our example. The "equals" method is a great way to compare the values between two Strings. Here is an example of what we said would not give the correct result if == was used, using the "equals" method for comparison.

 String str1 = "hello";
String str2 = "he";
String str3 = "llo";
str2 = str2 + str3; // The value is now "hello".

if (str1.equals(str2))
  System.out.println("The Strings are equal");
else
  System.out.println("The Strings are not equal"); 

The program will print out "The Strings are equal". The "equals" method takes the value of "str1" and compares it to the value of "str2". Since the value of both are "hello", they are considered equal, and true will be the result of the condition.

Note that the "equals" method is case sensitive. Therefore, "HELLO" and "hello" are not considered to be equal. However, the Java String API has an "equalsIgnoreCase" method which would consider the Strings to be equal.

That concludes our introduction for comparing Strings. Just be aware that there are other ways to compare Strings, but most of them are beyond the scope of this class.

Summary

In this section you learned how to use both logical and relative operators in order to create complex conditions for your if-statements, as well as the proper way to compare Strings. However, this isn't the only way to deal with multiple conditions within an if-statement. The next section, Nesting, will teach you another technique that can be used to handle multiple conditions.

Previous Page: The If-Else Statement Next Page: Nesting