Skip to main content

Command Palette

Search for a command to run...

🚦 Conditionals Explained Like You're 5

Published
β€’2 min read
S

Building AI systems and writing about how they actually work. Master of AI @ University of Technology Sydney. Previously B.Tech CS with focus on IoT. I believe the best way to learn is to explain. That's why I'm documenting tech concepts with simple analogies (@sreekarreddy.com). AWS Certified β€’ Azure AI Certified β€’ Neo4j Professional β€’ Google Data Analytics When not coding: exploring Sydney, working on side projects, and teaching tech to anyone who'll listen.

Traffic lights for your code

Day 61 of 149

πŸ‘‰ Full deep-dive with code examples


The Traffic Light Analogy

At a traffic light, you make decisions:

  • Red light? β†’ Stop
  • Green light? β†’ Go
  • Yellow light? β†’ Slow down

You decide based on what's happening.

Conditionals let your code make decisions!


The Problem They Solve

Some code needs to behave differently based on situations:

  • User logged in? β†’ Show dashboard
  • User not logged in? β†’ Show login page
  • Item in stock? β†’ Allow purchase
  • Item out of stock? β†’ Show "sold out"

Without conditionals, code would do the same thing every time.


How They Work

The basic pattern:

IF something is true:
    do this
ELSE:
    do that instead

It's like asking a yes/no question, then acting on the answer.


Real-World Examples

Checking age:

  • If age >= 18 β†’ "You can vote"
  • Else β†’ "Too young to vote"

Weather app:

  • If temperature > 30 β†’ "It's hot!"
  • Else if temperature > 15 β†’ "Nice weather"
  • Else β†’ "Bring a jacket"

Game logic:

  • If health <= 0 β†’ Game over
  • Else β†’ Keep playing

Multiple Conditions

Sometimes you need to check several things:

  • If sunny AND weekend β†’ Go to beach
  • If raining OR cold β†’ Stay home

You can combine conditions for more complex decisions.


Why They Matter

Conditionals make programs smart:

  • Respond to user input
  • Handle different situations
  • Make games interactive
  • Create personalized experiences

Without them, programs would be boring robots doing the same thing forever!


In One Sentence

Conditionals let your code ask "if this is true, then do that" so programs can make decisions and respond to different situations.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

More from this blog

esreekarreddy

132 posts