If-Else Statements in C: Basic, Advanced and Nested Interview Questions with Worked Answers
Learn how C selects branches, trace four calls to classify, spot assignment and dangling-else traps, and flatten deep nesting without losing behaviour.
KnowledgeGate Team
Exam prep & CS education

Defining if and else is easy. In an interview, one changed operator, missing braces, reordered else if ladder or nested decision can force you to prove which expressions are evaluated, which branch runs, how state changes and what prints. These 14 speakable model answers include one four-input trace that prints 13 3 1 -1, plus a repeatable solving routine; use C Programming Interview Questions for Freshers for the wider fundamentals-first and depth-oriented interview set, knowing that real employers can mix both styles.
if, if-else, else if and nested if: the syntax map
Question 1: What does an if statement do in C? C evaluates the controlling expression: zero is false, any nonzero true. An if controls one statement, possibly a braced compound statement; it need not receive a Boolean object.
Question 2: When should I use if, if-else and an else if ladder? if handles an optional action, if-else chooses one of two paths, and a connected ladder runs its first true branch, skipping the rest. else if is an else followed by another if, not a C keyword. int temperature = 32; if (temperature > 30) puts("Hot"); else puts("Not hot"); prints Hot.
Question 3: What is a nested if? It is an if inside an already selected branch. if (logged_in) { if (is_admin) ... } preserves separate outer and inner outcomes; if (logged_in && is_admin) ... creates one gate. See Conditionals in C for examples.
Fully worked nested trace: four inputs and four exact returns
Question 4: Trace every branch and give the exact output. Start each call at score = 0 and follow only its selected path.
int classify(int x, int y) {
int score = 0;
if (x > 0 && y > 0) {
score = x + y;
if (score >= 10) {
score += 2;
} else {
score -= 2;
}
} else if (x == 0 || y == 0) {
score = 1;
} else {
score = -1;
}
return score;
}
printf("%d %d %d %d\n",
classify(4, 7), classify(3, 2),
classify(0, 8), classify(-2, 5));input | outer condition | selected path | score transitions | return |
|---|---|---|---|---|
| true | positive branch, nested true |
|
|
| true | positive branch, nested false |
|
|
| false, zero test true | else-if |
|
|
| false, zero test false | final else |
|
|
The exact output is 13 3 1 -1. Success in the first condition skips its connected else if and else; only failures reach the zero test. score >= 10 runs only after both inputs are positive and their sum is assigned.

Basic output questions: truth values, assignment and connected branches
Question 5: Are if (5) and if (-3) valid? Yes. Both are nonzero and therefore true; if (0) is false. A C condition need not contain only 0 or 1.
Question 6: What happens when assignment appears inside a condition? In int flag = 0; if (flag = 5) printf("T "); printf("%d\n", flag);, assignment stores and yields nonzero 5, so the exact output is T 5. flag == 5 would compare and leave the initial value unchanged. Assignment here is legal C, but likely a bug.
Question 7: How do two independent if statements differ from an if/else if ladder? With int n = 12;, two independent tests for n > 5 and n > 10 print AB. Change the second to else if, and only A prints because the first true branch owns the ladder. Identify connectivity before evaluating values.
Advanced conditions: short-circuiting and ladder order
Question 8: How do && and || short-circuit inside an if? With int a = 0, b = 5; if (a != 0 && ++b > 5) puts("T");, the false left operand skips ++b; nothing prints and final b = 5. Reset to a = 2, b = 5; ++b makes b = 6, the condition succeeds and T prints. In int divisor = 0; if (divisor != 0 && 24 / divisor > 2) ..., division is skipped. Short-circuiting is a semantic guarantee that prevents invalid work, not merely a speed trick.
Question 9: Why does the order of an else if ladder matter? With int score = 85; if (score >= 40) puts("Pass"); else if (score >= 75) puts("Distinction");, the output is Pass. The narrower branch is unreachable because its values satisfy the broader first test. Test score >= 75 first, then score >= 40, then else; 85 now prints Distinction. Put specific overlapping ranges first and test exact boundaries.
Nested if and the dangling else: braces reveal the branch tree
Question 10: Which if owns this else, and what prints?
int x = 5;
int y = 8;
if (x > 0)
if (y < 5)
puts("A");
else
puts("B");The else belongs to the nearest unmatched if, which is if (y < 5). Since x > 0 is true and y < 5 is false, the exact output is B.
Question 11: Can every nested if be replaced by &&? No. Without an inner else, two nested tests can often become if (x > 0 && y < 5) action();, preserving left-to-right short-circuit order. Here the inner false branch matters. Braces around the original structure preserve output B. Move the else to the outer if, and x = 5, y = 8 prints nothing. Use braces to expose ownership.

Refactoring deep nesting and proving every result path
Question 12: How would you make a three-level nested decision easier to read? Use guard clauses when they make each outcome clearer and keep the main path flat:
const char *level(int logged_in, int age, int score) {
if (!logged_in) return "DENY";
if (age < 18) return "MINOR";
if (score >= 80) return "PRIORITY";
return "STANDARD";
}Each return completes one result path. Early returns are useful only when every outcome becomes clearer and remains easy to verify.
Question 13: Which tests prove that every branch works? Use one representative input per result, then test exact boundaries and values just below them:
level(0, 25, 95) -> DENYlevel(1, 16, 90) -> MINORlevel(1, 24, 85) -> PRIORITYlevel(1, 24, 60) -> STANDARDlevel(1, 18, 80) -> PRIORITYlevel(1, 18, 79) -> STANDARD
This rewrite exposes missing braces, wrong else ownership, badly ordered checks, missing returns and happy-path-only tests.
How interviews probe if-else depth and how to answer an unseen trace
Question 14: How should I solve an unfamiliar if-else output question aloud? Use four passes: (1) write starting values and side effects; (2) evaluate only the current condition with short-circuiting; (3) enter one connected ladder branch and cross out the rest; (4) record changes before the next nested test or final output.
For classify(-2, 5), the first and zero tests fail. The final else sets score = -1 and returns -1.
A fundamentals-first prompt may test syntax, truth values and = versus ==; a depth-oriented follow-up may add side effects, dangling else, overlapping ranges, refactoring and boundaries. Service-based and product-based employers may mix either depth. The practice bank has over 40 questions. Use Pseudocode Questions for Placements for more branch-state tracing.
The 60-second if-else answer and next step
Zero is false and nonzero is true; if-else chooses one of two paths; a connected ladder stops at its first true branch, while independent if statements all test. && and || short-circuit, overlapping ranges need careful order, else binds to the nearest unmatched if, and braces plus boundary tests reveal the tree.
Reproduce 13 3 1 -1, T 5, AB versus A, b = 5, b = 6, B, and all six level results. Continue with the C Language Course, or browse C learning modules.
Keep learning
If-Else Statements in C: Basic, Nested and Advanced Control Flow with Worked Traces
Learn how C chooses a branch, then trace ladders, nested decisions, short-circuit expressions and common output traps with exact values.
If-Else Statements in C MCQs: 11 Basic, Advanced and Nested Questions with Explanations
Test C control-flow skills with 11 published questions and step-by-step explanations of simple, nested and expression-based decisions.
Python Conditional Statements Explained: if, elif, else, Nested Logic and Worked Examples
Learn a reliable way to trace Python branches, from Boolean expressions and short-circuiting to nested decisions, truthiness, boundary checks, and a complete scholarship classifier.
Control Flow in C Programming: If-Else, Loops, Switch and Jump Statements with Worked Examples
Learn how C chooses, repeats and redirects execution. Trace common output questions step by step, including do-while, continue, switch fall-through and dangling else.





