Control Flow & Decision Making
Make your programs smart! Use if-else to make decisions and switch for multiple choices. Programs can now respond differently to different situations.
Track Your Progress
Sign in to save your learning progress
What You Will Learn
- ✓Understand true/false in C (0 = false, non-zero = true)
- ✓Use if, if-else, and else-if statements
- ✓Master switch statement for multiple choices
- ✓Know when to use switch vs if-else
01What is Control Flow?
Simple Definition
Control flow lets your program make decisions. Instead of running every line from top to bottom, your program can choose which code to run based on conditions.
Real World Example
Is it raining?
YES
☂️ Take umbrella
NO
Wear sunglasses
This is exactly what if-else does in programming!
| Type | Purpose | Keywords |
|---|---|---|
| Decision Making | Choose which code to run | if, else, switch |
| Looping | Repeat code multiple times | for, while, do-while |
| Jump | Skip or exit code blocks | break, continue, goto |
This Tutorial Covers Decision Making
This tutorial focuses on if-else and switch statements. For loops (for, while, do-while), see the Loops Tutorial.
02Understanding True and False in C
Before using if statements, you must understand how C interprets true and false. Unlike some languages, C doesn't have a built-in bool type (before C99).
The Golden Rule in C
0
= FALSE
The condition is NOT met
Code block is SKIPPED
≠ 0
= TRUE
Any non-zero value
Code block EXECUTES
What Values Are True or False?
| Expression / Value | Numeric Value | Result | if() Behavior |
|---|---|---|---|
| 0 | 0 | FALSE | Block skipped |
| 1 | 1 | TRUE | Block executes |
| -1, -5, -100 | non-zero | TRUE | Block executes |
| 42, 100, 999 | non-zero | TRUE | Block executes |
| 0.0 (float) | 0 | FALSE | Block skipped |
| 3.14 (float) | non-zero | TRUE | Block executes |
| '\0' (null char) | 0 | FALSE | Block skipped |
| 'A' (any char) | 65 | TRUE | Block executes |
| NULL (pointer) | 0 | FALSE | Block skipped |
| &variable (address) | non-zero | TRUE | Block executes |
Comparison Expressions Return 0 or 1
When you use comparison operators (>, <,==, etc.), the result is always 1 (true) or 0 (false).
| Expression | Evaluates To | Meaning |
|---|---|---|
| 5 > 3 | 1 | TRUE (5 is greater than 3) |
| 5 < 3 | 0 | FALSE (5 is not less than 3) |
| 5 == 5 | 1 | TRUE (5 equals 5) |
| 5 != 5 | 0 | FALSE (5 is not different from 5) |
| 10 >= 10 | 1 | TRUE (10 is >= 10) |
| (5 > 3) && (2 < 1) | 0 | FALSE (true AND false = false) |
| (5 > 3) || (2 < 1) | 1 | TRUE (true OR false = true) |
▶ Try it: Demonstrates how C evaluates different values as true or false.
1#include <stdio.h>23int main() {4 // Demonstrating true/false values5 printf("=== True/False in C ===\n\n");6 7 // Zero is FALSE8 if (0) {9 printf("0: This won't print\n");10 } else {11 printf("0: FALSE (block skipped)\n");12 }13 14 // Non-zero is TRUE15 if (1) printf("1: TRUE\n");16 if (-5) printf("-5: TRUE (negative numbers are non-zero)\n");17 if (100) printf("100: TRUE\n");18 19 // Comparison results20 printf("\n=== Comparisons Return 0 or 1 ===\n");21 printf("5 > 3 = %d (TRUE)\n", 5 > 3);22 printf("5 < 3 = %d (FALSE)\n", 5 < 3);23 printf("5 == 5 = %d (TRUE)\n", 5 == 5);24 printf("5 != 5 = %d (FALSE)\n", 5 != 5);25 26 // You can store comparison results27 int isAdult = (25 >= 18); // isAdult = 128 printf("\n25 >= 18 stored in variable: %d\n", isAdult);29 30 return 0;31}=== True/False in C ===
0: FALSE (block skipped)
1: TRUE
-5: TRUE (negative numbers are non-zero)
100: TRUE
=== Comparisons Return 0 or 1 ===
5 > 3 = 1 (TRUE)
5 < 3 = 0 (FALSE)
5 == 5 = 1 (TRUE)
5 != 5 = 0 (FALSE)
25 >= 18 stored in variable: 1
Common Mistake: = vs ==
= is assignment, == is comparison. Writing if (x = 5) assigns 5 to x and is always TRUE (5 ≠ 0)!
if (x = 5) // WRONG! Assigns 5 to x, always TRUE if (x == 5) // CORRECT! Compares x to 5
03Comparison Operators
Before writing conditions, you need to know how to compare values. These operators compare two values and return 1 (true) or 0 (false).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | 1 (true) |
| != | Not equal to | 5 != 3 | 1 (true) |
| > | Greater than | 5 > 3 | 1 (true) |
| < | Less than | 5 < 3 | 0 (false) |
| >= | Greater than or equal | 5 >= 5 | 1 (true) |
| <= | Less than or equal | 3 <= 5 | 1 (true) |
Quick Example
#include <stdio.h>int main() { int a = 10, b = 5; printf("a = %d, b = %d\n", a, b); printf("a == b: %d\n", a == b); // 0 (false) printf("a != b: %d\n", a != b); // 1 (true) printf("a > b: %d\n", a > b); // 1 (true) printf("a < b: %d\n", a < b); // 0 (false) return 0;}a = 10, b = 5
a == b: 0
a != b: 1
a > b: 1
a < b: 0
Common Mistake: = vs ==
= (Assignment)
x = 5; // Sets x to 5
== (Comparison)
x == 5; // Is x equal to 5?
04Logical Operators
Logical operators let you combine multiple conditions.
| Operator | Name | Meaning | Example |
|---|---|---|---|
| && | AND | Both must be true | a > 0 && b > 0 |
| || | OR | At least one must be true | a > 0 || b > 0 |
| ! | NOT | Reverses true/false | !(a > 0) |
&& (AND)
Both must be TRUE
|| (OR)
At least one TRUE
! (NOT)
Reverses the value
In C:
!0 = 1, !1 = 0
Real World Examples
&& (AND) - Both conditions required
Can drive = age >= 18 && hasLicense
Must be 18+ AND have license
|| (OR) - Either condition works
Free entry = age < 5 || age > 65
Either under 5 OR over 65
Example: Combining Conditions
#include <stdio.h>int main() { int age = 25; int hasLicense = 1; // 1 = true // AND: Both conditions must be true if (age >= 18 && hasLicense) { printf("You can drive!\n"); } // OR: At least one condition must be true int isWeekend = 1; int isHoliday = 0; if (isWeekend || isHoliday) { printf("No work today!\n"); } // NOT: Reverses the condition int isRaining = 0; if (!isRaining) { printf("No umbrella needed.\n"); } return 0;}You can drive!
No work today!
No umbrella needed.
05If Statement
The if statement executes code only when the condition is true.
How if Works
Syntax
if (condition) {
// Code runs if condition is TRUE
}
1.Simple Example: Check Temperature
#include <stdio.h>int main() { int temp = 35; if (temp > 30) { printf("It's hot today!\n"); } printf("Done.\n"); return 0;}It's hot today!
Done.
Since 35 > 30 is true, the message prints.
2.Another Example: Check if Positive
#include <stdio.h>int main() { int num = 10; if (num > 0) { printf("%d is positive\n", num); } return 0;}10 is positive
06If-Else Statement
Add else to handle the case when the condition is FALSE. Now you have two paths - one for true, one for false.
How if-else Works
1.Check Even or Odd
#include <stdio.h>int main() { int num = 7; if (num % 2 == 0) { printf("%d is EVEN\n", num); } else { printf("%d is ODD\n", num); } return 0;}7 is ODD
% is the modulo operator (gives remainder). 7 % 2 = 1, not 0, so it's odd.
2.Check Pass or Fail
#include <stdio.h>int main() { int marks = 55; if (marks >= 40) { printf("Result: PASS\n"); } else { printf("Result: FAIL\n"); } return 0;}Result: PASS
3.Check Voting Eligibility
#include <stdio.h>int main() { int age = 16; if (age >= 18) { printf("You can vote!\n"); } else { printf("Too young to vote.\n"); printf("Wait %d more years.\n", 18 - age); } return 0;}Too young to vote.
Wait 2 more years.
07Else-If Ladder
When you have multiple conditions to check, use an else-if ladder. Conditions are evaluated top-to-bottom; only the first true condition executes.
Syntax
if (condition1) {
// Runs if condition1 is TRUE
}
else if (condition2) {
// Runs if condition1 is FALSE and condition2 is TRUE
}
else if (condition3) {
// Runs if condition1, condition2 are FALSE and condition3 is TRUE
}
else {
// Runs if ALL conditions are FALSE
}▶ Try it: Converts marks to letter grades using multiple conditions.
1#include <stdio.h>23int main() {4 int marks = 73;5 char grade;6 7 printf("Marks: %d\n", marks);8 9 if (marks >= 90) {10 grade = 'A';11 printf("Excellent work!\n");12 }13 else if (marks >= 80) {14 grade = 'B';15 printf("Great job!\n");16 }17 else if (marks >= 70) {18 grade = 'C';19 printf("Good effort!\n");20 }21 else if (marks >= 60) {22 grade = 'D';23 printf("You passed.\n");24 }25 else {26 grade = 'F';27 printf("Need improvement.\n");28 }29 30 printf("Grade: %c\n", grade);31 32 return 0;33}Marks: 73
Good effort!
Grade: C
Order Matters!
With 73 marks: We check marks >= 90 (false), then marks >= 80 (false), then marks >= 70 (TRUE!). The rest are skipped.
08Nested If Statements
You can place an if inside another if to check multiple related conditions. This is called nesting.
▶ Try it: Checks if a person is eligible to vote (must be a citizen AND at least 18).
1#include <stdio.h>23int main() {4 int age = 20;5 int isCitizen = 1; // 1 = true, 0 = false6 7 printf("Age: %d, Citizen: %s\n", age, isCitizen ? "Yes" : "No");8 9 if (isCitizen) {10 // Only check age if they're a citizen11 if (age >= 18) {12 printf("✓ You can vote!\n");13 }14 else {15 printf("✗ Too young to vote (must be 18+).\n");16 }17 }18 else {19 printf("✗ Only citizens can vote.\n");20 }21 22 return 0;23}Age: 20, Citizen: Yes
✓ You can vote!
Simplify with Logical AND
Nested ifs can often be simplified using &&:
// Instead of nesting:
if (isCitizen && age >= 18) {
printf("You can vote!\n");
}09Ternary Operator (? :)
The ternary operator is a shorthand for simple if-else statements. It's called "ternary" because it takes three operands.
Syntax
If condition is true → returns first value, else → returns second value
Using if-else (4 lines)
int max;if (a > b) { max = a;} else { max = b;}Using ternary (1 line)
int max = (a > b) ? a : b;Same result, much shorter!
Example: Find Maximum
#include <stdio.h>int main() { int a = 10, b = 20; int max = (a > b) ? a : b; printf("Max of %d and %d is %d\n", a, b, max); return 0;}Max of 10 and 20 is 20
Example: Even/Odd String
#include <stdio.h>int main() { int num = 7; // Ternary in printf printf("%d is %s\n", num, (num % 2 == 0) ? "even" : "odd"); return 0;}7 is odd
Example: Absolute Value
#include <stdio.h>int main() { int num = -5; int abs_val = (num >= 0) ? num : -num; printf("Absolute value of %d is %d\n", num, abs_val); return 0;}Absolute value of -5 is 5
When to Use Ternary
- • Use for simple one-liner assignments
- • Use inside printf for dynamic strings
- • Avoid for complex logic (hard to read)
- • Avoid nesting ternary operators
10Switch Statement
The switch statement selects one of many code blocks based on the value of an expression. It is cleaner than multiple if-else when comparing one variable to many constant values.
Syntax
switch (expression) {
case value1:
// code for value1
break; // Exit switch
case value2:
// code for value2
break;
default:
// code if no case matches
}| Component | Description |
|---|---|
| switch(expr) | Expression must be int or char (integer types) |
| case value: | Must be a constant (not variable), unique values only |
| break; | Exits switch. Without it, execution "falls through" to next case |
| default: | Optional. Runs if no case matches (like else) |
▶ Try it: Displays the day name based on a number (1-7).
1#include <stdio.h>23int main() {4 int day = 3;5 6 printf("Day number: %d\n", day);7 printf("Day name: ");8 9 switch (day) {10 case 1:11 printf("Monday\n");12 break;13 case 2:14 printf("Tuesday\n");15 break;16 case 3:17 printf("Wednesday\n");18 break;19 case 4:20 printf("Thursday\n");21 break;22 case 5:23 printf("Friday\n");24 break;25 case 6:26 printf("Saturday\n");27 break;28 case 7:29 printf("Sunday\n");30 break;31 default:32 printf("Invalid day (use 1-7)\n");33 }34 35 return 0;36}Day number: 3
Day name: Wednesday
Don't Forget break!
Without break, execution falls through to the next case. This is sometimes intentional, but usually a bug.
Intentional Fall-Through
Sometimes fall-through is useful to group multiple cases with the same code:
▶ Try it: Uses fall-through to group weekdays and weekends.
1#include <stdio.h>23int main() {4 int day = 6;5 6 printf("Day %d is a ", day);7 8 switch (day) {9 case 1:10 case 2:11 case 3:12 case 4:13 case 5:14 printf("Weekday\n");15 break;16 case 6:17 case 7:18 printf("Weekend!\n");19 break;20 default:21 printf("Invalid day\n");22 }23 24 return 0;25}Day 6 is a Weekend!
11Switch vs If-Else: When to Use Which
| Criteria | switch | if-else |
|---|---|---|
| Comparison Type | Exact value match only | Any condition (>, <, &&, etc.) |
| Data Types | int, char (integer types) | Any type (int, float, pointers) |
| Case Values | Must be constants | Can use variables |
| Range Checking | Not supported | Supported (x > 10 && x < 20) |
| Readability | Better for many exact values | Better for complex conditions |
| Performance | Often optimized to jump table | Evaluated sequentially |
✓Use switch for:
- • Menu options (1, 2, 3, 4...)
- • Day of week (1-7)
- • Month selection
- • Character commands ('a', 'b', 'c')
- • Enum values
✓Use if-else for:
- • Range checks (marks >= 90)
- • Boolean conditions
- • Comparing floats
- • Complex expressions
- • Variable comparisons
12Jump Statements: break, continue, goto
Jump statements transfer control to another part of the program. They interrupt the normal sequential flow of execution.
| Statement | Purpose | Used In | Effect |
|---|---|---|---|
| break | Exit immediately | loops, switch | Stops execution, exits the block |
| continue | Skip current iteration | loops only | Jumps to next iteration |
| goto | Jump to label | anywhere in function | Unconditional jump |
The break Statement
break immediately exits the innermost loop or switch statement. Execution continues with the statement following the loop/switch.
When to Use break
- ✓Exit a switch case (required to prevent fall-through)
- ✓Stop searching when you find what you need
- ✓Exit an infinite loop when a condition is met
- ✓Handle error conditions that require early exit
▶ Try it: Searches for a target value and exits the loop immediately when found.
1#include <stdio.h>23int main() {4 int numbers[] = {10, 25, 33, 47, 56, 68, 72};5 int size = 7;6 int target = 47;7 int found = 0;8 9 printf("Searching for %d...\n", target);10 11 for (int i = 0; i < size; i++) {12 printf("Checking index %d: %d\n", i, numbers[i]);13 14 if (numbers[i] == target) {15 printf(">>> Found %d at index %d!\n", target, i);16 found = 1;17 break; // Exit loop immediately - no need to check rest18 }19 }20 21 if (!found) {22 printf("Value not found.\n");23 }24 25 printf("Search complete.\n");26 return 0;27}Searching for 47...
Checking index 0: 10
Checking index 1: 25
Checking index 2: 33
Checking index 3: 47
>>> Found 47 at index 3!
Search complete.
The continue Statement
continue skips the rest of the current iteration and jumps to the next iteration of the loop. The loop itself continues running.
When to Use continue
- ✓Skip invalid or unwanted data
- ✓Process only items matching criteria (filter)
- ✓Avoid deep nesting by handling edge cases early
break vs continue
break
Loop: 1 2 3 [break] done
Exits loop completely
continue
Loop: 1 2 [skip] 4 5 done
Skips to next iteration
▶ Try it: Prints only positive numbers from an array, skipping negative ones.
1#include <stdio.h>23int main() {4 int numbers[] = {5, -3, 10, -7, 8, -2, 15};5 int size = 7;6 int sum = 0;7 8 printf("Processing numbers (skipping negatives):\n");9 10 for (int i = 0; i < size; i++) {11 // Skip negative numbers12 if (numbers[i] < 0) {13 printf("Skipping %d (negative)\n", numbers[i]);14 continue; // Jump to next iteration15 }16 17 // This code only runs for positive numbers18 printf("Adding %d\n", numbers[i]);19 sum += numbers[i];20 }21 22 printf("\nSum of positive numbers: %d\n", sum);23 return 0;24}Processing numbers (skipping negatives):
Adding 5
Skipping -3 (negative)
Adding 10
Skipping -7 (negative)
Adding 8
Skipping -2 (negative)
Adding 15
Sum of positive numbers: 38
The goto Statement
goto performs an unconditional jump to a labeled statement anywhere within the same function.
Syntax
goto label_name; // Jump to label
// ... other code ...
label_name: // Label definition (ends with :)
// Code continues hereWhy goto is Controversial
goto is considered bad practice by most programmers because:
- • Creates "spaghetti code" that's hard to read and maintain
- • Makes program flow unpredictable
- • Can be replaced by structured constructs (loops, functions)
- • Makes debugging difficult
✓Acceptable Uses of goto
There are a few cases where goto can make code cleaner:
- 1. Breaking out of deeply nested loops - When you need to exit multiple loops at once
- 2. Centralized cleanup code - Jumping to cleanup/error handling at end of function
- 3. State machines - Some state machine implementations
Acceptable use: Breaking out of nested loops to find a target in a 2D grid.
1#include <stdio.h>23int main() {4 int grid[3][3] = {5 {1, 2, 3},6 {4, 5, 6},7 {7, 8, 9}8 };9 int target = 5;10 int found_row = -1, found_col = -1;11 12 printf("Searching for %d in 3x3 grid...\n\n", target);13 14 // Nested loops - break would only exit inner loop15 for (int i = 0; i < 3; i++) {16 for (int j = 0; j < 3; j++) {17 printf("Checking [%d][%d] = %d\n", i, j, grid[i][j]);18 19 if (grid[i][j] == target) {20 found_row = i;21 found_col = j;22 goto found; // Exit BOTH loops at once23 }24 }25 }26 27 // Only reaches here if not found28 printf("\nValue not found in grid.\n");29 goto end;30 31found:32 printf("\n>>> Found %d at position [%d][%d]!\n", target, found_row, found_col);33 34end:35 printf("Search complete.\n");36 return 0;37}Searching for 5 in 3x3 grid...
Checking [0][0] = 1
Checking [0][1] = 2
Checking [0][2] = 3
Checking [1][0] = 4
Checking [1][1] = 5
>>> Found 5 at position [1][1]!
Search complete.
Another acceptable use: Centralized error handling and resource cleanup.
1#include <stdio.h>2#include <stdlib.h>34int main() {5 FILE *file = NULL;6 char *buffer = NULL;7 int result = 0;8 9 // Allocate memory10 buffer = malloc(100);11 if (buffer == NULL) {12 printf("Error: Memory allocation failed\n");13 goto cleanup; // Jump to cleanup14 }15 16 // Open file17 file = fopen("data.txt", "r");18 if (file == NULL) {19 printf("Error: Cannot open file\n");20 goto cleanup; // Jump to cleanup21 }22 23 // Process file...24 printf("Processing file...\n");25 result = 1; // Success26 27cleanup: // Single cleanup point for all exits28 if (file != NULL) {29 fclose(file);30 printf("File closed.\n");31 }32 if (buffer != NULL) {33 free(buffer);34 printf("Memory freed.\n");35 }36 37 return result ? 0 : 1;38}Error: Cannot open file
Memory freed.
Alternatives to goto
In most cases, you can avoid goto using:
- • Flag variables - Use a boolean to break out of nested loops
- • Functions - Put nested loops in a function and use return
- • break + flag - Set a flag before break, check in outer loop
!Code Pitfalls: Common Mistakes & What to Watch For
These are the most common mistakes that trip up beginners. Study them carefully to avoid hours of debugging!
Using = instead of == in conditions
This is the most common bug! = assigns a value,== compares values.
// WRONG - assigns 5 to x, condition is always TRUE (5 != 0)if (x = 5) { printf("This always runs!\n");}// CORRECT - compares x to 5if (x == 5) { printf("x is 5\n");}Forgetting break in switch
Without break, execution falls through to the next case!
// WRONG - falls through to next casesswitch (day) { case 1: printf("Monday\n"); // Missing break! Falls through... case 2: printf("Tuesday\n"); // Also prints!}// CORRECTswitch (day) { case 1: printf("Monday\n"); break; // Exits switch case 2: printf("Tuesday\n"); break;}Semicolon after if condition
A semicolon after if() creates an empty statement!
// WRONG - semicolon makes if do nothingif (x > 5); // Empty statement!{ printf("This ALWAYS runs!\n"); // Not part of if}// CORRECTif (x > 5) { printf("x is greater than 5\n");}Dangling else problem
Without braces, else binds to the nearest if.
// CONFUSING - which if does else belong to?if (a > 0) if (b > 0) printf("a and b positive\n");else printf("This runs when b <= 0, NOT when a <= 0!\n");// CLEAR - use braces to show intentif (a > 0) { if (b > 0) { printf("a and b positive\n"); }} else { printf("a is not positive\n");}Using switch with non-constant cases
Switch case values must be compile-time constants, not variables.
int option = 2;// WRONG - case values must be constantsswitch (choice) { case option: // ERROR! Variables not allowed break;}// CORRECT - use constant valuesswitch (choice) { case 2: // Literal constant - OK break; case MAX_VALUE: // #define constant - OK break;}Wrong order in else-if ladder
More specific conditions should come first; otherwise, they're never reached.
int score = 95;// WRONG - first condition catches everything >= 60if (score >= 60) { printf("D\n"); // 95 matches here!} else if (score >= 70) { printf("C\n"); // Never reached for 95} else if (score >= 90) { printf("A\n"); // Never reached}// CORRECT - most specific firstif (score >= 90) { printf("A\n"); // 95 matches correctly} else if (score >= 80) { printf("B\n");} else if (score >= 60) { printf("D\n");}Using floats in switch
Switch only works with integer types (int, char, enum). Use if-else for floats.
float grade = 3.5;// WRONG - float not allowed in switchswitch (grade) { // ERROR! case 4.0: break;}// CORRECT - use if-else for floatsif (grade >= 4.0) { printf("A\n");} else if (grade >= 3.0) { printf("B\n");}Watch Out When Copying Code!
Why copied Conditionals Need Your Review!
When you ask someone to write if-else or switch statements, it often generates code that looks correct but has subtle logical flaws:
1. Incomplete Edge Cases
Copied code might write a grade calculator that handles A, B, C, D but forgets what happens when score is negative or above 100.
// copied - looks fine but incompleteif (score >= 90) printf("A");else if (score >= 80) printf("B");// What if score = -5 or score = 150?2. Missing Break in Switch
Beginners sometimes generates switch statements with intentional fall-through but doesn't comment it, or accidentally forgets break statements.
3. Condition Order Bugs
Copied code might place score >= 60 before score >= 90, meaning a score of 95 would match the first condition (60+) and never reach the A grade check.
Your Job: Always trace through copied conditionals with extreme values (0, -1, max, boundary values) to catch logic errors that "look right" but aren't.
15Frequently Asked Questions
Q:When should I use switch instead of if-else?
A: Use switch when comparing ONE variable against multiple exact values (like menu choices, day numbers, character codes). Use if-else for ranges, multiple conditions, floating-point comparisons, or string comparisons. Switch is often cleaner for >3 exact-match cases.
Q:Why do I need braces if there's only one statement after if?
A: You don't technically need them for single statements, but you should always use them anyway. Why? When you add a second line later, forgetting to add braces causes a bug where only the first line runs conditionally. This "Apple SSL bug" actually crashed iOS security in 2014! Always use braces — it costs nothing and prevents disasters.
Q:What's the difference between = and == again?
A: = is assignment (put value into variable). == is comparison (check if equal). Writing if (x = 5) assigns 5 to x, then evaluates to true (since 5 ≠ 0). This is the #1 C bug. Tip: Write if (5 == x) — if you accidentally use =, the compiler catches it since you can't assign to 5.
Q:What happens if I forget break in a switch case?
A: Execution "falls through" to the next case! If case 1 matches but has no break, it runs case 1's code, then case 2's code, then case 3's, etc. until hitting a break or the switch ends. This is sometimes useful (grouping cases), but usually a bug. Modern compilers warn about this with -Wimplicit-fallthrough.
Q:Can I use strings in a switch statement?
A: No! Switch only works with integer types:int, char,enum, short,long. For strings, you must use if-else with strcmp(). Note: char works because characters are integers (ASCII codes), so case 'A': meanscase 65:.
Q:What does the ternary operator ?: do?
A: It's a compact if-else in one line:condition ? value_if_true : value_if_false. Example:int max = (a > b) ? a : b; assigns the larger of a or b to max. Use it for simple value selection, not complex logic. Nested ternaries become unreadable — prefer if-else for anything complex.
Q:Why does if(x) work when x is a number, not a boolean?
A: In C, there's no built-in boolean type (until C99's stdbool.h). Instead, C uses integers: 0 = false,any non-zero = true. So if(5) is true,if(-1) is true, if(0) is false. This is why if(x = 5) evaluates as true — the assignment returns 5, which is non-zero!
16Summary
Key Takeaways
- •TRUE/FALSE: 0 = false, any non-zero = true
- •Comparison: ==, !=, >, <, >=, <= (return 1 or 0)
- •Logical: && (AND), || (OR), ! (NOT)
- •if: Execute code only when condition is true
- •if-else: Two paths - one for true, one for false
- •else-if: Check multiple conditions in order
- •Ternary:
condition ? valueIfTrue : valueIfFalse - •switch: Match exact values (int/char), always use break!
- •break: Exit loop/switch immediately
- •continue: Skip to next loop iteration
- •= vs ==: Assignment vs comparison - #1 bug source!
Quick Reference
Decision Statements
if (condition) { ... }
if (cond) { ... } else { ... }
switch (var) { case 1: ... break; }
Operators
== != > < >= <= (comparison)
&& || ! (logical)
?: (ternary)
Test Your Knowledge
Related Tutorials
Loops in C
Repeat actions automatically! Master for, while, and do-while loops. Learn which loop to use when, and how to control loops with break and continue.
Operator Precedence & Associativity
Understand which operators are evaluated first. Learn the PUMAS REBL TAC memory trick and when to use parentheses.
Keywords in C
Learn all 32 reserved words in C that have special meaning. These words are reserved by C and cannot be used as variable names.
Have Feedback?
Found something missing or have ideas to improve this tutorial? Let us know on GitHub!