C Decision Making
C - Decision Making
Section titled “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.
Truthiness in C
Section titled “Truthiness in C”Unlike some languages with a dedicated boolean type, C traditionally uses integers to represent true and false in conditional expressions:
- False: The integer value
0is 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
booltype (usually an alias for_Bool). - Constants
true(typically expands to1) andfalse(typically expands to0).
Using <stdbool.h> can make code more explicit and readable, but the underlying principle (0 is false, non-zero is true) remains fundamental.
Control Flow Statements
Section titled “Control Flow Statements”C provides the following decision-making statements:
| Statement | Description |
|---|---|
if statement | Executes a block of code only if a specified condition evaluates to true (non-zero). |
if...else statement | Executes one block of code if the condition is true, and a different block if the condition is false (zero). |
nested if statements | An if or else block can contain another if or if...else statement, allowing for more complex branching logic. |
if...else if...else ladder | A common pattern using nested ifs to test a series of conditions sequentially. The block associated with the first true condition is executed. |
switch statement | Allows 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 statements | A case or default block within a switch statement can contain another switch statement. |
The Ternary Conditional Operator (? :)
Section titled “The Ternary Conditional Operator (? :)”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;- The
conditionis evaluated first. - If
conditionis true (non-zero),expression_if_trueis evaluated, and its value becomes the result of the entire ternary expression. - If
conditionis false (zero),expression_if_falseis 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.