Skip to content
BENIBIX Learn

Conditional Statement

Development🧩 Programming ConceptsControl Flow

Also known as If Statement, If Else, Conditional

An if runs code only when a test is true. Else runs when it is not.

Think of it like

Think of it like a fork in a path. If it rains you take the left path, else you take the right.

Example

A weather app checks if temp > 25. On a 30 degree day in Madrid it says shorts; on a cold day it says bring a coat.

Why it matters

Ifs are how a program makes choices. Every rule like you must be 13 to join is an if.

Definition

A conditional statement runs some code only when a test is true. It starts with if, followed by a question like temp > 25. If the answer is true, the lines inside run. An else part can say what to do when the answer is false. Many languages add elif or else if to check more than one test in turn.

Example code

python
temp = 30
if temp > 25:
    print("Shorts today")
else:
    print("Bring a coat")
# prints: Shorts today
The test is true, so only the first message is shown.

Common mistake

Putting the tests of an elif chain in the wrong order. The first true test wins, and the rest are skipped.

Key takeaways

  1. 1
    Test first. The code inside runs only on true.
  2. 2
    Else catches the rest. It runs when the test is false.
  3. 3
    First match wins. In a chain, later tests are skipped.

Learn it properly