Nearby lessons

40 of 124

C - Nested if Statements

Nested if Statements is one of the foundational topics in C programming. This lesson explains Nested if and Program: check result and attendance together with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Nested if

An if inside another if is called a nested if. It is useful when one condition only matters after another is true.

Program: check result and attendance together

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks = 55, attendance = 80;
6 
7 if (marks >= 40) {
8 if (attendance >= 75) {
9 printf("Pass with good attendance\n");
10 } else {
11 printf("Pass but short attendance\n");
12 }
13 } else {
14 printf("Fail\n");
15 }
16}
Output
Pass with good attendance
📝 Key Takeaways
  • if runs a block when the condition is true; else covers the false case.
  • if-else-if ladder chooses the first true condition.
  • Nested if = an if inside another if.
  • switch jumps to a matching case; remember break and default.
  • switch works with int/char, not float or string.
  • Ternary ?: is a one-line if-else.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1