Lesson 4: The If Statement
Now that we can define data and move it around, how do we check what it is? The if
statement is the most common way to do that.
Let's break down a common if
statement:
This checks if foo
's value is equal to 9, if it is then it prints the value of bar
. If we ran this program, it would output nothing, because the statement foo == 9
evaluates to false
when int foo = 3;
.
A very important thing to understand here is that the if
statement only takes a boolean
. If we tried to do if (3)
we would get an error, because 3 is not a boolean
. (If you recall, a boolean is a data type that can only be true or false).
There are a few operators that compare values and "return" a boolean (they are not methods though). Here's a table of them (they should look familiar):
Operator
Name
==
Equals
!=
Not equals
>
or <
Greater/less than
>=
or <=
Greater/less than or equal to
Here's a few examples of these:
There are also two conditional operators that compare booleans:
Operator
Name
Example
&&
Conditional AND
true && true
evaluates to true
❘❘
Conditional OR
true && false
evaluates to false
Also important to note is the logical compliment operator: !
. It just turns a true value to false. For example:
This example would output foo is false! because !false
is true
.
Else and Else If
What if you want one if statement to depend on the value of another?
You could do the following if you want the second statement to get run only if the first statement doesn't run:
The above works, but it's much more elegant to use the else if
statement:
Or, if you don't care that bar == 3
, just that the previous statement didn't run, you can use the else
statement:
Notice that else
and else if
must immediately follow the end of an if
statement body.
Equality Caveats
An important thing to note is that the equality operator (==
) compares primitive types' values, but it does not compare the value of non-primitive types, it compares their pointer (memory address) instead.
Because of this, you shouldn't compare non-primitive types using ==
if you want to compare their values.
There are 8 primitive types, and they all start with lower case letters: int
, double
, boolean
, short
, long
, float
, char
, and byte
. The only non-primitive we've covered is String
.
It is for that reason that the following does not print anything:
This is because String
is not a primitive, and we're not comparing the values of those two strings. Instead, you should call a method on those strings: equals
. For example, calling foo.equals("foo");"
would return true. Please note that not all objects have an equals
method.
If we want to make the earlier example work, we would do the following:
Conclusion
Go ahead and do the following PracticeIt problems:
Last updated