This document details fundamental control flow mehcanisms in C programming, specifically focusing on conditional branching and iterative looping.
Conditional Branching
C provides two primary constructs for conditional execution: if-else statements and switch statements.
1. The if-else Statement
The basic if statement structure is:
if (condition)
statement1;
To execute multiple statements based on a condition, enclose them within curly braces {}:
if (condition) {
statement1;
statement2;
statement3;
}
The else clause extends the if statement to provide an alternative execution path:
if (condition)
statement1;
else
statement2;
Nested if-else structures are also supported. A key point to remember is that an else block is always associated with the nearest preceding if statement. Proper code indentation is crucial for readability and to avoid logical errors.
Relational Operators
These operators are used to compare values:
==: Equal to!=: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
When comparing a variable to a constant for equality, it's a common practice to place the constant on the left side of the == operator (e.g., 5 == count) to catch potential typos where = might be used instead of ==.
Avoid chaining multiple relational operators like a < b < c; instead, use logical operators to combine comparisons.
2. The Conditional (Ternary) Operator
The conditional operator, also known as the ternary operator, offers a concise way to express simple if-else logic. It takes three operands:
condition ? expression_if_true : expression_if_false
If condition evaluates to true, expression_if_true is evaluated, and its result is the outcome of the entire expression. If condition is falce, expression_if_false is evaluated.
For instance, the following if-else block:
if (a > 5) {
b = 3;
} else {
b = -3;
}
can be refactored using the ternary operator:
b = (a > 5) ? 3 : -3;
Logical Operators
Logical operators combine or modify boolean expressions:
!: Logical NOT (negates the truth value of an expression).&&: Logical AND (evaluates to true if both operands are true).||: Logical OR (evaluates to true if at least one operand is true).
Short-Circuit Evaluation
C's logical operators (&& and ||) exhibit short-circuit behavior. Evaluation proceeds from left to right. If the result of the entire logical expression can be determined solely from the left operand, the right operand is not evaluated.
- With
&&: If the left operand evaluates to false (0), the entire expression is false, and the right operand is skipped.if (month >= 3 && month <= 5) // If month < 3, month <= 5 is not checked. - With
||: If the left operand evaluates to true (non-zero), the entire expression is true, and the right operand is skipped.if (month == 12 || month == 1 || month == 2) // If month == 12, subsequent checks are skipped.
3. The switch Statement
The switch statement provides an alternative to lengthy if-else if-else chains when checking a single expression against multiple constant values.
switch (expression) {
case constant_value1:
// Statements for value1
break;
case constant_value2:
// Statements for value2
break;
// ... other cases
default:
// Default statements if no case matches
}
- The
expressioninswitch()must evaluate to an integral type (likeint). - Each
caselabel must be followed by a constant integral expression. - The
breakstatement is crucial. Without it, execution