Conditionals

If not that, then… ?

An Insightful Process

No worries

This diagram isn't quite right. If you don't think learning to code is difficult, then you miiiight not get it yet.

Conditional Statements

Conditional statements are one of the ways that we control the flow of a program.

We might say…

If I am 16 or older, then I can drive legally.

In Java, we would say:

int age = 42;
if (age >= 16) {
  System.out.println("You are " + age + ", so you are legal to drive.");
}

We might say…

If I get eight or more hours of sleep, I am likely to feel more rested.

In Java, we would say:

int hoursOfSleep = 42;
if (hoursOfSleep >= 8) {
  System.out.println("You are likely to feel more rested.");
}

We can follow one conditional with another.

if (hoursOfSleep == 42) {
  System.out.println("He used 42 again, didn't he?");
 }

Let's Break It Down

An if statement executes if its condition evaluates to true:

int heightInInches = 73;

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride");
}

Sorry, you're too short. :(

int heightInInches = 42;

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride");
}

Syntax

An if statement starts with the keyword if:

int heightInInches = 73;

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride");
}

Syntax

It is followed by parentheses that contain a condition:

int heightInInches = 73;

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride");
}

Syntax

This condition must evaluate to a boolean value:

int heightInInches = 73;

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride");
}

Syntax

This is followed by a code block that executes only if the condition is true. Remember that code blocks open with a left curly bracket ({) and close with a right curly bracket (}):

int heightInInches = 73;

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride");
}

We can skip the curly brackets if we only want to execute one line of code, but that's usually frowned upon since it's easy to mess up. This code is the same:

int heightInInches = 73;

if(heightInInches >= 60) 
  System.out.println("You are tall enough to ride");

Else what?

It's polite to offer an apology, eh? We can be polite.

An if statement allows us to do something else if its condition does not evaluate to true:

int heightInInches = 42;

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride.");
} else {
  System.out.println("I'm sorry, too short.");
}

And then?

We can create sequences of if/else statements:

if(heightInInches >= 60) {
  System.out.println("You are tall enough to ride.");
} else if(heightInInches >= 30) {
  System.out.println("Try the teacups. They look fun.")
} else {
  System.out.println("I'm sorry, too short.");
}

Think of this as connecting multiple if/else statements.

Swichin' it up!

Sometimes, we can use a switch/case statement to replace several if/else statements. Let's say we are categorizing our amusement park patrons into "adult", "youngster" (just short people, really), and everyone else.

The teacups are fun, right?

We can use a switch/case statement to execute one or more statements based on a value:

String ageGroup = "youngster";

switch (ageGroup) {
case "adult":
    System.out.println("You can ride the rollercoaster!");
case "youngster":
    System.out.println("The teacups are fun.");
    break;
default:
    System.out.println("Oh, you must be a toddler. Toddle on!");
}

What is displayed if we change ageGroup's value to "adult"? Why?

What is displayed if we change its value to anything other than "adult" or "youngster"? Why?