Nested Conditonals
A blog understanding nested conditionals taught by students and how they work and help
What is a Nested Conditonals?
- Nested Conditionals
- Definition: Conditional statements inside other conditional statements.
-
Usage: Handle multiple, related conditions.
- Benefits:
- Enhanced Control: Manage complex decisions.
- Modular Code: Easier to maintain.
- Flexibility: Implement intricate logic clearly.
Example of a Nested Conditonal
Nested Condiontals were important because the students who taught this lesson stated that you can create diffrent complex statements to make sure they are small and don’t take up space and secondly it works perfectly in the game you are working on creating compex dialouges with npcs or outcomes and example is when we were working on one of our popcorn hack.
%%html
<!-- HTML output div -->
<div id="message"></div>
<script>
function runWhenDOMLoaded() {
// Example choices
let appleColor = "red"; // Change this to "green" to test different outcomes
let eatApple = "yes"; // Change this to "no" to test different outcomes
const messageElement = document.getElementById("message");
function displayMessage(message) {
console.log(message); // Log to console
messageElement.textContent = message; // Display on the webpage
}
// Check the color of the apple chosen by the user
if (appleColor === "red") {
// If the apple is red, check if the user wants to eat it
if (eatApple === "yes") {
displayMessage("Lmao, the apple you ate was poisonous. You're dead :D");
} else {
displayMessage("You decided not to eat the red apple (You're boring -_-).");
}
} else if (appleColor === "green") {
// If the apple is green, check if the user wants to eat it
if (eatApple === "yes") {
displayMessage("You ate the green apple.");
} else {
displayMessage("You decided not to eat the green apple (You're boring -_-).");
}
} else {
// If the user enters an invalid color, display an error message
displayMessage("Invalid answer. Please choose 'red' or 'green'.");
}
}
// Ensure the function runs only after the page loads
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', runWhenDOMLoaded);
} else {
runWhenDOMLoaded();
}
</script>
- The code checks the color of an apple and whether the user wants to eat it.
- Apple Color Check:
- If the apple is “red”:
- If the user wants to eat it, display a message that the apple was poisonous.
- If the user doesn’t want to eat it, display a message saying they didn’t eat the apple.
- If the apple is “green”:
- If the user wants to eat it, display a message that they ate the green apple.
- If the user doesn’t want to eat it, display a message saying they didn’t eat the apple.
- If the apple is neither red nor green, display an error message.
Conclusion
- Nested Conditionals:
- Handle related conditions within other conditions.
- Offer precise control over decision-making.
- Enable modular and readable code.
- Useful in scenarios like decision-making in applications.
- Real-life Examples:
- Decision-making in apps, like shopping carts or game actions.
- Key Takeaway: Nested conditionals simplify complex decision processes in programming.