Condition (If Statement)

A condition (if statement) is a control structure that executes code only if a specified condition is true. It allows a program to make decisions.

Why conditions are important

Programs often need to choose between different actions.

 

For example:

  • If the user is logged in → show dashboard
  • If the password is incorrect → show error
  • If temperature is below 0 → display warning

Without conditions, programs would always execute the same instructions.

How an if statement works

A condition checks whether an expression is true or false.

 

Basic structure:

if (condition) { // code runs if condition is true }

Example:

age = 18 if (age >= 18) { print("Adult") }

If the condition is true, the code inside runs.

If false, it is skipped.

If–Else Structure

Sometimes a program must choose between two options.

if (condition) { // runs if true } else { // runs if false }

Example:

if (score >= 50) { print("Pass") } else { print("Fail") }

Else If

Used when checking multiple conditions.

if (condition1) { // code } else if (condition2) { // code } else { // default code }

This allows more complex decision-making.

Conditions and Boolean Values

Conditions always evaluate to a Boolean value:

  • true
  • false

Comparison and logical operators are commonly used inside conditions.

 

Example:

if (age >= 18 AND hasID == true)

Why learning conditions matters

Conditions allow programs to:

  • Make decisions
  • React to user input
  • Control logic
  • Create dynamic behavior

Decision-making is a core part of programming.

Simple example

Think of a traffic light:

  • If the light is green → go
  • If the light is green → go

That is a real-life conditional structure.

Related terms

Source

Simplified from general programming documentation and Wikipedia.

Nach oben scrollen