Control Flow Statements in C Programming
Introduction
Control flow statements, also called control structures, are used to control the order in which statements in a program are executed.
Normally, if no control statement is used, program statements execute sequentially, one after another from the beginning to the end.
Control flow statements allow a programmer to:
- Make decisions
- Repeat statements
- Skip statements
- Transfer program control from one place to another
Types of Control Flow Statements
Control flow statements in C can mainly be classified into:
- Conditional / Decision-Making Statements
- Looping / Iteration Statements
- Jumping Statements
A. Conditional / Decision-Making Statements
Conditional statements are used when a program needs to make a decision based on a particular condition.
The program checks a condition and executes the appropriate block of code according to whether the condition is true or false.
The main decision-making statements in C are:
ifif-elseif-else-ifladderswitch-case
1. if Statement
The if statement is a basic conditional statement.
It checks a condition. If the condition is true, the statements inside the if block are executed.
If the condition is false, the block is skipped.
Syntax
if (condition)
{
// statements
}
Flow
Start → Check Condition → True → Execute Statements → End
If the condition is false, the program skips the statements inside the if block.
Example: Check Whether a Number is Positive
#include <stdio.h>
int main(void)
{
int a;
printf("Enter the value of a: ");
scanf("%d", &a);
if (a > 0)
{
printf("The number is positive.");
}
return 0;
}
Example Output
Enter the value of a: 10
The number is positive.
2. if-else Statement
The if-else statement is used for two-way decision making.
First, the condition is evaluated.
- If the condition is true, the
ifblock is executed. - If the condition is false, the
elseblock is executed.
Syntax
if (condition)
{
// statements executed when condition is true
}
else
{
// statements executed when condition is false
}
Flow
Condition
/ \
True False
| |
if block else block
\ /
End
Example: Check Whether a Number is Even or Odd
#include <stdio.h>
int main(void)
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
if (a % 2 == 0)
{
printf("The number is even.");
}
else
{
printf("The number is odd.");
}
return 0;
}
3. if-else-if Ladder
The if-else-if ladder is used when a program needs to check several conditions.
Conditions are checked from top to bottom.
When a condition becomes true:
- The corresponding block is executed.
- The remaining conditions are skipped.
If none of the conditions is true, the final else block is executed.
Syntax
if (condition1)
{
// statement 1
}
else if (condition2)
{
// statement 2
}
else if (condition3)
{
// statement 3
}
else
{
// default statement
}
Flow
Start
|
Condition 1?
/ \
Yes No
| |
Block 1 Condition 2?
/ \
Yes No
| |
Block 2 Condition 3?
|
...
|
Else
|
End
Example: Display Grade According to Marks
#include <stdio.h>
int main(void)
{
int result;
printf("Enter the marks of the student: ");
scanf("%d", &result);
if (result >= 75)
{
printf("Passed: Grade A\n");
}
else if (result >= 60)
{
printf("Passed: Grade B\n");
}
else if (result >= 45)
{
printf("Passed: Grade C\n");
}
else
{
printf("Failed\n");
}
return 0;
}
Explanation
75or above → Grade A60–74→ Grade B45–59→ Grade C- Below
45→ Failed
4. switch-case Statement
The switch-case statement is another form of multi-way decision-making statement.
It compares the value of an expression with different case values.
When a matching case is found, the corresponding statements are executed.
The break statement is normally used to stop execution from continuing into the next case.
If no case matches, the default block is executed.
Syntax
switch (expression)
{
case value1:
// statements
break;
case value2:
// statements
break;
case value3:
// statements
break;
default:
// statements
}
Example: Display Day of the Week
#include <stdio.h>
int main(void)
{
int day;
printf("Enter the day number (1-7): ");
scanf("%d", &day);
switch (day)
{
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Important Points About switch-case
- The
switchexpression is evaluated first. - Each
caserepresents a possible value. breakends the current case.defaultexecutes when no case matches.- The
defaultcase is optional. - Without
break, execution may continue into the following case.
B. Looping / Iteration Statements
A loop is used to execute a statement or group of statements repeatedly.
Looping is also known as:
- Repetition
- Iteration
Instead of writing the same statement many times, we can use a loop.
The main loops in C are:
forloopwhileloopdo-whileloop- Nested loop
1. for Loop
A for loop is commonly used when the number of repetitions is known in advance.
It contains three main parts:
- Initialization
- Condition
- Increment or decrement
Syntax
for (initialization; condition; increment/decrement)
{
// statements
}
Working of for Loop
The loop works in the following order:
- Initialization is executed.
- Condition is checked.
- If the condition is true, the loop body executes.
- Increment or decrement is performed.
- The condition is checked again.
- The process repeats until the condition becomes false.
Example: Display Numbers from 1 to 10
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
printf("\n");
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
2. while Loop
A while loop repeatedly executes a block of statements as long as the given condition remains true.
The condition is checked before executing the loop body.
Therefore, a while loop is called an entry-controlled loop.
If the condition is false at the beginning, the loop body may not execute even once.
Syntax
initialization;
while (condition)
{
// statements
increment/decrement;
}
Example: Display Numbers from 0 to 9
#include <stdio.h>
int main(void)
{
int i = 0;
while (i < 10)
{
printf("%d ", i);
i++;
}
return 0;
}
Output
0 1 2 3 4 5 6 7 8 9
3. do-while Loop
A do-while loop executes the body of the loop before checking the condition.
Therefore, the statements inside a do-while loop execute at least once.
It is called an exit-controlled loop.
Syntax
do
{
// statements
} while (condition);
Notice that a semicolon ; is required after the while(condition) statement.
Example
#include <stdio.h>
int main(void)
{
int count = 1;
do
{
printf("Count: %d\n", count);
count++;
} while (count <= 5);
return 0;
}
Output
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Difference Between while and do-while Loop
| while Loop | do-while Loop |
|---|---|
| Condition is checked before executing the body. | Condition is checked after executing the body. |
| It is an entry-controlled loop. | It is an exit-controlled loop. |
| The body may execute zero times. | The body executes at least once. |
Semicolon is not written after the while condition. | A semicolon is required after the final while(condition);. |
4. Nested Loop
A nested loop means using one loop inside another loop.
The loop inside another loop is called the inner loop, while the outside loop is called the outer loop.
Syntax
for (initialization; condition; increment/decrement)
{
for (initialization; condition; increment/decrement)
{
// inner loop statements
}
// outer loop statements
}
Example of Nested Loop
#include <stdio.h>
int main(void)
{
int rows, cols;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
printf("(%d, %d) ", i, j);
}
printf("\n");
}
return 0;
}
Example Output
If:
Rows = 2
Columns = 3
The output will be:
(1, 1) (1, 2) (1, 3)
(2, 1) (2, 2) (2, 3)
C. Jumping Statements
Jumping statements are used to transfer program control from one place to another.
The major jumping statements in C are:
breakcontinuegotoreturn
1. break Statement
The break statement is used to immediately terminate a loop or switch statement.
When break is encountered:
- The remaining statements inside the loop or switch are skipped.
- Control moves to the first statement after the loop or switch.
Syntax
break;
Example of break Statement
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4
Explanation
When i becomes 5, the break statement terminates the loop immediately.
2. continue Statement
The continue statement skips the remaining statements of the current iteration and moves to the next iteration of the loop.
Unlike break, it does not completely terminate the loop.
Syntax
continue;
Example of continue Statement
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 10; i++)
{
if (i % 2 != 0)
{
continue;
}
printf("%d ", i);
}
return 0;
}
Output
2 4 6 8 10
Explanation
When the value of i is odd, the continue statement skips the printf() statement.
Therefore, only even numbers are displayed.
Difference Between break and continue
| break | continue |
|---|---|
| Terminates the entire loop. | Skips only the current iteration. |
| Control moves outside the loop. | Control moves to the next iteration. |
Used in loops and switch. | Mainly used in loops. |
| Remaining iterations are not executed. | Remaining iterations continue normally. |
3. goto Statement
The goto statement transfers program control directly to a labeled statement.
A label is an identifier followed by a colon :.
Syntax
goto label;
/* statements */
label:
// statements
Example of goto Statement
#include <stdio.h>
int main(void)
{
int i = 0;
start:
printf("%d\n", i);
i++;
if (i < 5)
{
goto start;
}
return 0;
}
Output
0
1
2
3
4
Important Note About goto
Although goto is available in C, excessive use of it can make programs difficult to understand and maintain.
Loops and functions are generally preferred whenever possible.
4. return Statement
The return statement is used to terminate a function and return program control to the function that called it.
A return statement may:
- Return a value
- Return without a value in a
voidfunction
Syntax
return;
or:
return expression;
Example of return Statement
#include <stdio.h>
int add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
int main(void)
{
int result;
result = add(5, 3);
printf("Sum: %d\n", result);
return 0;
}
Output
Sum: 8
Difference Between Syntax Error and Semantic / Logical Error
Errors are mistakes in a computer program that can prevent the program from working correctly.
Two important types discussed here are:
- Syntax Error
- Semantic / Logical Error
| Syntax Error | Semantic / Logical Error |
|---|---|
| It occurs when the rules or grammar of the programming language are violated. | It occurs when the program is syntactically valid but produces an incorrect result because the intended meaning or logic is wrong. |
| It is related to the structure of the program. | It is related to the meaning or logic of the program. |
| It is generally detected by the compiler. | It may not be detected automatically by the compiler. |
| The program normally cannot be compiled successfully until the error is corrected. | The program may compile and execute successfully but produce the wrong output. |
| It is generally easier to locate. | It can be more difficult to identify. |
| Example: missing semicolon. | Example: using subtraction when addition was required. |
Example of Syntax Error
Incorrect:
int a = 5
The semicolon is missing.
Correct:
int a = 5;
Example of Semantic / Logical Error
Suppose we want to calculate:
c = a + b
But mistakenly write:
c = a - b;
The program may compile and run successfully, but the result will be incorrect.
Quick Revision
Control Flow Statements
Control flow statements determine the order in which program instructions are executed.
They are mainly:
- Decision-making statements
- Looping statements
- Jumping statements
Decision-Making Statements
if
Executes statements only when a condition is true.
if-else
Selects one of two possible blocks.
if-else-if Ladder
Checks multiple conditions in sequence.
switch-case
Selects one case from several possible values.
Looping Statements
for Loop
Best suited when the number of repetitions is known.
while Loop
Checks the condition before executing the loop body.
It is an entry-controlled loop.
do-while Loop
Executes the body first and checks the condition afterward.
It is an exit-controlled loop.
Nested Loop
A loop placed inside another loop.
Jumping Statements
break
Terminates a loop or switch statement.
continue
Skips the current iteration and continues with the next iteration.
goto
Transfers control to a labeled statement.
return
Terminates a function and may return a value.
Important Exam Points
- Control structures determine the flow of program execution.
ifis used for one-way decision making.if-elseis used for two-way decision making.if-else-ifis used for multiple conditions.switch-caseis used for multi-way selection.for,while, anddo-whileare looping statements.whileis an entry-controlled loop.do-whileis an exit-controlled loop.- A
do-whileloop executes at least once. breakterminates a loop.continueskips the current iteration.gototransfers control to a labeled statement.returnterminates a function and can return a value.- Syntax errors violate language grammar.
- Logical or semantic errors produce incorrect results even when the program can execute.
Discussion
Share a helpful question, idea, or explanation with other students.