What Are Control Statements in C?
Control statements in C language are instructions that determine the flow of a program's
execution based on certain conditions or repetitions. They help decide whether a block of
code should run, repeat, or skip. These statements make programs dynamic by adding
decision-making, looping, and jumping capabilities, which are essential for solving real-
world problems.
Let’s understand it with a real-life example:
Think of control statements like traffic signals. A green light allows cars to move
(execution), a red light stops them (condition not met), and a yellow light prepares them for
the next action (transition).
Types of Control Statements in C
There are three types of control statements in C programming:
1. Decision-Making Statements: These statements allow the program to make decisions
and execute specific blocks of code based on conditions.
2. Loop Control Statements: These statements repeatedly execute a block of code as long
as a specified condition is true.
3. Jump Statements: These statements transfer the program's control from one part of the
code to another, either conditionally or unconditionally.
Decision-Making Control Statements in C
Decision-making statements in C allow the program to make choices and execute specific
blocks of code based on conditions. These statements enable a program to behave
dynamically and handle different scenarios effectively.
Let’s discuss the different types of decision control statements in C:
1. if Statement
This is the simplest form of ‘if’ statement. The expression is to be placed in parenthesis. It
can be any logical [Link] Block is executed when the given condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.");
}
return 0;
}
When to Use: Use the if statement when you need to perform an action based on a single
condition.
2. if-else Statement
In the “if” statement seen earlier, we can take some action if expression is true. But if
expression is false there is no action. We can include an action for both conditions (i.e. true
or false) by using if-else statement. If condition is true block1 is executed otherwise block2.
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
Example:
#include <stdio.h>
int main() {
int number = -5;
if (number >= 0)
{
printf("The number is non-negative.");
}
else
{
printf("The number is negative.");
}
return 0;
}
When to Use: Use if-else control statement in C programming when you need to perform one
action for a true condition and another for a false condition.
3. nested if Statement
It is an if statement inside another if statement, used to check multiple conditions.
Syntax:
if (condition1)
{
if (condition2)
{
// Code if both conditions are true
}
}
Example:
#include <stdio.h>
int main()
{
int number = 5;
if (number > 0)
{
if (number % 2 == 0)
{
printf("The number is positive and even.");
}
else
{
printf("The number is positive and odd.");
}
}
return 0;
}
When to Use: Use nested if when you need to test multiple related conditions.
4. if-else-if Ladder
The evolutions of if-else –if ladder or multiple alternative if statement is carried out from top
to bottom. Each conditional expression is tested and if found true only then its corresponding
statements is executed. In a situation where none of the nested conditions is found true then
the final else part is executed.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
Example:
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 90) {
printf("Grade: A");
} else if (marks >= 75) {
printf("Grade: B");
} else if (marks >= 50) {
printf("Grade: C");
} else {
printf("Grade: F");
}
return 0;
}
When to Use: Use the if-else-if ladder when you need to check multiple conditions in
sequence.
5. switch case Statement
The switch case in C tests a variable against multiple values (cases) and executes the
matching block of code.
Syntax:
switch (variable) {
case value1:
// Code for case 1
break;
case value2:
// Code for case 2
break;
default:
// Code if no case matches
}
Example:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid day");
}
return 0;
}
When to Use: Use switch when you need to test a variable against a fixed set of values.
Loop Control Statements in C
Loop control statements in C are used to repeatedly execute a block of code as long as a
specific condition is true. They simplify repetitive tasks and help in writing concise
programs.
Imagine you are filling bottles with water. You continue filling bottles one by one until you
run out of empty bottles. Similarly, in programming, looping statements repeatedly perform
tasks until a condition is met.
Let’s understand the different loops in C programming:
1. for Loop
The for loop in C executes a block of code a specific number of times, controlled by an
initialization, condition, and increment/decrement.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute in each iteration
}
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Number: %d\n", i);
}
return 0;
}
When to Use: Use the for loop when the number of iterations is known beforehand.
2. while Loop
The while loop in C executes a block of code repeatedly as long as the specified condition is
true.
Syntax:
while (condition) {
// Code to execute while condition is true
}
Example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("Number: %d\n", i);
i++;
}
return 0;
}
When to Use: Use the while loop when the number of iterations is not known and depends
on a condition.
3. do-while Loop
The do-while loop in C executes a block of code at least once and then continues to execute
as long as the condition is true.
Syntax:
do {
// Code to execute
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 1;
do {
printf("Number: %d\n", i);
i++;
} while (i <= 5);
return 0;
}
When to Use: Use the do-while loop when you want the code to execute at least once,
regardless of the condition.
Comparison of Looping Control Statements in C
Loop Type Condition Checked Best Used For
for Before the first iteration Known number of iterations.
while Before each iteration Unknown iterations, based on a condition.
do-while After the first iteration Code must run at least once.
Jumping Control Statements in C
Jumping statements in C are used to transfer control of the program from one point to
another. These statements enable conditional or unconditional jumps, allowing flexibility in
program flow.
Real-Life Example:
Imagine reading a book:
You can skip pages you don’t need (continue).
You can stop reading altogether (break).
You can flip directly to a specific page (goto).
Jumping statements in C programming work similarly by moving control within the code.
There are four main jump control statements in C language:
1. break Statement
The break statement in C terminates the nearest enclosing loop or switch statement and
transfers control to the next statement after it.
Syntax:
break;
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
printf("Number: %d\n", i);
}
return 0;
}
When to Use: Use break to exit a loop or switch prematurely when a specific condition is
met.
2. continue Statement
The continue statement in C skips the current iteration of the loop and moves to the next
iteration.
Syntax:
continue;
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("Number: %d\n", i);
}
return 0;
}
When to Use: Use continue when you want to skip specific iterations of a loop without
exiting it.
3. goto Statement
The goto statement in C transfers control to a labeled statement within the program.
Syntax:
goto label;
...
label:
// Code to execute
Example:
#include <stdio.h>
int main() {
int number = 3;
if (number < 5) {
goto small;
}
printf("Number is not small.\n");
return 0;
small:
printf("Number is small.\n");
}
When to Use: Use goto sparingly, typically in cases like error handling, where other
approaches may complicate the code.
4. return Statement
The return statement in C exits the current function and optionally returns a value to the
calling function.
Syntax:
return; // Without value
return value; // With value
Example:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("Result: %d\n", result);
return 0;
}
When to Use: Use return to exit a function and optionally pass a value back to the calling
function.
Comparison of Jumping Statements
Jump Type Purpose Best Used For
break Exit a loop or switch To terminate loops/switch on a condition.
continue Skip to the next iteration To bypass specific iterations in a loop.
goto Jump to a labeled statement Rarely, for complex flow control or errors.
return Exit a function To end function execution and return a value.