Conditionals in C
π’ Beginner
π Definition
Conditional statements (if, else if, else, switch) control the execution flow of a C program by executing specific code blocks based on whether logical expressions evaluate to true (non-zero) or false (0).
π Multilingual Explanation
English
C uses if-else branches and switch statements to make decisions. In C, any non-zero numeric value evaluates as true, while 0 evaluates as false. switch statements require break keywords to prevent accidental fallthrough to subsequent cases.
Hindi
C program decision-making ke liye if, else if, else aur switch statements ka use karta hai. C mein zero (0) ka matlab false hota hai aur koi bhi non-zero number (jaise 1, 100, -5) true hota hai. switch statement mein break na lagane par fallthrough ho jata hai.
Marathi
C madhye decision-making sathi if, else if, else aani switch statements vaparali jatat. C madhye 0 mhanje false aani 0 vyatirikta konta hi number true manla jato. switch madhye break naslyas tya nantarche cases sudha execute hotat.
Hinglish
Program execution flow control karne ke liye if-else aur switch use hota hai. Comparison ke liye if (score == 100) likhein, = assignment ke sath mix mat karein. Short-circuit evaluation se pehli condition fail hone par logical AND && doosri condition evaluation skip kar deta hai.
π€ Why Do We Use Them?
Real-world software is dynamic. A banking app must check if a user has sufficient account balance before approving a withdrawal. An ATM must present choices based on user menu selections. Conditionals allow code to make decisions based on changing data.
π§ Simple Explanation
Think of conditionals as road signs:
if (weather == RAINY): Take an umbrella.else if (weather == SUNNY): Wear sunglasses.else: Wear normal clothes.
π Syntax & Decision Structures
1. if - else if - else Ladder
if (condition1) {
// Executes if condition1 is non-zero (true)
} else if (condition2) {
// Executes if condition1 is 0 AND condition2 is non-zero
} else {
// Executes if all preceding conditions evaluate to 0 (false)
}
2. switch Statement
switch compares an integer or char expression against constant case values:
switch (expression) {
case CONSTANT1:
// Statements
break; // Exits switch block
case CONSTANT2:
// Statements
break;
default:
// Executes if no cases match
break;
}
π‘ Practical Example
Here is a practical student grading and academic standing system demonstrating both if-else ladders and switch menus:
#include <stdio.h>
int main(void) {
int studentMarks = 84;
char gradeLetter;
// 1. Determining Grade using if - else if - else Ladder
if (studentMarks >= 90) {
gradeLetter = 'A';
} else if (studentMarks >= 75) {
gradeLetter = 'B';
} else if (studentMarks >= 60) {
gradeLetter = 'C';
} else if (studentMarks >= 40) {
gradeLetter = 'D';
} else {
gradeLetter = 'F';
}
printf("Student Score : %d / 100\n", studentMarks);
printf("Grade Assigned: %c\n\n", gradeLetter);
// 2. Performance Feedback using switch
printf("--- ACADEMIC FEEDBACK ---\n");
switch (gradeLetter) {
case 'A':
printf("Remark: Outstanding performance! Eligible for scholarship.\n");
break;
case 'B':
printf("Remark: Good performance. Keep up the consistent work.\n");
break;
case 'C':
printf("Remark: Satisfactory performance. Room for improvement.\n");
break;
case 'D':
printf("Remark: Marginal pass. Academic counseling recommended.\n");
break;
case 'F':
printf("Remark: Failed course. Re-examination required.\n");
break;
default:
printf("Remark: Invalid grade specified!\n");
break;
}
return 0;
}
π Code Breakdown
if (studentMarks >= 90): Checks conditions sequentially. Once a condition evaluates to1(true), its block executes, and C skips all remainingelse ifandelsebranches.switch (gradeLetter): Evaluates the single character variablegradeLetter.break: Essential inside eachcase. It terminates execution of theswitchstatement and jumps out to the next statement outside the switch block.
π Output
Student Score : 84 / 100
Grade Assigned: B
--- ACADEMIC FEEDBACK ---
Remark: Good performance. Keep up the consistent work.
β οΈ Common Mistakes
- Accidental Assignment in Condition: Writing
if (mark = 100)assigns100tomark. Since100is non-zero, the condition is alwaystrue! Always use==for comparisons:if (mark == 100). - Missing
breakinswitch(Fallthrough): Forgettingbreakcauses execution to βfall throughβ and execute subsequent case blocks even if their case labels do not match!switch (code) { case 1: printf("One\n"); // Missing break! case 2: printf("Two\n"); // Executes both "One" and "Two" if code is 1! } - Float in
switch:switchstatements only accept integral values (int,char,enum). Floating-point numbers (float,double) cannot be used in aswitch.
π‘οΈ Safety / Short-Circuit Evaluation
C uses Short-Circuit Evaluation for logical operators:
- In
A && B: IfAevaluates to0(false), C will not evaluateBat all, because the overall result is guaranteed to befalse. - In
A || B: IfAevaluates to non-zero (true), C will not evaluateBat all.
Example Safety Guard:
if (ptr != NULL && *ptr == 10) { // Safe! If ptr is NULL, *ptr is never evaluated, preventing crash
printf("Valid match\n");
}
π Real-World Usage
Conditionals drive user authorization checks, operating system kernel interrupt handling, game engine state machines, network packet routing, and error recovery handlers.
π§ͺ Try It Yourself
- Write a C program that takes a number and checks if it is Positive, Negative, or Zero.
- Also check if the number is Even or Odd using
% 2 == 0.
π― Mini Challenge
Write a menu-driven ATM program using switch. Present 3 choices:
- Check Balance
- Deposit Money
- Withdraw Money
Input the userβs menu choice and execute the selected action using a starting balance of
$1000.00.
π Related Topics
π§ Navigation
| β C Home | β Previous: Operators | Next: Loops β |