C Language
4. Control Flow & LoopsBeginner8 min read

Decision Making & Control Statements in C

Control execution flow using if, if-else, nested if-else, else-if ladders, and switch-case statements with break & default.

Key Takeaways & Core Concepts
  • if statements evaluate expressions: any non-zero value is treated as true, 0 is false.
  • switch-case is optimized by compilers into jump tables for fast multi-way branching.
  • The break statement prevents fallthrough to subsequent case blocks in a switch.
  • The default block handles any value that doesn't match a listed case.

Conditional Statements (if, if-else, else-if)

In C, conditionals evaluate any expression to either 0 (False) or non-zero (True).

C Code Example
1if (condition) {
2 // executed when condition != 0
3} else if (another_condition) {
4 // executed when first is 0 and this is != 0
5} else {
6 // executed when all conditions are 0
7}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
7 lines
Accidental Assignment in If Conditions

Writing `if (x = 5)` instead of `if (x == 5)` assigns 5 to x! Because 5 is non-zero, this condition is always true. Pro tip: Use Yoda conditions like `if (5 == x)` so the compiler errors out if you accidentally type a single `=`.

The switch-case Statement

The switch statement provides an elegant alternative to lengthy else-if chains when testing an integer or character variable against multiple constant values.

Clean Switch-Case Example with Fallthrough Handling
1#include <stdio.h>
2
3int main(void) {
4 char tier = 'B';
5
6 printf("CodeZov Student Tier Evaluation:\n");
7 switch (tier) {
8 case 'A':
9 case 'a':
10 printf("Tier: Exceptional! You have access to Advanced Agentic AI projects.\n");
11 break;
12 case 'B':
13 case 'b':
14 printf("Tier: Proficient! Ready for Systems & Kernel programming.\n");
15 break;
16 case 'C':
17 case 'c':
18 printf("Tier: Developing. Review Data Structures & Pointers.\n");
19 break;
20 default:
21 printf("Invalid tier code: %c\n", tier);
22 break;
23 }
24
25 return 0;
26}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
26 lines
Note: Notice how case 'B' and case 'b' share logic through intentional fallthrough, terminated by the 'break' statement.
Practice & AssessmentC Programming Complete Course

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 1 correct(0 attempted)

1

What happens if you omit the 'break' statement in a switch-case block?

Beginner