School of Computer Science | Cloud & Software Operations Cluster
Instructor: Dr. Mohsin Dar
Topics Covered:
Conditional statements allow programs to make decisions based on certain conditions. They enable the program to execute different blocks of code depending on whether a condition is true or false.
Condition → Evaluation → Action
Example: "If it's raining, take an umbrella; otherwise, don't."
The simplest form of conditional statement. It executes a block of code only if a condition is true.
if (condition) {
// Code to execute if condition is true
statement1;
statement2;
// ...
}
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote!\n");
}
printf("Program continues...\n");
return 0;
}
Output: You are eligible to vote!
Program continues...
↓
Start
↓
Evaluate Condition
↓
|
True ↓ Execute if block ↓ |
False ↓ Skip if block ↓ |
Continue with next statement
↓
End
Provides an alternative path of execution when the condition is false.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
#include <stdio.h>
int main() {
int number = 15;
if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
return 0;
}
Output: 15 is an odd number.
Used when you need to check multiple conditions in sequence.
if (condition1) {
// Code for condition1 = true
} else if (condition2) {
// Code for condition2 = true
} else if (condition3) {
// Code for condition3 = true
} else {
// Code when all conditions are false
}
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 90) {
printf("Grade: A+\n");
} else if (marks >= 80) {
printf("Grade: A\n");
} else if (marks >= 70) {
printf("Grade: B\n");
} else if (marks >= 60) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Output: Grade: A
An if-else statement inside another if-else statement. Useful for complex decision-making scenarios.
if (condition1) {
if (condition2) {
// Code when both condition1 and condition2 are true
} else {
// Code when condition1 is true but condition2 is false
}
} else {
// Code when condition1 is false
}
#include <stdio.h>
int main() {
int age = 20;
char hasLicense = 'N';
if (age >= 18) {
printf("Age requirement met.\n");
if (hasLicense == 'Y') {
printf("You can drive legally!\n");
} else {
printf("You need to get a license first.\n");
}
} else {
printf("You are too young to drive.\n");
}
return 0;
}
Output: Age requirement met.
You need to get a license first.
| Statement Type | Use Case | Number of Conditions | Example Scenario |
|---|---|---|---|
| if | Single condition check | 1 | Check if student passed |
| if-else | Binary decision | 1 (with alternative) | Pass or Fail |
| if-else if-else | Multiple exclusive conditions | Multiple | Grade classification |
| Nested if-else | Dependent conditions | Hierarchical | Age and license check |
#include <stdio.h>
int main() {
int mathMarks = 85, scienceMarks = 78, englishMarks = 88;
float percentage;
char category = 'G'; // G = General, S = SC/ST
percentage = (mathMarks + scienceMarks + englishMarks) / 3.0;
printf("Student's Percentage: %.2f%%\n", percentage);
if (percentage >= 60) {
printf("Basic eligibility: MET\n");
if (category == 'G') {
if (percentage >= 85) {
printf("Admission Status: CONFIRMED (General Category)\n");
} else if (percentage >= 75) {
printf("Admission Status: WAITLISTED (General Category)\n");
} else {
printf("Admission Status: REJECTED (General Category)\n");
}
} else if (category == 'S') {
if (percentage >= 70) {
printf("Admission Status: CONFIRMED (Reserved Category)\n");
} else {
printf("Admission Status: WAITLISTED (Reserved Category)\n");
}
}
} else {
printf("Basic eligibility: NOT MET\n");
printf("Admission Status: REJECTED (Minimum criteria not satisfied)\n");
}
return 0;
}
// Good Practice - Clear and readable
if (temperature > 30) {
printf("It's hot outside!\n");
}
// Avoid - Unclear condition
if (t > 30) {
printf("It's hot outside!\n");
}
// Good Practice
if (score >= 90) {
if (attendance >= 80) {
printf("Excellent performance!\n");
}
}
// Poor Practice
if(score>=90){
if(attendance>=80){
printf("Excellent performance!\n");
}
}
// Recommended
if (x > 0) {
printf("Positive number\n");
}
// Can lead to errors later
if (x > 0)
printf("Positive number\n");
// WRONG - Assignment instead of comparison
if (x = 5) { // This assigns 5 to x, doesn't compare!
printf("x is 5\n");
}
// CORRECT - Comparison operator
if (x == 5) {
printf("x is 5\n");
}
// PROBLEMATIC - Only first statement is in if block
if (x > 0)
printf("Positive\n");
printf("This always executes!\n");
// CORRECT - Use braces
if (x > 0) {
printf("Positive\n");
printf("This is also in the if block\n");
}
// AMBIGUOUS - Which if does the else belong to?
if (x > 0)
if (y > 0)
printf("Both positive\n");
else // This belongs to inner if, not outer!
printf("x is not positive\n");
// CLEAR - Use braces to clarify
if (x > 0) {
if (y > 0) {
printf("Both positive\n");
}
} else {
printf("x is not positive\n");
}
Write a program to classify temperature as:
A year is a leap year if:
Examples: 2000 (leap), 1900 (not leap), 2024 (leap)
Create a calculator that takes two numbers and an operator (+, -, *, /) and performs the corresponding operation using nested if-else statements.
Questions & Discussion
Dr. Mohsin Dar
Assistant Professor
Cloud & Software Operations Cluster | SOCS | UPES
Office Hours: Available for doubts and clarifications