When you code an if statement within another if statement as in the following?

If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

You’ve seen that values have different types. A type tells the computer what kind of data a value is, or what type of value a variable holds. So far you’ve mostly worked with the

float score = 95;
boolean isGradeA = score >= 90;
1 type.

This tutorial introduces the

float score = 95;
boolean isGradeA = score >= 90;
2 type, which can only hold two possible values:
float score = 95;
boolean isGradeA = score >= 90;
3 or
float score = 95;
boolean isGradeA = score >= 90;
4. This tutorial also introduces if statements, which allow you to perform different actions depending on the value of a boolean.

Remember that a type tells the computer what kind of value a variable will hold. For example, the

float score = 95;
boolean isGradeA = score >= 90;
5 type holds whole numbers, the
float score = 95;
boolean isGradeA = score >= 90;
1 type holds decimal numbers, and the
float score = 95;
boolean isGradeA = score >= 90;
7 type holds text. You create a variable by giving it a type, a name, and then a value:

int catLives = 9;
float accountBalance = -123.45;
String message = "Happy Coding!";

You can create a

float score = 95;
boolean isGradeA = score >= 90;
2 variable the same way, but it can only hold two possible values:
float score = 95;
boolean isGradeA = score >= 90;
3 or
float score = 95;
boolean isGradeA = score >= 90;
4.

This might not seem very useful yet, but it will become more handy in a couple paragraphs. Keep reading!

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
1 variables wouldn’t be very useful if you had to decide their values ahead of time. Instead, you can obtain
float score = 95;
boolean isGradeA = score >= 90;
2 values through relational operators. You might have called these inequalities in algebra class, where you compared two sides with symbols like
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
3 less than,
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
4 greater than, or
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
5 equal to. Here’s an example:

float score = 95;
boolean isGradeA = score >= 90;

This code creates a

float score = 95;
boolean isGradeA = score >= 90;
1 variable named
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 and sets it equal to
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
8. It then creates a
float score = 95;
boolean isGradeA = score >= 90;
2 variable named
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
0 and sets it equal to the result of the inequality
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
1. In this case, the inequality is
float score = 95;
boolean isGradeA = score >= 90;
3 because
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
8 is greater than
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
4. So at the end of this code,
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
0 is holding the
float score = 95;
boolean isGradeA = score >= 90;
2 value of
float score = 95;
boolean isGradeA = score >= 90;
3.

If that line of code is confusing, try reading the right side first. First you take the inequality

boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
1 and get a
float score = 95;
boolean isGradeA = score >= 90;
2 value from it (in this case,
float score = 95;
boolean isGradeA = score >= 90;
3), and then you point the
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
0 variable to that value.

Similar to how you can add two

float score = 95;
boolean isGradeA = score >= 90;
1 values using the
boolean sinks = !canSwim;
boolean falls = !canFly;
boolean isTodayWeekday = !isTodayWeekend;
3 operator to get a third
float score = 95;
boolean isGradeA = score >= 90;
1 value, or subtract them using the
boolean sinks = !canSwim;
boolean falls = !canFly;
boolean isTodayWeekday = !isTodayWeekend;
5 operator, you can also operate on two
float score = 95;
boolean isGradeA = score >= 90;
2 values to get a third
float score = 95;
boolean isGradeA = score >= 90;
2 value.

And

You can combine two

float score = 95;
boolean isGradeA = score >= 90;
2 values using the and operator, which looks like two ampersands:
boolean sinks = !canSwim;
boolean falls = !canFly;
boolean isTodayWeekday = !isTodayWeekend;
9. The and operator evaluates to
float score = 95;
boolean isGradeA = score >= 90;
3 whenever the two
float score = 95;
boolean isGradeA = score >= 90;
2 values on either side of it are also
float score = 95;
boolean isGradeA = score >= 90;
3.

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;

The

boolean isMammal = !canSwim && !canFly;
3 variable will only be
float score = 95;
boolean isGradeA = score >= 90;
3 when both
boolean isMammal = !canSwim && !canFly;
5 and
boolean isMammal = !canSwim && !canFly;
6 are also true. If either one of them is false, then
boolean isMammal = !canSwim && !canFly;
3 will also be false.

Again, it might make more sense to read the right side first. First the code evaluates the

boolean sinks = !canSwim;
boolean falls = !canFly;
boolean isTodayWeekday = !isTodayWeekend;
9 operator, which creates a
float score = 95;
boolean isGradeA = score >= 90;
2 value of
float score = 95;
boolean isGradeA = score >= 90;
3. Then it points the
boolean isMammal = !canSwim && !canFly;
3 variable to that value.

Or

The or operator evaluates to true if either of the two

float score = 95;
boolean isGradeA = score >= 90;
2 values on either side of it is
float score = 95;
boolean isGradeA = score >= 90;
3.

To use the or operator, type two pipes

size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
4 (they’re above the enter key, or
size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
5) between two
float score = 95;
boolean isGradeA = score >= 90;
2 values:

boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;

The

size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
7 variables will be
float score = 95;
boolean isGradeA = score >= 90;
3 if either the
size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
9 or
size(200, 100);

float score = 95;

if (score >= 90) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
0 variables are
float score = 95;
boolean isGradeA = score >= 90;
3.

First the code evaluates the

size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
4 operator which creates a
float score = 95;
boolean isGradeA = score >= 90;
2 value (in this case it’s
float score = 95;
boolean isGradeA = score >= 90;
3), and then it points the
size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
7 variable to that value.

Not

In addition to operating on two

float score = 95;
boolean isGradeA = score >= 90;
2 values, you can also calculate the opposite of a single
float score = 95;
boolean isGradeA = score >= 90;
2 value. The opposite of
float score = 95;
boolean isGradeA = score >= 90;
3 is
float score = 95;
boolean isGradeA = score >= 90;
4, and the opposite of
float score = 95;
boolean isGradeA = score >= 90;
4 is
float score = 95;
boolean isGradeA = score >= 90;
3.

This is called the not operator, and you use it by typing an exclamation point

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else {
  background(255, 0, 0);
  text("Study more!", 50, 55);
}
2 before the value you want to switch.

boolean sinks = !canSwim;
boolean falls = !canFly;
boolean isTodayWeekday = !isTodayWeekend;

Each line of this code evaluates the

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else {
  background(255, 0, 0);
  text("Study more!", 50, 55);
}
2 operator which creates a new
float score = 95;
boolean isGradeA = score >= 90;
2 value based on the opposite of whatever follows it, and then it points a
float score = 95;
boolean isGradeA = score >= 90;
2 variable to that value.

Combining Operators

You can also combine these operators to form more complicated logic. So you can do things like this:

boolean isMammal = !canSwim && !canFly;

First this code takes the opposite of the

boolean isMammal = !canSwim && !canFly;
5 and
boolean isMammal = !canSwim && !canFly;
6 variables to create two new
float score = 95;
boolean isGradeA = score >= 90;
2 values. It then takes those values and feeds them into the
boolean sinks = !canSwim;
boolean falls = !canFly;
boolean isTodayWeekday = !isTodayWeekend;
9 operator, which creates yet another
float score = 95;
boolean isGradeA = score >= 90;
2 value. Then it points the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
1 variable to that value.

(If this bothers you because it doesn’t account for animals like bats, beavers, and dolphins… check out the homework!)

There is a whole field of study devoted to

float score = 95;
boolean isGradeA = score >= 90;
2 logic, so check that out if it sounds interesting. But for now, keep in mind that
float score = 95;
boolean isGradeA = score >= 90;
2 values contain
float score = 95;
boolean isGradeA = score >= 90;
3 or
float score = 95;
boolean isGradeA = score >= 90;
4, and you can use operators like
boolean sinks = !canSwim;
boolean falls = !canFly;
boolean isTodayWeekday = !isTodayWeekend;
9,
size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}
4, and
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else {
  background(255, 0, 0);
  text("Study more!", 50, 55);
}
2 on them.

An

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement checks a
float score = 95;
boolean isGradeA = score >= 90;
2 value and only executes a block of code if that value is
float score = 95;
boolean isGradeA = score >= 90;
3.

To write an

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement, write the keyword
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9, then inside parentheses
float score = 95;
boolean isGradeA = score >= 90;
04 insert a
float score = 95;
boolean isGradeA = score >= 90;
2 value, and then in curly brackets
float score = 95;
boolean isGradeA = score >= 90;
06 write the code that should only execute when that value is
float score = 95;
boolean isGradeA = score >= 90;
3. That code is called the body of the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement.

Here’s an example that draws a congratulations message, but only if your grade is an A:

size(200, 100);

float score = 95;
boolean isGradeA = score >= 90;

if (isGradeA) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}

This code uses an

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement to check whether
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
0 is
float score = 95;
boolean isGradeA = score >= 90;
3, and if it is, it draws a green background, changes the fill color to black and the text size to
float score = 95;
boolean isGradeA = score >= 90;
12, and then writes
float score = 95;
boolean isGradeA = score >= 90;
13 on the screen.

Since

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 is
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
8 (and
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
8 is greater than
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
4), then you see the congratulations message:

When you code an if statement within another if statement as in the following?

If

boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
0 is
float score = 95;
boolean isGradeA = score >= 90;
4 (if
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 is less than
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
4), then the program doesn’t do anything. Try changing
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 to
float score = 95;
boolean isGradeA = score >= 90;
23, and you’ll see a blank window:

When you code an if statement within another if statement as in the following?

Since

boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
0 is now
float score = 95;
boolean isGradeA = score >= 90;
4, the body of the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement is not executed, and it skips over all of the code inside the body.

Boolean Expressions

So far, all of the examples have separated the inequality and the

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement into two steps: the code first created a
float score = 95;
boolean isGradeA = score >= 90;
2 variable from an inequality, and then it used that variable in an
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement. But you can combine them into a single step:

size(200, 100);

float score = 95;

if (score >= 90) {
  background(0, 255, 0);
  fill(0);
  textSize(18);
  text("Congratulations!", 30, 55);
}

This does the exact same thing as the old code, except now the inequality (which evaluates to a

float score = 95;
boolean isGradeA = score >= 90;
2 value) is inside the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement instead of being split into its own step. Either approach is fine, so you should use whichever style makes the most sense to you.

An

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement executes some code if its
float score = 95;
boolean isGradeA = score >= 90;
2 value is
float score = 95;
boolean isGradeA = score >= 90;
3, and it skips that code if the
float score = 95;
boolean isGradeA = score >= 90;
2 value is
float score = 95;
boolean isGradeA = score >= 90;
4. But what if you want to do one thing if the value is
float score = 95;
boolean isGradeA = score >= 90;
3 and a different thing if it’s
float score = 95;
boolean isGradeA = score >= 90;
4? Sounds like a job for an
float score = 95;
boolean isGradeA = score >= 90;
39 statement!

To use an

float score = 95;
boolean isGradeA = score >= 90;
39 statement, type the
float score = 95;
boolean isGradeA = score >= 90;
39 keyword after an
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement, and then inside curly brackets
float score = 95;
boolean isGradeA = score >= 90;
06 put the code you want to execute when the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement’s
float score = 95;
boolean isGradeA = score >= 90;
2 evaluates to
float score = 95;
boolean isGradeA = score >= 90;
4:

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else {
  background(255, 0, 0);
  text("Study more!", 50, 55);
}

This code uses an

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement to check whether
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 is greater than or equal to
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
4. Since
float score = 95;
boolean isGradeA = score >= 90;
23 is less than
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
4, that inequality is
float score = 95;
boolean isGradeA = score >= 90;
4, so the code inside the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement is skipped. Instead, the program jumps to the code inside the body of the
float score = 95;
boolean isGradeA = score >= 90;
39 statement, which draws a red background and displays the “Study more!” message.

When you code an if statement within another if statement as in the following?

You can think about the code like this: “If the score is greater than or equal to 90, then display the ‘Congratulations!’ message. Otherwise, display the ‘Study more!’ message instead.”

An

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement executes some code if its
float score = 95;
boolean isGradeA = score >= 90;
2 evaluates to
float score = 95;
boolean isGradeA = score >= 90;
3, and an
float score = 95;
boolean isGradeA = score >= 90;
39 statement executes code if it evaluates to
float score = 95;
boolean isGradeA = score >= 90;
4. But what if you want to take different actions depending on multiple cases? This is where
float score = 95;
boolean isGradeA = score >= 90;
60 statements come in handy.

An

float score = 95;
boolean isGradeA = score >= 90;
60 statement is like a mix between an
float score = 95;
boolean isGradeA = score >= 90;
39 statement and an
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement. You put an
float score = 95;
boolean isGradeA = score >= 90;
60 statement after an
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement, and if the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement evaluates to
float score = 95;
boolean isGradeA = score >= 90;
4, then the
float score = 95;
boolean isGradeA = score >= 90;
60 statement’s
float score = 95;
boolean isGradeA = score >= 90;
2 is evaluated:

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}

This code uses an

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement to check whether
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 is greater than or equal to
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
4. Since
float score = 95;
boolean isGradeA = score >= 90;
23 is less than
boolean isTodaySaturday = true;
boolean isTodaySunday = false;
boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
4, that inequality is
float score = 95;
boolean isGradeA = score >= 90;
4, so the code inside the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement is skipped. The program jumps down to the
float score = 95;
boolean isGradeA = score >= 90;
60 statement and checks the
float score = 95;
boolean isGradeA = score >= 90;
2 expression inside the
float score = 95;
boolean isGradeA = score >= 90;
60 statement. Since
float score = 95;
boolean isGradeA = score >= 90;
23 is greater than
float score = 95;
boolean isGradeA = score >= 90;
81, the inequality is
float score = 95;
boolean isGradeA = score >= 90;
3, and the code inside the body of the
float score = 95;
boolean isGradeA = score >= 90;
60 is executed. The code then draws a blue background and and displays the “Good job!” message.

When you code an if statement within another if statement as in the following?

You can think about the code like this: “If the score is greater than or equal to 90, then display the ‘Congratulations!’ message. Otherwise check whether the score is greater than or equal to 80. If it is, then display the `Good job!’ message instead.”

If

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 was
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
8, then only the code inside the body of the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement would run. The code inside the
float score = 95;
boolean isGradeA = score >= 90;
60 statement would be skipped. And if
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 was
float score = 95;
boolean isGradeA = score >= 90;
89, then the
float score = 95;
boolean isGradeA = score >= 90;
2 expressions of both the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement and the
float score = 95;
boolean isGradeA = score >= 90;
60 statement would evaluate to
float score = 95;
boolean isGradeA = score >= 90;
4, so neither one of their bodies would be executed.

You can follow an

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement with multiple
float score = 95;
boolean isGradeA = score >= 90;
60 statements, and you can follow an
float score = 95;
boolean isGradeA = score >= 90;
60 statement with an
float score = 95;
boolean isGradeA = score >= 90;
39 statement. So you could expand your program to detect every grade:

float score = 95;
boolean isGradeA = score >= 90;
0

This code uses a series of

size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9,
float score = 95;
boolean isGradeA = score >= 90;
99 and
float score = 95;
boolean isGradeA = score >= 90;
39 statements to run this logic:

  • Is
    boolean isTodaySaturday = true;
    boolean isTodaySunday = false;
    boolean isTodayWeekend = isTodaySaturday || isTodaySunday;
    
    1?
    • If so, show the “Congratulations!” message and stop checking any other conditions.
    • If not, keep checking.
  • Is
    boolean canSwim = true;
    boolean canFly = true;
    boolean isDuck = canSwim && canFly;
    
    02?
    • If so, show the “Good job!” message and stop checking any other conditions.
    • If not, keep checking.
  • Is
    boolean canSwim = true;
    boolean canFly = true;
    boolean isDuck = canSwim && canFly;
    
    03?
    • If so, show the “Just okay.” message and stop checking any other conditions.
    • If not, keep checking.
  • Is
    boolean canSwim = true;
    boolean canFly = true;
    boolean isDuck = canSwim && canFly;
    
    04?
    • If so, show the “Not good!” message and stop checking any other conditions.
    • If not, show the “Study more!” message.

Try changing the

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 variable to see the different messages.

Avoiding Unnecessary Checks

Keep in mind that only one body of code is entered in a series of

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
06 statements. As soon as the code enters one of the
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 or
float score = 95;
boolean isGradeA = score >= 90;
60 statements, it stops checking subsequent
float score = 95;
boolean isGradeA = score >= 90;
60 statements.

Look at this line of code, from the above program:

Notice that the code only checks whether

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
02. But for the grade to be a B, it also has to be
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
11. Why doesn’t the code also check for that?

The code doesn’t check to make sure that

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
12, because it already knows that it is. If
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
7 was
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
14, then the first
size(200, 100);
fill(0);
textSize(18);

float score = 85;

if (score >= 90) {
  background(0, 255, 0);
  text("Congratulations!", 30, 55);
}
else if (score >= 80) {
  background(0, 100, 255);
  text("Good job!", 60, 55);
}
9 statement would have been entered, and the code wouldn’t even reach this line.

Think about it this way: first the code checks whether the grade is an A. If it is, then it displays the “Congratulations!” message and doesn’t check any other grades since it already knows the grade is an A. If it’s not an A, then it knows that

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
12 and it keeps checking other grades.

That’s why the code can check whether the grade is a B by only checking whether

boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
17. It it is, then it displays the “Good job!” message and doesn’t check against any other grades since it already knows the grade is a B. If it’s not a B, then it knows that
boolean canSwim = true;
boolean canFly = true;
boolean isDuck = canSwim && canFly;
18, and it continues that pattern for the rest of the program.

When you code an if statement within another if statement The statements are?

A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.

When you code an if statement within another if statement The statements are quizlet?

Terms in this set (20) When you code an if statement within another if statement, the statements are nested.

When an if statement is nested in the else clause of another statement?

When an if statement is nested in the else clause of another statement, the only time the inner if statement is executed is when the boolean expression of the outer if statement is true. The scope of a variable is limited to the block in which it is defined.

What is the nested if statement?

Nested IF functions, meaning one IF function inside of another, allows you to test multiple criteria and increases the number of possible outcomes. We want to determine a student's grade based on their score. If Bob's score in B2 is greater than or equal to 90, return an A.