Basic Control Flow
Understanding basic control flow is essential for writing programs that can make decisions and execute statements in different orders based on conditions. In C, control flow is managed using conditional constructs such as if, if-else, if-else ladder, and switch-case.
Block
A block is a group of code statements intended to be executed as a unit. In C, a block is enclosed within curly braces {}. Blocks can be nested inside other blocks.
Example:
{
int x = 10;
int y = x + 10;
int z;
z = x + y;
}
Conditional Constructs
A conditional is a statement that instructs the computer to execute a block of code only if a specific condition is met. Conditionals are used for decision-making based on the truth value of a condition or the state of a variable or expression.
If Construct
The simplest conditional construct. It specifies that a block should be executed only if a certain condition is true.
Syntax:
if (test expression) {
block1;
}
If the test expression is true, block1 is executed. If false, block1 is ignored.
If-Else Construct
Used to execute one of two blocks based on the truth value of a condition.
Syntax:
if (test expression) {
block1;
} else {
block2;
}
If the test expression is true, block1 is executed. If false, block2 is executed.
Nesting
If and if-else statements can be nested to create more complex control flows.
Example:
if (test expression1) {
statements1;
if (test expression2) {
statements2;
}
} else {
statements3;
if (test expression3) {
statements4;
} else {
statements5;
}
}
If-Else Ladder
Used when a series of mutually exclusive conditions must be checked (e.g., deciding grade based on marks).
Syntax:
if (test expression1)
statements1;
else if (test expression2)
statements2;
else if (test expression3)
statements3;
else if (test expression4)
statements4;
else
statements5;
Conditions are evaluated from top to bottom. The first true condition executes its statements and exits the construct. The final else is executed only if all conditions are false.
Switch-Case Construct
Switch-case allows jumping to one of multiple blocks based on the value of a variable or expression. A default case can be specified for unmatched values.
Syntax:
switch (test expression) {
case 1:
statement1;
break;
case 2:
statement2;
break;
case n:
statementn;
break;
default:
statements_default;
}
If test expression matches a case, its statements are executed, followed by break. If no case matches, the default is executed.
Important Notes
- The relational operator "equal to" is
==, while the assignment operator is=. Using=instead of==in a condition will not be reported by the compiler and may cause logical errors. Example:
if (x = 3) {
statements1;
}
This will always be true, so statements1 are always executed. Instead, use:
if (x == 3) {
statements1;
}
- Typically, the last statement for each case in a switch is
break. Omittingbreakcauses execution to continue into the next case, known as fall-through.