Control Flow
If, Else & Switch
Make decisions with if, else if, nested conditions and switch — with the classic grade, leap-year and calculator programs.
Programs become useful when they can decide. C runs a block of code only when a condition is true (any non-zero value) and skips it when the condition is false (zero).
if
#include <stdio.h>
int main(void) {
int age;
printf("Age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You can vote.\n");
}
printf("Thanks for checking!\n");
return 0;
}Age: 17
Thanks for checking!The braces can be dropped when the block has exactly one statement, but keeping them avoids bugs when you later add a second line.
if...else
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0)
printf("%d is even\n", n);
else
printf("%d is odd\n", n);
return 0;
}Enter a number: -7
-7 is oddelse if ladders
Check conditions top to bottom; the first true one wins and the rest are skipped.
#include <stdio.h>
int main(void) {
int marks;
printf("Marks out of 100: ");
scanf("%d", &marks);
if (marks < 0 || marks > 100)
printf("Invalid marks\n");
else if (marks >= 90)
printf("Grade O (Outstanding)\n");
else if (marks >= 80)
printf("Grade A+\n");
else if (marks >= 70)
printf("Grade A\n");
else if (marks >= 60)
printf("Grade B+\n");
else if (marks >= 40)
printf("Grade P (Pass)\n");
else
printf("Grade F (Fail)\n");
return 0;
}Marks out of 100: 83
Grade A+Nested if: the leap-year program
A year is a leap year if it is divisible by 4, except century years, which must be divisible by 400. This is one of the most-asked programs in CSE 101 exams.
#include <stdio.h>
int main(void) {
int year;
printf("Year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
} else {
printf("%d is a leap year\n", year);
}
} else {
printf("%d is not a leap year\n", year);
}
return 0;
}Year: 1900
1900 is not a leap yearThe same logic fits in one condition: (year % 4 == 0 && year % 100 != 0) || year % 400 == 0.
switch
When you compare one integer or character against many fixed values, switch is cleaner than a long else if ladder:
#include <stdio.h>
int main(void) {
double a, b;
char op;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
printf("Operator (+ - * /): ");
scanf(" %c", &op);
switch (op) {
case '+': printf("%.2f\n", a + b); break;
case '-': printf("%.2f\n", a - b); break;
case '*': printf("%.2f\n", a * b); break;
case '/':
if (b != 0) printf("%.2f\n", a / b);
else printf("Cannot divide by zero\n");
break;
default: printf("Unknown operator %c\n", op);
}
return 0;
}Enter two numbers: 12 4 Operator (+ - * /): / 3.00
Rules to remember:
- The
switchexpression must be an integer type (int,char,enum) — notfloator a string. - Each
caselabel must be a constant, and labels must be unique. breakexits the switch. Without it, execution falls through into the next case.defaultruns when no case matches. It is optional and can appear anywhere.
Fall-through is occasionally useful — for grouping cases:
#include <stdio.h>
int main(void) {
char ch;
printf("Letter: ");
scanf(" %c", &ch);
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("%c is a vowel\n", ch);
break;
default:
printf("%c is a consonant\n", ch);
}
return 0;
}Letter: e
e is a vowelTest yourself
4 questions · pick an answer to check it instantly
- 1
What does this print?
Cint x = 0; if (x = 0) printf("A"); else printf("B");x = 0assigns 0, and the value of the assignment (0) is false, so theelseruns. This is the classic=versus==bug. - 2
What happens if you leave out
breakat the end of acase?Withoutbreak, C keeps executing the following case's statements. - 3
Which of these cannot be used as a
switchexpression?switchonly works with integer types. Floating-point values are not allowed. - 4
Is 2000 a leap year?
Century years are leap years only when divisible by 400. 2000 qualifies; 1900 does not.