C Conditional Branching: if, else-if Ladders, Nested if & Common Traps
Welcome to Phase 4 (Part 1): C Conditional Branching, if-else Ladders, Nested if & Logical Evaluation Masterclass! In real-world software, programs must make dynamic decisions based on runtime user input and system conditions. In C, decision-making is rooted in integer-based Boolean logic, where 0 represents FALSE and any non-zero value represents TRUE. In this comprehensive guide, you will master the mechanics of if, if-else, multi-branch else-if ladders, nested conditions, short-circuit logical operators, character comparisons, and critical traps like accidental assignment in condition statements.
๐ก The Golden Rule of Boolean Truth in C:
โข 0 (Zero): Evaluates strictly to FALSE.
โข Any Non-Zero Value (1, -5, 100, 0.5): Evaluates strictly to TRUE!
[ Start Condition: (marks >= 40) ]
โ
โโโโโโโโโโโโโดโโโโโโโโโโโโ
โผ (TRUE) โผ (FALSE)
[ Grade: PASS ] [ Grade: FAIL ]
โ โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโ
โผ
[ Continue Execution ]
Multiple mutually exclusive conditions unnapudu else-if ladder sequential order lo top-to-bottom evaluate avthundhi. First match ayina block execute avvagane migilina conditions skip aypothayi:
#include <stdio.h>
int main(void) {
int marks;
printf("Enter marks: ");
if (scanf("%d", &marks) != 1) {
printf("โ Invalid input! Please enter an integer.\n");
return 1;
}
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 60) {
printf("Grade B\n");
} else if (marks >= 40) {
printf("Grade C\n");
} else {
printf("Fail\n");
}
return 0;
}
โก Short-Circuit Evaluation in C
โข Logical AND (&&): First expression FALSE ayithe, second expression ni compiler evaluate kuda cheyyadhu (because entire result will definitely be false).
โข Logical OR (||): First expression TRUE ayithe, second expression ni evaluate cheyyadhu (because entire result is already true).
โก Performance Tip: Inexpensive fast checks ni left side pettali!
#include <stdio.h>
int main(void) {
char ch;
printf("Enter any single character: ");
scanf(" %c", &ch);
// Character comparison using ASCII integer values
if (ch >= 'A' && ch <= 'Z') {
printf("'%c' is an UPPERCASE alphabet (ASCII: %d)\n", ch, ch);
} else if (ch >= 'a' && ch <= 'z') {
printf("'%c' is a LOWERCASE alphabet (ASCII: %d)\n", ch, ch);
} else if (ch >= '0' && ch <= '9') {
printf("'%c' is a numeric DIGIT.\n", ch);
} else {
printf("'%c' is a SPECIAL symbol / punctuation.\n", ch);
}
return 0;
}
๐ The 3 Most Dangerous C Conditional Bugs:
1. Accidental Assignment (= instead of ==):
if (x = 5) โ Idhi equality check kaadhu! x lo 5 assign ayyi non-zero number return avvadam tho condition Always TRUE aypothundhi!
๐ก Yoda Notation Defense: if (5 == x) ani raste, accidental if (5 = x) compile error tho catch aypothundhi!
2. Accidental Semicolon after if:
if (x > 10); { printf("Positive"); } โ Semicolon ; empty statement ga act chesi if condition ni terminate chesthundhi. Braces lopala unna code condition fail ayina compulsorily execute avthundhi!
3. Floating-Point Equality Check:
float f = 0.1f + 0.2f; if (f == 0.3f) is FALSE due to IEEE-754 precision rounding. Use fabs(f - 0.3f) < 0.0001 instead!
Run this positive/negative number and voting eligibility checker in our online C compiler:
#include <stdio.h>
int main(void) {
int age = 19;
// Ternary operator expression
const char* status = (age >= 18) ? "ELIGIBLE to Vote" : "NOT Eligible to Vote";
printf("Age %d: %s\n", age, status);
return 0;
}