Conditionals
Let's talk more about decision-making. Say I want to make the following program:
- Give my age
- If it is 18 or above, print a message saying I am an adult
- If it is not 18 or above, print a message saying I am not an adult
It's a simple decision. age >= 18, then do this. Otherwise, do something else.
And to do that? We use if statements.
If Statements
An if statement checks if a condition is true and then performs the code within.
Let's start our program in our main method:
Scanner input = new Scanner(System.in);
System.out.println("How old are you?");
int age = input.nextInt();
Pretty simple so far. We create a Scanner object named input, and receive the age input as an integer. (Make sure you imported Scanner via import java.util.Scanner;
Now, let's create our if statement. Using what we learned about conditions, we can create this conditional. Write this below the nextInt() usage:
if(age >= 18) {
System.out.println("You are an adult!");
}
That's it! We write if, put our condition in (), and use {} to put code inside. If we run the code, enter 20 as the age, you should see You are an adult! printed. Same thing if you enter 18.
But what happens if we enter something below 18? Nothing prints. Let's fix that.
Else Statements
When we look at age >= 18, it's true if age is greater than or equal to 18. But if it's less than 18, then it's false.
We use else statements to consider the false.
After the if statement, we can add an else statement. It trails on:
if(age >= 18) {
System.out.println("You are an adult!");
} else {
System.out.println("You are not an adult!");
}
If you try the code, you will get a different message when age is less than 18.
Let's take it one step further.
Nested Conditionals
Let's tell the user if they are a kid or if they are a teenager.
We can do this by nesting conditionals:
if(age >= 18) {
System.out.println("You are an adult!");
} else {
if(age >= 13) {
System.out.println("You are a teenager!");
} else {
System.out.println("You are a kid!");
}
}
Since we nested our conditional within the else statement, everything here assumes that age is greater than or equal to 18.
This means that to be a teenager, you need to be under 18 but over or equal to 13.
And if you aren't a teenager, you are a kid. However, we can take this one step further.
Else If Statements
Using Else If statements, we can simplify what we wrote above and make it all within the first conditional, without needing to nest.
For else statements, we write else. For else if statements, we write else if() with our condition inside the (), like so:
if(age >= 18) {
System.out.println("You are an adult!");
} else if(age >= 13) {
System.out.println("You are a teenager!");
} else {
System.out.println("You are a kid!");
}
Java moves from top to bottom in code. If the first condition is false, it will move to the next condition to see if that one is true, and so on.
However, it keeps in mind what made the above condition false, still following that rule. So to be a teenager, age would still need to be less than 18.