Unit 3 Team Teach - 3.3
Unit 3 Team Teach
3.3 If Else Statements
Purpose of Else Statements
Else statements: Handles what happens when the if condition is false. Structure of If-Else:
- If statement with a condition.
- Else statement without a condition.
- Both parts have code blocks surrounded by {}.
don’t forget the brackets
if (x > 10) {
console.log("x is greater than 10");
console.log("This code when the condition is true");
} else {
console.log("x is 10 or less");
console.log("This code runs when the condition is false");
}
Without brackets:
if (x > 10)
console.log("x is greater than 10");
console.log("this code will always run");
- Based on this code, if you were younger than 16 what would it print out?
- Write your own if else statement
// if I was younger than 16, it would print out "You are not old enough for a license yet."
// if else statement code below
int age = 20;
if (age < 13) {
System.out.println("you are very young");
} else if (age >= 13 && age < 20) {
System.out.println("you are a teen ");
} else if (age >= 20 && age < 65) {
System.out.println("your pretty young.");
} else {
System.out.println("hi");
}
your pretty young.
if (x >= 16) {
console.log("You areold enough to drive");
console.log("This code when the condition is true");
} else {
console.log("Grow older!");
console.log("This code runs when the condition is false");
}
| console.log("You areold enough to drive");
illegal character: '\u00a0'
| console.log("You areold enough to drive");
illegal character: '\u00a0'
| console.log("You areold enough to drive");
illegal character: '\u00a0'
| console.log("You areold enough to drive");
illegal character: '\u00a0'
| console.log("This code when the condition is true");
illegal character: '\u00a0'
| console.log("This code when the condition is true");
illegal character: '\u00a0'
| console.log("This code when the condition is true");
illegal character: '\u00a0'
| console.log("This code when the condition is true");
illegal character: '\u00a0'
| console.log("Grow older!");
illegal character: '\u00a0'
| console.log("Grow older!");
illegal character: '\u00a0'
| console.log("Grow older!");
illegal character: '\u00a0'
| console.log("Grow older!");
illegal character: '\u00a0'
| console.log("This code runs when the condition is false");
illegal character: '\u00a0'
| console.log("This code runs when the condition is false");
illegal character: '\u00a0'
| console.log("This code runs when the condition is false");
illegal character: '\u00a0'
| console.log("This code runs when the condition is false");
illegal character: '\u00a0'