Flow Control in C Language
In C programming, execution is sequential, meaning statements are executed one after the other. To control the flow of execution, developers use flow control structures, primarily conditional and loop statements.
if Statement
The if statement is used for conditional execution; it executes a specified statement if the condition is true.
Syntax:
1 | if (expression) statement |
Here, expression must be true (non-zero) for statement to execute. The condition inside the parentheses must be enclosed in parentheses, or it will result in an error. The statement can be a single statement or a block enclosed in curly braces.
Example:
1 | if (x == 10) |
In this case, if x equals 10, the message “x is 10” will be printed. For a single statement, it’s common to place it on a new line.
Example with newline:
1 | if (x == 10) |
If there are multiple statements, they should be enclosed in curly braces.
Example with multiple statements:
1 | if (line_num == MAX_LINES) { |
The if statement can also have an else branch, which executes if the condition is false.
Syntax:
1 | if (expression) statement |
Example:
1 | if (i > j) |
If the else statement has multiple lines, enclose them in curly braces.
The else clause can be used with another if to create a chain of conditions.
Syntax:
1 | if (expression) |
In complex cases, remember that else matches the nearest preceding if.
Example:
1 | if (number > 6) |
In this example, the else matches the closest if (i.e., number < 12). Thus, if number is 6, the else part does not execute. To avoid confusion and improve readability, use curly braces to clearly define the scope of else.
Example with curly braces:
1 | if (number > 6) { |
This makes it clear that the else belongs to the outer if.
Ternary Operator (?:)
In C language, the ternary operator ?: is a shorthand for an if...else statement:
1 | <expression1> ? <expression2> : <expression3> |
This means if expression1 is true (non-zero), expression2 is executed; otherwise, expression3 is executed.
For example, to return the larger of two values:
1 | (i > j) ? i : j; |
This is equivalent to:
1 | if (i > j) |
switch Statement
The switch statement is a specialized form of if...else used when you have multiple possible outcomes. It replaces multiple else if statements with a more readable format:
1 | switch (expression) { |
The code executes the case block matching the value of expression. If no match is found, the default block is executed.
For example:
1 | switch (grade) { |
Here, depending on the value of grade, different case blocks are executed. If grade is 0, it executes the case 0 block; if 1, it executes the case 1 block; otherwise, it executes the default block. The default block handles cases where no case matches.
Each case block should end with a break statement to prevent fall-through to the next case. If break is omitted, the next case or default block will be executed as well.
For example:
1 | switch (grade) { |
In this example, missing the break after case 0 causes the case 1 block to execute as well.
You can group multiple case labels to share the same statement:
1 | switch (grade) { |
In this case, both case 0 and case 1 execute the same statement. Note that case labels do not need curly braces, which is why break is necessary to avoid falling through.
The default block is optional and should be placed at the end of the switch statement. If omitted, the switch will exit without executing any code if no cases match.
while Statement
The while loop repeatedly executes a block of code as long as its condition is true.
1 | while (expression) |
In this code, if expression is non-zero (true), statement is executed, and expression is checked again. If expression becomes zero (false), the loop exits.
Example:
1 | while (i < n) |
In the example above, i will keep increasing by 2 as long as i is less than n.
To execute multiple statements in the loop, use braces:
1 | while (expression) { |
Example:
1 | i = 0; |
In this example, the loop runs 10 times, incrementing i by 1 each time until i equals 10.
An infinite loop can be created like this:
1 | while (1) { |
Use break to exit an infinite loop.
do...while Structure
The do...while loop executes its body once before checking the condition.
1 | do |
Here, statement is executed at least once regardless of expression. After the statement executes, expression is checked to determine if the loop should continue.
Example:
1 | i = 10; |
Even though i is not less than 10 initially, the loop body executes once.
for Statement
The for loop is used for precise control over the number of iterations.
1 | for (initialization; condition; action) |
initialization: Sets up the loop variable (executed once).condition: Checked before each iteration (loop continues if true).action: Updates the loop variable after each iteration.
Example:
1 | for (int i = 10; i > 0; i--) |
Here, i is initialized to 10, decremented each time, and the loop runs while i is greater than 0.
Multiple statements can be used in any of the three parts of the for loop, separated by commas:
1 | int i, j; |
If all three parts are omitted, the loop becomes infinite:
1 | for (;;) { |
break Statement
The break statement has two primary uses: one is with switch statements to exit a particular case, and the other is to exit a loop prematurely.
Example with Nested Loops:
1 | for (int i = 0; i < 3; i++) { |
In this example, the break statement exits the inner loop, causing the program to proceed to the next iteration of the outer loop.
Example with while Loop:
1 | while ((ch = getchar()) != EOF) { |
Here, the break statement exits the while loop as soon as a newline character is encountered, so it stops processing further input.
Note: The break statement can only exit from loops and switch statements, not from if statements.
Invalid Use Example:
1 | if (n > 1) { |
In this example, the break statement is ineffective because it cannot exit from an if statement.
continue Statement
The continue statement is used to skip the rest of the current loop iteration and proceed to the next iteration.
Example with Nested Loops:
1 | for (int i = 0; i < 3; i++) { |
In this case, the continue statement does not change the behavior of the loop because it simply moves to the next iteration of the inner loop, which would happen even without it.
Example with while Loop:
1 | while ((ch = getchar()) != '\n') { |
Here, if a tab character (\t) is encountered, the continue statement skips printing it and proceeds to the next character.
goto Statement
The goto statement allows jumping to a specified label within the same function. Its use is generally discouraged as it can make code harder to read and maintain.
Example with Label:
1 | char ch; |
In this example, top is a label, and the goto statement transfers control to that label. This causes the program to loop back and read characters until ‘q’ is encountered.
Example of Infinite Loop:
1 | infinite_loop: |
This code results in an infinite loop because the goto statement continuously jumps back to the infinite_loop label.
Using goto to Exit Nested Loops:
1 | for(...) { |
In this case, goto is used to break out of several nested loops quickly when an error condition is met.
Using goto for Early Exit:
1 | if (do_something() == ERR) |
Here, goto allows the program to skip the remaining checks and jump to the error label if any of the conditions fail.
Note: goto can only be used within the same function; it cannot jump to a label in a different function.


