Nested Conditionals
Breadcrumb: /csp/nested_conditionalsThis page will teach you about nested conditionals in programming.
Lesson: Nested Conditionals
What are Nested Conditionals?
A nested conditional is a conditional statement inside another conditional statement, or an if statement within an if statement.
This allows us to check multiple conditions in a structured way, which is useful when decisions depend on more than one factor
For example:
- First, check if a main condition is true.
- Then, only if it’s true, check an additional condition inside it.
The examples below uses if and else statments to form a nested conditional in both Python and Java.
# Example of a nested conditional in Python
age = 18 ##Sets age variable
has_id = True ##Sets has_id variable
if age >= 18: ##Checks if age is 18 or older
if has_id: ##Checks if the person has an ID
print("You are allowed to enter.")
else: ##If the person does not have an ID
print("You need an ID to enter.")
else: ##If the person is younger than 18
print("You are too young to enter.")
// Example of a nested conditional in JavaScript
let age = 18;
let hasID = true;
if (age >= 18) {
if (hasID) {
console.log("You are allowed to enter.");
} else {
console.log("You need an ID to enter.");
}
} else {
console.log("You are too young to enter.");
}
How the example works:
- The program first checks if
age >= 18
. - If False, it immediately goes to the
else
branch. - If True, it goes inside and checks the nested condition
if has_id
. - Depending on if the if statement is true or false, a different final result is printed.
Snake Game Example
Heres an example of a nested conditional in our Snake Game Hack. In this example, the nested conditional is used to check whether or not the snake hits a wall, losing a life if they do.
// Wall Checker
if(wall === 1){
// Wall on, Game over test
if (snake[0].x < 0 || snake[0].x === canvas.width / BLOCK || snake[0].y < 0 || snake[0].y === canvas.height / BLOCK){
loseLife();
return;
}
}else{
// Wall Off, Circle around
for(let i = 0, x = snake.length; i < x; i++){
if(snake[i].x < 0){
snake[i].x = snake[i].x + (canvas.width / BLOCK);
}
if(snake[i].x === canvas.width / BLOCK){
snake[i].x = snake[i].x - (canvas.width / BLOCK);
}
if(snake[i].y < 0){
snake[i].y = snake[i].y + (canvas.height / BLOCK);
}
if(snake[i].y === canvas.height / BLOCK){
snake[i].y = snake[i].y - (canvas.height / BLOCK);
}
}
}
Calculator Example
Heres another example of a nested conditional, this time in our calculator hack. In this example, multiple nested conditionals are used to do multiple things:
- Distinguishing between digits and decimal points
- Overwrting the display when inputing new numbers
- prevent leading zeros
- Allowing only 1 decimal per number
// Number action
function number (value) { // function to input numbers into the calculator
if (value != ".") {
if (nextReady == true) { // nextReady is used to tell the computer when the user is going to input a completely new number
output.innerHTML = value;
if (value != "0") { // if statement to ensure that there are no multiple leading zeroes
nextReady = false;
}
} else {
output.innerHTML = output.innerHTML + value; // concatenation is used to add the numbers to the end of the input
}
} else { // special case for adding a decimal; can't have two decimals
if (output.innerHTML.indexOf(".") == -1) {
output.innerHTML = output.innerHTML + value;
nextReady = false;
}
}
}
Nested Conditionals MC
–
Question 1
What is a nested conditional?
- A) Two separate if statements next to each other
- B) An if statement inside another if statement
- C) A while loop inside an if statement
- D) A conditional that always runs no matter what
Question 2
What will the following Python code print?
age = 18
has_id = False
if age >= 18:
if has_id:
print("You are allowed to enter.")
else:
print("You need an ID to enter.")
else:
print("You are too young to enter.")
- A) You are allowed to enter.
- B) You need an ID to enter.
- C) You are too young to enter.
- D) Nothing will print.
Question 3
In the nested condition below, what condition must be true for “Access Granted” to print?
if username == "admin":
if password == "1234":
print("Access Granted")
- A) Only username is correct
- B) Only password is correct
- C) Both username and password are correct
- D) Either one is correct