← Back to Programming Fundamentals
07

Conditionals and Decisions: Making a Program Choose a Path

Learn to use `if`, `else if`, and `else` so a program can make clear decisions based on comparisons and boolean values.

📘 Theory

What a Conditional Is and What Problem It Solves

A conditional lets the program adapt its behavior instead of always following one fixed path.

If the age is high enough, allow access. If not, block it. If a grade reaches the minimum, pass the student. If not, fail them. This idea of alternative paths appears everywhere in programming.

The important part is not only learning a keyword. It is understanding that the program is reading a condition and choosing one branch of the flow.

  • First a condition is evaluated.
  • Then the program decides which block to execute.
  • Only one of the planned paths continues.

The First Pattern: `if` and `else`

When there are two possible outcomes, the most direct structure is one condition plus the opposite case.

1

The program first evaluates `age >= 18`. If the result is `true`, it runs the `if` block. If the result is `false`, it runs the `else` block.

2

This shows an important idea: the condition itself does not perform the action. It only decides which action gets executed.

When There Are More Than Two Cases: `else if`

Many real problems are not just yes-or-no. They include several possible scenarios.

  • Order matters: conditions are checked from top to bottom.
  • Once one condition is true, the rest of that chain no longer run.
  • `else` works as the default case if nothing before it matches.

Common Mistakes When Writing Decisions

Most beginner problems with conditionals come from messy logic, not difficult syntax.

A frequent issue is writing a technically correct condition but pairing it with the wrong message or action. Another is placing a broad rule too early and preventing more specific cases from ever being checked.

The best way to debug this stage is to change the input values and observe which branch the program actually enters.

  • Using the right condition with the wrong output message.
  • Putting a broad condition before a more specific one.
  • Forgetting the opposite case when the problem clearly needs it.
  • Not testing several values to see which branch is taken.

🧪 Learn by doing

Example Guided Example: Access to a Private Area One comparison activates a different message depending on whether the user can enter or not.

🏁 Challenges

Challenge Challenge: Show the Right Message for a Grade Create a decision with three possible outcomes based on a numeric grade.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com