Table of Contents:
Decision Statements
In real life, we make decisions all the time—if it rains, take an umbrella; otherwise, go without it.
Similarly, computer programs also need to make decisions based on certain conditions. Decision statements (also called conditional statements) allow a program to choose different actions depending on the situation.
These statements help programs behave intelligently by checking conditions and executing the appropriate block of code.
What Are Decision Statements?
Decision statements evaluate a condition and execute specific code only when that condition is true.
A condition typically involves relational or logical operators.
Example condition:
age >= 18
If this condition is true, the program may allow the user to vote; otherwise, it may show a message saying they are not eligible.
Types of Decision Statements
Most programming languages, including C, Java, and Python, use the following decision-making constructs:
decision statements, conditional statements, if else programming, switch statement, elif Python, decision making in programming, C if statement, Java conditions, Python conditions, programming basics
If Statement
Runs a block of code only if the condition is true.
Example in C
if (age >= 18) {
printf("You are eligible to vote.");
}
Example in Java
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
Example in Python
if age >= 18:
print("You are eligible to vote.")
If…Else Statement
Provides an alternative block of code when the condition is false.
Example
if (condition) {
// runs if true
} else {
// runs if false
}
Python
if age >= 18:
print("Adult")
else:
print("Minor")
Else If / Elif Ladder
Used when multiple conditions need to be checked in sequence.
C Example
if (marks >= 90) {
printf("Grade A");
} else if (marks >= 75) {
printf("Grade B");
} else {
printf("Grade C");
}
Python (elif)
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")
Nested If
Placing one if statement inside another.
if (age >= 18) {
if (citizen == true) {
System.out.println("Eligible to vote");
}
}
Switch Statement (C and Java Only)
Switch executes code blocks based on matching values. Python uses match (from Python 3.10+).
C Example
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid day");
}
Python Match
match day:
case 1: print("Monday")
case 2: print("Tuesday")
case _: print("Invalid day")
Why Decision Statements Matter
You use decision statements when your program must:
- Check conditions
- Validate user input
- Control the flow of execution
- Handle different outcomes
- Make logical choices
They are fundamental to writing interactive and intelligent programs.
FAQ
What is a decision statement in programming?
A decision statement allows a program to choose different actions based on conditions.
What is the difference between if and else?
if executes when the condition is true; else runs when it is false.
Does Python have a switch statement?
Older versions do not, but Python 3.10+ introduced match (similar to switch).
Can we use multiple conditions?
Yes, using else if (C/Java) or elif (Python).
What is a nested if?
A nested if is an if statement placed inside another if, used for checking multiple related conditions.