C Programming Day 4
Conditional Statements (if, if-else, Nested if, switch)
Introduction
Programming becomes truly powerful when a program can make decisions. Imagine an ATM machine that checks your PIN before allowing transactions, or a website that verifies your login credentials. These are examples of conditional statements.
Conditional statements allow a program to execute different blocks of code depending on whether a condition is true or false.
In this lesson, you'll learn every important decision-making statement in C with diagrams, syntax, examples, interview questions, and practical programs.
By the end of this lesson, you will understand:
- What are Conditional Statements?
- Why Decision Making is Important
- Boolean Expressions
- if Statement
- if-else Statement
- Nested if Statement
- else-if Ladder
- switch Statement
- Difference Between if and switch
- Real-world Programs
- Common Mistakes
- Best Practices
- Interview Questions
- Practice Problems
- Summary
What are Conditional Statements?
Conditional statements help a program choose between multiple options based on a condition.
Think of it like real life:
- If it rains → Take an umbrella.
- Otherwise → Wear sunglasses.
Programming follows the same logic.
Condition
↓
Execute Code A
FALSE
↓
Execute Code B
Why Decision Making is Important
Decision making is used everywhere.
Examples:
- ATM PIN verification
- Login system
- Online shopping discounts
- Student grade calculation
- Voting eligibility
- Electricity bill calculation
- Salary bonus
- Bank transactions
Without conditional statements, programs cannot react intelligently.
Comparison Operators
Operator Meaning
!= Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal
<= Less Than or Equal
Example
if(age >= 18)
{
printf("Adult");
}
Logical Operators
Operator Meaning
! NOT
Example
if Statement
The if statement executes code only when the condition is true.
Syntax
{
statements;
}
Flowchart
↓
Condition?
↓
True → Execute Statement
↓
End
False
↓
End
Example 1
int main()
{
int age = 20;
if(age >= 18)
{
printf("You are eligible to vote.");
}
return 0;
}
Output
You are eligible to vote.
Example 2
#include <stdio.h>
{
int number;
printf("Enter number: ");
scanf("%d",&number);
if(number>0)
{
printf("Positive Number");
}
return 0;
}
if-else Statement
When one condition has two possible outcomes.
Syntax
{
statements;
}
else
{
statements;
}
Flowchart
↓
Condition?
↓
True → Block A
False → Block B
↓
End
Example
#include <stdio.h>
{
int age;
printf("Enter Age: ");
scanf("%d",&age);
if(age>=18)
{
printf("Adult");
}
else
{
printf("Minor");
}
return 0;
}
Output
Minor
Nested if Statement
An if statement inside another if statement is called a Nested if.
Useful when multiple conditions depend on each other.
Syntax
{
if(condition2)
{
statements;
}
}
Example
#include <stdio.h>
{
int age = 25;
int citizen = 1;
if(age>=18)
{
if(citizen==1)
{
printf("Eligible to Vote");
}
}
return 0;
}
Real-Life Example
Is Age >=18?
↓
Is Citizen?
↓
YES
↓
Allow Voting
else-if Ladder
Used when there are multiple conditions.
Syntax
{
}
else if(condition2)
{
}
else if(condition3)
{
}
else
{
}
Grade Calculator
#include <stdio.h>
{
int marks;
printf("Enter Marks: ");
scanf("%d",&marks);
if(marks>=80)
printf("Grade A+");
else if(marks>=70)
printf("Grade A");
else if(marks>=60)
printf("Grade A-");
else if(marks>=50)
printf("Grade B");
else
printf("Fail");
return 0;
}
switch Statement
The switch statement selects one option from many possible choices.
Best used for menus.
Syntax
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}
Example
#include <stdio.h>
{
int day;
printf("Enter Day Number: ");
scanf("%d",&day);
switch(day)
{
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid Day");
}
return 0;
}
Without break
{
case 1:
printf("One");
case 2:
printf("Two");
case 3:
printf("Three");
}
Output
Two
Three
This is called fall-through.
Difference Between if and switch
Works with ranges Works with fixed values
Supports logical operators No logical operators
Can compare complex conditions Compares one expression
Slightly slower in some cases Often faster for many discrete options
Best for decision making Best for menu systems
Real-World Decision-Making Programs
Program 1 – Even or Odd
#include <stdio.h>
{
int n;
scanf("%d",&n);
if(n%2==0)
printf("Even");
else
printf("Odd");
return 0;
}
Program 2 – Largest Number
#include <stdio.h>
{
int a,b;
scanf("%d%d",&a,&b);
if(a>b)
printf("%d",a);
else
printf("%d",b);
return 0;
}
Program 3 – Leap Year
#include <stdio.h>
{
int year;
scanf("%d",&year);
return 0;
}
Program 4 – Login System
#include <stdio.h>
{
int password=1234;
int input;
scanf("%d",&input);
if(input==password)
printf("Login Successful");
else
printf("Wrong Password");
return 0;
}
Program 5 – ATM Withdrawal
#include <stdio.h>
{
int balance=5000;
int amount;
scanf("%d",&amount);
if(amount<=balance)
printf("Transaction Successful");
else
printf("Insufficient Balance");
return 0;
}
Program 6 – Calculator Using switch
#include <stdio.h>
{
int a,b;
char op;
scanf("%d %c %d",&a,&op,&b);
switch(op)
{
case '+':
printf("%d",a+b);
break;
case '-':
printf("%d",a-b);
break;
case '*':
printf("%d",a*b);
break;
case '/':
if(b!=0)
printf("%d",a/b);
else
printf("Division by Zero");
break;
default:
printf("Invalid Operator");
}
return 0;
}
Common Mistakes
❌ Using = instead of = =
Wrong
Correct
if(a==5)
❌ Forgetting break in switch
printf("One");
Always use
unless you intentionally want fall-through.
Missing braces for multiple statements
Wrong
printf("Adult");
printf("Welcome");
Correct
{
printf("Adult");
printf("Welcome");
}
Best Practices
- Use meaningful variable names.
- Keep conditions simple and readable.
- Prefer switch for menu-driven programs.
- Use else-if instead of multiple separate if statements when conditions are mutually exclusive.
- Indent code consistently.
- Test edge cases (e.g., boundary values).
Interview Questions
1. What is a conditional statement?
A statement that executes different code based on a condition.
2. Difference between if and if-else?
if executes code only when the condition is true, while if-else provides an alternative block when the condition is false.
3. What is Nested if?
An if statement placed inside another if statement.
4. Why use switch?
To simplify selection among multiple fixed choices.
5. What is fall-through?
When execution continues into the next case because break is omitted.
Practice Problems
- Check whether a number is positive, negative, or zero.
- Find the largest among three numbers.
- Calculate student grades using an else-if ladder.
- Build a menu-driven calculator using switch.
- Determine whether a year is a leap year.
- Check voting eligibility based on age and citizenship.
- Verify login credentials with a username and password.
- Create an ATM withdrawal simulation with balance checking.
Summary
In Day 4, you learned how to control the flow of a C program using conditional statements. You explored the if, if-else, nested if, else-if ladder, and switch statement, along with comparison and logical operators. Through real-world examples such as login systems, ATM transactions, calculators, and grade evaluators, you saw how decision-making forms the foundation of interactive and intelligent applications.
In the next lesson (Day 5), you'll learn Loops in C (for, while, and do-while) to execute repetitive tasks efficiently.

Post a Comment
Thank You For Visit My Website.
I Will Contact As Soon As Possible.