© We Can Code IT, LLC
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 are one of the ways that we control the flow of a program.
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.");
}
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?");
}
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");
}
An if
statement starts with the keyword if
:
int heightInInches = 73;
if(heightInInches >= 60) {
System.out.println("You are tall enough to ride");
}
It is followed by parentheses that contain a condition:
int heightInInches = 73;
if(heightInInches >= 60) {
System.out.println("You are tall enough to ride");
}
This condition must evaluate to a boolean
value:
int heightInInches = 73;
if(heightInInches >= 60) {
System.out.println("You are tall enough to ride");
}
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");
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.");
}
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.
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.
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?