Programs need to choose different actions based on conditions, such as passing or failing a student. C gives you if, else, else-if ladders, switch and the ternary operator.
How C Programs Choose Between Paths
What is Decision Making?
Decision making in programming refers to the ability of a program to choose between different execution paths based on conditions. In C, this is achieved through conditional statements that evaluate expressions and execute specific blocks of code accordingly.
Key concepts:
- Condition: An expression that evaluates to true (non-zero) or false (zero).
- Branching: Diverting program flow based on condition results.
- Blocks: Groups of statements enclosed in
{ }that execute together.
Why Decision Making Matters
- Program control: Directs execution flow based on user input, data values, or system state.
- Flexibility: Programs can respond differently to different situations.
- Error handling: Check for invalid conditions before proceeding.
- Logic implementation: Implement complex algorithms and business rules.
Types of Decision-Making Statements
C provides several decision-making constructs:
| Statement | Purpose |
|---|---|
if | Execute block if condition is true |
if-else | Execute one block if true, another if false |
Nested if-else | Multiple levels of conditions inside conditions |
if-else-if ladder | Test multiple conditions sequentially |
switch | Select from multiple cases based on value |
Conditional operator ?: | Shorthand for simple if-else |
Boolean Representation in C
C does not have a built-in bool type in C89/C90. Conditions are evaluated as integers:
- 0 = false
- Non-zero (usually 1) = true
C99 introduced _Bool type (via <stdbool.h> as bool with true/false).

if, else if and switch Syntax
1. if Statement
The simplest decision-making statement. Executes a block only when the condition is true.
Syntax:
if (condition) {
// statements
}
Example:
#include <stdio.h>
int main(void) {
int age = 20;
if (age >= 18) {
printf("Eligible for vote\n");
}
/* Single statement without braces */
if (age > 0)
printf("Age is positive\n");
return 0;
}
Expected output:
Eligible for vote Age is positive
Important: If there’s only one statement in the body, braces can be omitted. However, using braces consistently is recommended to avoid bugs.
2. if-else Statement
Executes one block if condition is true, another if false.
Syntax:
if (condition) {
// executed if condition is true
} else {
// executed if condition is false
}
Example:
#include <stdio.h>
int main(void) {
int age = 10;
if (age >= 18) {
printf("Eligible for vote\n");
} else {
printf("Not Eligible for vote\n");
}
return 0;
}
Expected output:
Not Eligible for vote
3. Nested if-else Statement
An if or if-else statement inside another if or else block.
Syntax:
if (condition1) {
if (condition2) {
// executed if both condition1 and condition2 are true
} else {
// executed if condition1 true, condition2 false
}
} else {
// executed if condition1 is false
}
Example:
#include <stdio.h>
int main(void) {
int age = 11;
if (age >= 18) {
if (age >= 60) {
printf("Eligible to vote (Senior Citizen)\n");
} else {
printf("Eligible for vote\n");
}
} else {
printf("Not eligible to vote (Under 18)\n");
if (age >= 13) {
printf("Teenager\n");
} else {
printf("Not a teenager\n");
}
}
return 0;
}
Expected output:
Not eligible to vote (Under 18) Not a teenager
4. if-else-if Ladder
Tests multiple conditions sequentially. The first true condition’s block executes.
Syntax:
if (condition1) {
// executed if condition1 is true
} else if (condition2) {
// executed if condition1 false, condition2 true
} else if (condition3) {
// executed if condition1 and condition2 false, condition3 true
} else {
// executed if all conditions are false
}
Example:
#include <stdio.h>
int main(void) {
int score = 85;
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Expected output:
Grade: B
5. switch Statement
Selects from multiple cases based on an integer or character expression.
Syntax:
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
break;
}
Example:
#include <stdio.h>
int main(void) {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Weekend\n");
break;
}
return 0;
}
Expected output:
Wednesday
Important: The break statement is crucial; without it, execution “falls through” to subsequent cases.
6. Conditional Operator (?:)
A ternary operator for simple if-else assignments.
Syntax:
condition ? expression1 : expression2;
If condition is true, expression1 evaluates; otherwise, expression2 evaluates.
Example:
#include <stdio.h>
int main(void) {
int flag = 0;
int var;
var = (flag == 0) ? 25 : -25;
printf("Value of var when flag is 0: %d\n", var);
int max = (10 > 5) ? 10 : 5;
printf("Max: %d\n", max);
return 0;
}
Expected output:
Value of var when flag is 0: 25 Max: 10
Decision-Making Programs in C
This program demonstrates all decision-making constructs in a realistic scenario: a grade classification system with input validation.
Program: Student Grade Classifier
/**
* grade_classifier.c
* Demonstrates if, if-else, nested if-else, if-else-if ladder,
* switch, and conditional operator.
* Classifies student grades based on percentage score.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#define MAX_LINE_LEN 100
/* Function prototypes */
int readInteger(const char *prompt, int *outValue);
char getLetterGrade(int score);
void printGradeComment(char grade);
bool isValidScore(int score);
int main(void) {
int score;
char letterGrade;
bool passed;
printf("=== Student Grade Classifier ===\n");
/* Read score with validation */
if (readInteger("Enter score (0-100): ", &score) != 0) {
fprintf(stderr, "Failed to read score.\n");
return EXIT_FAILURE;
}
/* Validate score range using if */
if (!isValidScore(score)) {
fprintf(stderr, "Invalid score. Must be between 0 and 100.\n");
return EXIT_FAILURE;
}
/* Determine letter grade using if-else-if ladder */
letterGrade = getLetterGrade(score);
/* Check pass/fail using conditional operator */
passed = (score >= 60) ? true : false;
/* Display results */
printf("\n--- Results ---\n");
printf("Score: %d\n", score);
printf("Letter Grade: %c\n", letterGrade);
/* Use nested if-else for additional comments */
if (passed) {
printf("Status: PASSED\n");
if (score >= 90) {
printf("Excellent work!\n");
} else if (score >= 80) {
printf("Great job!\n");
} else if (score >= 70) {
printf("Good effort!\n");
} else {
printf("You passed. Keep improving!\n");
}
} else {
printf("Status: FAILED\n");
if (score >= 50) {
printf("Close! You almost passed.\n");
} else if (score >= 40) {
printf("Need significant improvement.\n");
} else {
printf("Please seek help from the instructor.\n");
}
}
/* Use switch for letter grade comment */
printGradeComment(letterGrade);
/* Demonstrate conditional operator for simple assignment */
int bonusPoints = (score >= 90) ? 5 : (score >= 80) ? 3 : (score >= 70) ? 1 : 0;
printf("Bonus points earned: %d\n", bonusPoints);
/* Use if-else-if for final message */
if (score == 100) {
printf("Perfect score!\n");
} else if (score >= 90) {
printf("Outstanding performance!\n");
} else if (score >= 80) {
printf("Very good performance!\n");
} else if (score >= 70) {
printf("Good performance!\n");
} else if (score >= 60) {
printf("Satisfactory performance!\n");
} else {
printf("Needs improvement!\n");
}
return EXIT_SUCCESS;
}
/* Returns letter grade based on score using if-else-if ladder */
char getLetterGrade(int score) {
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 70) {
return 'C';
} else if (score >= 60) {
return 'D';
} else {
return 'F';
}
}
/* Prints a comment based on letter grade using switch */
void printGradeComment(char grade) {
printf("Comment: ");
switch (grade) {
case 'A':
printf("Outstanding!\n");
break;
case 'B':
printf("Very Good!\n");
break;
case 'C':
printf("Good!\n");
break;
case 'D':
printf("Satisfactory!\n");
break;
case 'F':
printf("Fail!\n");
break;
default:
printf("Invalid grade!\n");
break;
}
}
/* Checks if score is in valid range */
bool isValidScore(int score) {
return (score >= 0 && score <= 100);
}
/* Reads an integer safely */
int readInteger(const char *prompt, int *outValue) {
char buffer[MAX_LINE_LEN];
char *endPtr;
long val;
while (1) {
printf("%s", prompt);
fflush(stdout);
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
return -1;
}
errno = 0;
val = strtol(buffer, &endPtr, 10);
if (endPtr == buffer || (*endPtr != '\n' && *endPtr != '\0')) {
printf("Invalid integer. Please try again.\n");
continue;
}
if (errno == ERANGE || val < -2147483647 - 1 || val > 2147483647) {
printf("Number out of range. Please try again.\n");
continue;
}
*outValue = (int)val;
return 0;
}
}
Explanation of Decision-Making Usage
ifstatement: Validates score range:if (!isValidScore(score)).if-else-ifladder: IngetLetterGrade(), determines letter grade based on score ranges.- Nested
if-else: Inmain(), provides detailed comments based on pass/fail status and score ranges. switch: InprintGradeComment(), selects comment based on letter grade.- Conditional operator: Assigns
passedstatus andbonusPoints. if-else-iffor final message: Provides multiple conditions for performance feedback.
Expected Output
Sample run 1 (score = 95):
=== Student Grade Classifier === Enter score (0-100): 95 --- Results --- Score: 95 Letter Grade: A Status: PASSED Excellent work! Comment: Outstanding! Bonus points earned: 5 Outstanding performance!
Sample run 2 (score = 45):
=== Student Grade Classifier === Enter score (0-100): 45 --- Results --- Score: 45 Letter Grade: F Status: FAILED Please seek help from the instructor. Comment: Fail! Bonus points earned: 0 Needs improvement!
Sample run 3 (invalid score):
=== Student Grade Classifier === Enter score (0-100): 150 Invalid score. Must be between 0 and 100.
Missing Braces, = vs == and Fall-Through
1. Using = Instead of ==
Assignment (=) in a condition always evaluates to true (non-zero) unless the assigned value is 0.
if (a = 10) { // Always true; assigns 10 to a
}
Solution: Use == for comparison. Enable compiler warnings (-Wall).
2. Dangling else
An else always binds to the nearest unmatched if, not the one you might intend.
if (condition1)
if (condition2)
printf("Both true\n");
else
printf("condition1 false or condition2 false?\n"); // This else binds to inner if
Solution: Use braces to clarify binding:
if (condition1) {
if (condition2)
printf("Both true\n");
} else {
printf("condition1 false\n");
}
3. Forgetting Braces for Multiple Statements
Without braces, only the first statement after if is part of the block.
if (condition)
statement1;
statement2; // This always executes, not part of if
Solution: Always use braces, even for single statements:
if (condition) {
statement1;
statement2;
}
4. switch Fall-Through
Forgetting break causes execution to continue into the next case.
switch (x) {
case 1:
printf("One\n");
case 2:
printf("Two\n"); // Executes for x == 1 and x == 2
}
Solution: Always use break unless fall-through is intentional. Document intentional fall-through with a comment.
5. switch Expression Type
switch expression must be integer or character type. Floating-point or strings are not allowed.
switch (3.14) { // Error: switch quantity not an integer
}
Solution: Use if-else-if for non-integer conditions.
6. Comparison Chaining
a < b < c does not work as expected in C.
if (a < b < c) { // Evaluates as (a < b) < c
}
Solution: Use logical operators: if (a < b && b < c).
7. Floating-Point Equality
Comparing floating-point values for equality is unreliable due to precision.
float f = 0.1f + 0.2f;
if (f == 0.3f) { // May be false
}
Solution: Use epsilon comparison: fabs(f - 0.3f) < 0.0001f.
8. Side Effects in Conditions
Conditions with side effects can be confusing and error-prone.
if (i++ > 5 && array[i] > 0) { // i is incremented before array access
}
Solution: Avoid side effects in conditions; perform them separately.
9. Uninitialized Variables in Conditions
Using uninitialized variables in conditions is undefined behavior.
int x;
if (x > 10) { // x is garbage
}
Solution: Always initialize variables before use.
10. Memory Considerations
- Decision-making constructs do not allocate dynamic memory.
- Nested conditions can increase stack usage if large local variables are declared in blocks.
- Avoid deeply nested
if-else(more than 3-4 levels) as it hurts readability; considerswitchor functions instead.
Which Decision Statement to Use
Decision-making statements in C allow programs to execute different code paths based on conditions. Understanding the various constructs and their proper use is essential for writing clear, correct, and maintainable code.
Key Takeaways
ifexecutes a block when a condition is true.if-elseprovides an alternative block when the condition is false.- Nested
if-elsechecks conditions inside other conditions. if-else-ifladder tests multiple conditions sequentially.switchselects from multiple cases based on an integer/character value.- Conditional operator
?:is a shorthand for simple if-else. - Always use braces to avoid dangling
elseand block errors.
Best Practices
- Use braces
{ }consistently for all control flow blocks, even single statements. - Use
==for comparison and=for assignment; enable compiler warnings. - Avoid side effects in conditions; keep conditions simple and pure.
- Use
if-else-ifladder when testing multiple ranges or discrete values. - Use
switchwhen comparing against many constant values. - Use conditional operator only for simple, clear assignments.
- Limit nesting depth (3-4 levels max); refactor into functions if deeper.
- Validate inputs before using them in conditions.
- Use
booltype (from<stdbool.h>) for boolean variables and return types. - Document complex conditions with comments explaining the logic.
By mastering decision-making in C, you’ll write programs that respond intelligently to different situations, handle errors gracefully, and implement complex logic clearly.
Related lessons
Want the full path? See every lesson in the C course, in order.
