Tutorials Home   >   Programming Basics   >   What are Conditional Statements?

What are Conditional Statements?

What Are Conditional Statements in Programming?

Introduction

Programs are useful because they can make decisions. Just like humans decide what to do based on situations, programs use conditional statements to choose different actions depending on conditions. Without conditional statements, programs would run the same way every time and would not be very intelligent.

Conditional statements allow a program to check whether something is true or false, and then decide what to do next. They are one of the most important concepts in programming and are used in almost every type of software.

In this explanation, you will learn:

  • What conditional statements are

  • Why they are important

  • Types of conditional statements

  • How conditional statements work in different programming languages

  • Common mistakes learners make


1. Definition of Conditional Statements

A conditional statement is a programming instruction that performs different actions based on whether a condition is true or false.

Simple Definition:

Conditional statements allow a program to make decisions by testing conditions.

These conditions usually compare values using operators such as:

  • greater than (>)

  • less than (<)

  • equal to (==)

  • not equal to (!=)


2. Why Are Conditional Statements Important?

Conditional statements help programs behave intelligently.

2.1 Decision Making

Programs can react differently based on user input or data.

Example:

  • If a student passes, display “Pass”

  • If not, display “Fail”


2.2 Control Program Flow

Conditional statements control which parts of a program are executed and which are skipped.

This helps:

  • Reduce errors

  • Avoid unnecessary calculations

  • Improve performance


2.3 Real-Life Use

Conditional statements are used in:

  • Games (checking score or lives)

  • Banking apps (checking balance)

  • Websites (login systems)


3. The if Statement

The most basic conditional statement is the if statement.

How It Works:

  • The program checks a condition

  • If the condition is true, the code inside runs

  • If the condition is false, the code is skipped

Example (Python):

if age >= 18:
print("You are an adult")

If age is 18 or more, the message is displayed.


4. The if-else Statement

The if-else statement allows a program to choose between two options.

How It Works:

  • If the condition is true, one block runs

  • If the condition is false, another block runs

Example:

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

5. The if-elif-else Statement

When there are multiple conditions, programmers use if-elif-else.

Example:

if score >= 75:
print("Distinction")
elif score >= 50:
print("Pass")
else:
print("Fail")

This checks conditions in order, from top to bottom.


6. Comparison and Logical Operators

Conditional statements often use operators.

Comparison Operators:

  • == equal to

  • != not equal to

  • > greater than

  • < less than

  • >= greater than or equal to

  • <= less than or equal to

Logical Operators:

  • and – all conditions must be true

  • or – at least one condition must be true

  • not – reverses the condition

Example:

if age >= 18 and age <= 60:
print("Eligible")

7. Switch / Case Statements

Some languages use switch or case statements to handle many choices.

Example (C++ / Java):

switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
default: cout << "Invalid day";
}

This is often clearer than many if-else statements.


8. Conditional Statements in Different Languages

8.1 Python

  • Uses indentation

  • No brackets

if temperature > 30:
print("Hot day")

8.2 Java and C++

  • Uses brackets { }

  • Uses parentheses ( )

if (temperature > 30) {
System.out.println("Hot day");
}

8.3 JavaScript

if (temperature > 30) {
console.log("Hot day");
}

9. Real-World Examples of Conditional Statements

9.1 Login System

if password == "1234":
print("Access granted")
else:
print("Access denied")

9.2 Grading System

if marks >= 50:
print("Pass")
else:
print("Fail")

9.3 Traffic Light System

  • If light is red → Stop

  • If light is green → Go


10. Common Mistakes Learners Make

  1. Using = instead of ==

  2. Forgetting indentation (Python)

  3. Writing conditions in the wrong order

  4. Using too many if statements instead of elif

  5. Forgetting an else when needed


Conclusion

Conditional statements are a fundamental part of programming. They allow programs to make decisions by checking conditions and choosing different actions based on the results.

From simple if statements to complex decision-making systems, conditional statements help programs respond to data, user input, and real-world situations. Mastering conditional statements is a key step toward writing intelligent and useful programs.

As you continue learning, remember: