Skip to content

C Decision Making

Decision making allows a program to execute different blocks of code based on whether certain conditions are true or false. This enables programs to react dynamically to different inputs or situations.

The core idea is to evaluate a condition (an expression) and choose a path of execution based on the result. If the condition is true, one set of statements is executed; if it’s false, another set (or none) might be executed.

Unlike some languages with a dedicated boolean type, C traditionally uses integers to represent true and false in conditional expressions:

  • False: The integer value 0 is considered false.
  • True: Any non-zero integer value (positive or negative) is considered true.

The result of relational (<, >, <=, >=) and equality (==, !=) operators is 1 (true) or 0 (false). Logical operators (&&, ||, !) also work with these integer-based true/false values.

Since the C99 standard, C includes the <stdbool.h> header, which defines:

  • A bool type (usually an alias for _Bool).
  • Constants true (typically expands to 1) and false (typically expands to 0).

Using <stdbool.h> can make code more explicit and readable, but the underlying principle (0 is false, non-zero is true) remains fundamental.

C provides the following decision-making statements:

StatementDescription
if statementExecutes a block of code only if a specified condition evaluates to true (non-zero).
if...else statementExecutes one block of code if the condition is true, and a different block if the condition is false (zero).
nested if statementsAn if or else block can contain another if or if...else statement, allowing for more complex branching logic.
if...else if...else ladderA common pattern using nested ifs to test a series of conditions sequentially. The block associated with the first true condition is executed.
switch statementAllows testing a variable against a list of constant integer or character values (cases). Provides an efficient alternative to long if...else if ladders for specific equality checks.
nested switch statementsA case or default block within a switch statement can contain another switch statement.

C also offers a shorthand conditional expression using the ternary operator ? :. It’s often used for simple conditional assignments or expressions.

condition ? expression_if_true : expression_if_false;
  1. The condition is evaluated first.
  2. If condition is true (non-zero), expression_if_true is evaluated, and its value becomes the result of the entire ternary expression.
  3. If condition is false (zero), expression_if_false is evaluated, and its value becomes the result of the entire ternary expression.

Example:

#include <stdio.h>
#include <stdlib.h> // For EXIT_SUCCESS
int main(void) {
int a = 10, b = 20;
int max_val;
// Use ternary operator to find the maximum value
max_val = (a > b) ? a : b;
printf("Maximum value is: %d\n", max_val);
return EXIT_SUCCESS;
}

This is equivalent to:

if (a > b) {
max_val = a;
} else {
max_val = b;
}

While concise, overuse of complex or nested ternary operators can decrease readability compared to standard if...else statements.