In programming, we might want the computer to do something only in certain
cases. Up until now, code runs no matter what. What if we established
conditions that have to be met in order for code to run? This is what an
if
statement does.
In its simplest form, if
statements are organized in the following manner:
if (<condition>) {
<statement>;
<statement>;
}
If the condition holds true, then the statements within the body will run. Otherwise, we skip over the body entirely and move on to the next line of code.
What's a condition? It's simply an expression that evaluates to either true or
false. For instance, 3 * 4
would not be a valid condition, but x == 4
would
be one. When in doubt, think whether this expression could be used to solve a
yes or no question.
Recall that there is often times ambiguity between using =
and ==
. The =
operator is used for assignment of a new value. For instance, when you do:
int diff = x - y;
you are assigning a new value to the diff
variable. While it's true the
variables are equal during this line, you could later in your program change
the value of diff
to something else. So =
doesn't necessarily mean the left
and right side are the same.
The way you check for equality of the two sides is by using the ==
operator.
If you say diff == 5
, then this will check whether diff
is equal to 5
without assigning the value 5 to it.
For if
statements' conditions, we will almost always be using ==
inside the
condition. Only in advanced code will we be using =
inside the condition, but
that is mostly beyond the scope of CS 312, especially at this point.
We can add some complexity to our if
statement to do things if the condition
actually didn't hold true. We call this companion statement an else
statement. It must always be used in conjunction with an if
statement. In
other words, an else
statement can't exist alone; it must always follow an
if
statement.
if (<condition>) {
<statement>;
<statement>;
} else {
<statement>;
<statement>;
}
An if
statement only allows us to check against one condition. An else
statement will run if that if
statement doesn't hold true. However, if we want
to have several different conditions, each with their own bodies, then we should
use an else if
statement. They always come after if
and always before
else
.
if (<condition 1>) {
<statement>;
<statement>;
} else if (<condition 2>) {
<statement>;
<statement>;
} else if (<condition 3>) {
<statement>;
<statement>;
} else {
<statement>;
<statement>;
}
In this compound if
block, which consists of if
, else if
, and else
statements, <condition 1>
is checked first. If that is met, then we run the
contents of the body of the if
statement and we don't check any more
conditions. Instead, we move past everything. If it's not met, we move on and
check against condition 2
, our next condition. If that's met, we run the
body of the first else if
statement, and if not, we move on. We repeat this
until we run out of else if
conditions to check against. If none of the
conditions are met, then we will run the body of the else
statement.
It's possible to have a compound if
statement with an if
and else if
(s),
but not have an else
. In that case, the conditions will still be checked,
but once we reach the end of the else if
s, we have nothing else to do, so
we end up doing nothing for this entire compound if
block.
Let's say we want to output what letter grade you have, given a certain numerical grade. We can do this:
int numGrade = 99;
if (numGrade >= 90) {
System.out.println("A");
} else if (numGrade >= 80 && numGrade < 90) {
System.out.println("B");
} else if (numGrade >= 70 && numGrade < 80) {
System.out.println("C");
} else if (numGrade >= 60 && numGrade < 70) {
System.out.println("D");
} else {
System.out.println("F");
}
In this case, we'll print out "A", since 99 >= 90
, clearly.
You might notice we're using &&
in our conditions. This means "and". It's used
to denote we want both of these expressions to evaluate as true if we want the
entire statement to evaluate to true as well. Otherwise, if both are false or
even if one of them is false, the whole condition becomes false.
Notice that we check the conditions sequentially. Therefore, if we know that
numGrade >= 90
is false, it will always be the case that by the time we reach
numGrade >= 80 && numGrade < 90
, numGrade
will always be less than 90.
Therefore, we can logically say that we don't need to include && numGrade < 90
because it's implied. We can therefore revise our program to be:
int numGrade = 99;
if (numGrade >= 90) {
System.out.println("A");
} else if (numGrade >= 80) {
System.out.println("B");
} else if (numGrade >= 70) {
System.out.println("C");
} else if (numGrade >= 60) {
System.out.println("D");
} else {
System.out.println("F");
}
and it will output to be the same thing.
Back to top© 2019–2024 Jeffrey Wang. All rights reserved.
In loving memory of Haskell, 19??-2001.