Skip to main content
Advertisement

C: Conditional Statements

Control statements that allow different code blocks to execute based on specific conditions.

1. if / else

int score = 85;

if (score >= 90) {
printf("Grade A\n");
} else if (score >= 80) {
printf("Grade B\n");
} else {
printf("Other\n");
}

2. switch / case

Used when selecting a block of code to execute among many alternatives based on a specific value.

int day = 2;

switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
default:
printf("Other Day\n");
}

info

Be careful: if you omit the break statement in a switch block, the execution will "fall through" to the subsequent case.

Advertisement