Course contentsShow
← Computer Science
Lesson 166 of 2,8724. Introduction to ProgrammingFree lesson

Boolean Expressions in Conditionals

How boolean expressions evaluate to true or false to determine which code path to execute.

Boolean Expressions in Conditionals

You'll learn: How boolean expressions evaluate to true or false to determine which code path your program takes.

The Core Idea

In the last lesson, you learned what conditionals are—decision points in your code. Now let's understand how they make decisions: through boolean expressions.

A boolean expression is any expression that evaluates to either true or false. When you write a conditional, you're essentially asking your program a yes-or-no question. The answer determines which block of code runs.

Think of it like a security checkpoint: "Is your ticket valid?" If the answer is true, you pass through. If false, you're directed elsewhere.

How It Works

Remember the comparison operators (==, !=, <, >, <=, >=) and logical operators (&&, ||, !) you learned? These create boolean expressions:

  • age >= 18 evaluates to true or false
  • score == 100 evaluates to true or false
  • isRaining && temperature < 50 evaluates to true or false

When your program reaches a conditional, it evaluates the boolean expression first. Based on the result—true or false—it chooses which path to execute. If true, it runs the "then" branch. If false, it skips that branch (and might run an "else" branch instead, which we'll cover soon).

Example in Plain English

Imagine: if (temperature > 30) then wear shorts.

  1. The program evaluates temperature > 30
  2. If temperature is 35: 35 > 30 evaluates to true → wear shorts
  3. If temperature is 20: 20 > 30 evaluates to false → skip that instruction

Key Takeaway: Boolean expressions are the decision-makers in conditionals—they evaluate to true or false, and that boolean result determines which code path executes.