Nearby lessons

38 of 124

C - if...else Statement

if...else Statement is one of the foundational topics in C programming. This lesson explains The if-else Statement with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The if-else Statement

Example01
CCode Cell
1if (condition) {
2 // runs when true
3} else {
4 // runs when false
5}

The if-else Statement

In simple words: if-else is a fork in the road — exactly two paths. The if path runs when the condition is true, the else path runs when it is false. One of the two always runs, never both.
Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int age;
6 printf("Enter age: ");
7 scanf("%d", &age);
8 
9 if (age >= 18) {
10 printf("Eligible to vote\n");
11 } else {
12 printf("Not eligible to vote\n");
13 }
14}
Output
Enter age: 20 Eligible to vote
📝 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