Skip to content
GetITechSkills

Logic and Programming

If and Else: How Programs Make Decisions

Beginner · Concept · 6 min read · Updated October 30, 2026

A teacher reading a test paper does not give every student the same mark. She looks at the score and decides: this one is an A, that one needs another look. A program needs the same ability. Without it, code would do the same fixed steps on every run, whatever the data looked like. The tool that adds choice is the conditional, and the most common form is the if statement.

This entry shows how a program tests a condition and picks a path. It uses a small grade checker as the running example, then covers comparisons, combining conditions and the errors that appear most often. It assumes you know what a variable is.

The basic idea: a question with a yes or no answer

An if statement asks a question that can only be answered with true or false. If the answer is true, the program runs a block of steps. If the answer is false, it skips them. The question is called the condition.

score = 82

if score >= 50:
    print("Passed")

Here score >= 50 asks "is the score at least 50?" For 82 the answer is true, so the program prints Passed. If score were 40, the condition would be false and nothing would be printed.

Two details deserve attention. First, the line with if ends with a colon. Second, the steps that belong to the if are indented, which means they start a few spaces to the right. In Python, that indentation is the rule that groups lines together. Many other languages use curly brackets instead:

if (score >= 50) {
  console.log("Passed");
}

The idea is identical. Only the punctuation differs.

Adding else and elif

Often you want a second path for when the condition is false. The else part provides it. It has no condition of its own, since it simply means "in every other case".

score = 82

if score >= 50:
    print("Passed")
else:
    print("Not yet")

For several outcomes, add elif branches. The word is short for "else if". Python checks the conditions from the top, runs the first block whose condition is true, and skips the rest. This grade checker has four outcomes:

score = 82

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "Needs review"

print("Grade:", grade)

Follow it by hand. Is 82 at least 90? No. Is it at least 80? Yes, so grade becomes "B" and Python skips the remaining branches. The output is Grade: B. In JavaScript the middle keyword is written as two words, else if.

Why the order matters

Because the first true branch wins, the order of the checks changes the result. Suppose you wrote the grade checker like this:

if score >= 70:
    grade = "C"
elif score >= 90:
    grade = "A"

A score of 95 would receive a "C", since 95 is at least 70 and Python never looks at the second branch. When ranges overlap, put the most specific or highest test first.

Comparing values

A condition is usually a comparison. These are the common ones:

Operator Meaning Example Result when score is 82
== equal to score == 82 True
!= not equal to score != 82 False
> greater than score > 82 False
>= greater than or equal to score >= 82 True
< less than score < 90 True
<= less than or equal to score <= 80 False

The boundary cases are where bugs hide. With score > 90, a student who scored exactly 90 does not get an A. With score >= 90, they do. Decide in plain words first, such as "90 and above is an A", and then pick the operator that matches.

Combining conditions with and, or and not

Real decisions often depend on more than one fact. Three words let you combine conditions:

  • and is true only when both sides are true.
  • or is true when at least one side is true.
  • not flips true to false and false to true.

Suppose a course is passed only when the score is at least 50 and the student attended at least 8 sessions:

score = 82
sessions_attended = 6

if score >= 50 and sessions_attended >= 8:
    print("Course passed")
else:
    print("Course not passed")

The first part is true, but 6 is less than 8, so the whole and is false. The output is Course not passed. If you swapped and for or, the result would be Course passed, because one true side is enough. JavaScript writes these as &&, || and !.

Nesting and keeping it readable

You can place an if inside another one. This is called nesting. A little is fine, but deep nesting is hard to follow. If you find yourself three levels down, try combining conditions with and or splitting the logic into a function, which the entry on reusable functions explains.

A short form for simple choices

When you only need to pick one of two values, Python allows a one-line form:

result = "Pass" if score >= 50 else "Fail"

With a score of 82, result becomes "Pass". JavaScript spells this with a question mark and colon: score >= 50 ? "Pass" : "Fail". Use the short form only when it stays easy to read.

What counts as true

In Python, you can put a value that is not a boolean straight into a condition. Zero, empty text, an empty list and None count as false, and most other values count as true. So if name: is true when name has any characters in it. JavaScript has a similar idea with its own list of "falsy" values, which includes 0, "", null and undefined. The two lists are not identical, so check the documentation of the language you use.

Common mistakes

  • Using = instead of ==. A single equals sign stores a value. A comparison needs two. Python reports an error for this inside an if, but some languages accept it silently and change your data.
  • Overlapping ranges in the wrong order. As shown above, an earlier broad test hides a later narrow one. Check the highest or most specific case first.
  • Off-by-one boundaries. Mixing up > and >= gives the wrong answer exactly at the edge value. Test the edge values by hand.
  • Forgetting the indentation or the colon. In Python, a missing colon or inconsistent indentation stops the program with a syntax error. Keep to the same number of spaces each time.
  • Comparing text and numbers. "82" >= 50 is an error in Python, because one side is text. Convert first, as the entry on variables explains.
  • Writing if x == True. It works, but if x says the same thing more simply.

FAQ

What is the difference between if and elif?

if starts the chain and elif adds another test that runs only when every earlier test was false. You can have as many elif branches as you need, and the else at the end is optional.

Do I need an else after every if?

No. Many decisions only need to act when something is true, and doing nothing otherwise is fine. Add else when the false case needs its own action.

What is a switch or match statement?

Some languages offer a separate form for choosing between many fixed values. Python has match and JavaScript has switch. A chain of if and elif can do the same job, so start there.

Can a condition call a function?

Yes. Any expression that produces true or false can be a condition, including the result of a function. This is common, for example with a function that checks whether an email address looks valid.

Where do decisions fit with the other basics?

Decisions work with loops, which repeat steps, and with functions, which package them. The Logic and Programming area covers each one in turn, starting with loops.

Sources

  1. More Control Flow Tools: if Statements (opens in a new tab) — Python Software Foundation (docs.python.org). Official tutorial chapter on if, elif and else
  2. Control flow and error handling (JavaScript Guide) (opens in a new tab) — MDN Web Docs (Mozilla). How JavaScript writes if...else and which values count as false
  3. Built-in Types: Truth Value Testing (opens in a new tab) — Python Software Foundation (docs.python.org). Which Python values count as true or false

Next in Logic and Programming: Loops Explained: Repeating Work in Code

  • Logic and Programming

    Variables and Data Types Explained

    A variable is a named place to keep a value. Learn the main data types (numbers, text, true/false), how to convert between them, and common traps.

    • Beginner
    • Concept
    • 6 min read

    Updated October 30, 2026

  • Logic and Programming

    Loops Explained: Repeating Work in Code

    Learn what for and while loops do, how to pick between them, how break and continue work, and how to avoid infinite loops, with a shopping cart example.

    • Beginner
    • Concept
    • 6 min read

    Updated October 30, 2026

  • Logic and Programming

    Functions Explained: Reusable Pieces of Code

    A function is a named block of code you can run again and again. Learn parameters, return values, scope and why functions keep programs tidy.

    • Beginner
    • Concept
    • 6 min read

    Updated October 30, 2026