Control Statement in C
Control Statement in C
STATEMENT IN C
Table of contents
01 02
TYPES OF CONTROL CONDITIONAL STATEMENT
STATEMENTS
03 04
LOOPING STATEMENT JUMP STATEMENT
Introduction
● The Statement i.e. used to control the flow of execution of a program is
called control structure.
● By default the statements are executed sequentially. However, in practice,
we have situations where we may have to change the order of executions
of statements until specified conditions are met.
● They determine the order in which instructions are executed and allow us
to make decisions based on certain conditions.
Why are control structures important?
● Flow control: Control structures help us define the sequence in which statements
or blocks of code are executed. This is crucial for achieving the desired behavior
and logic in a program.
● Decision-making: Control structures allow us to make decisions based on
specific conditions. We can create different paths of execution based on whether
a condition is true or false.
● Repetition: Control structures enable us to repeat a block of code multiple times.
This is useful when we want to perform a task iteratively or until a certain
condition is met.
Conditional Statements (If-Else)
Conditional statements are programming constructs that allow us to make
decisions based on certain conditions. They help control the flow of execution by
executing specific blocks of code only if a condition evaluates to true.
int main() {
int age = 14;
return 0;
}
In the example above, if the variable age is greater than or equal to 18, the message "You are
eligible to vote" will be displayed. Otherwise, the message "You are not eligible to vote yet"
will be displayed.
The if-else statement is a fundamental control structure that allows us to create decision-making
logic in our programs. By evaluating conditions, we can direct the program's behavior based
on different scenarios.
Switch Statement
● The switch statement is a control structure used to perform
different actions based on the value of a variable or an
expression. It provides an alternative to using multiple if-else
statements when dealing with multiple possible cases.
Syntax of a switch statement in C:
switch (variable) {
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
// Add more cases as needed
default:
// Code to execute if none of the above cases match
break;
}
#include <stdio.h>
case 4:
int main() { printf("Thursday\n");
int day = 3; break;
case 5:
switch (day) { printf("Friday\n");
case 1: break;
printf("Monday\n"); default:
break; printf("Weekend\n");
case 2: break;
printf("Tuesday\n"); }
break;
case 3: return 0;
printf("Wednesday\n"); }
break;
LOOPING STATEMENT
Unlike the while loop, the do-while loop guarantees that the code block will be executed
at least once, regardless of the initial condition.
Example of a do-while loop: Structure of a do-while loop in C:
int num = 1;
do {
do {
printf("Number: %d\n", num);
// Code to be executed
num++;
} while (condition);
} while (num <= 5);
JUMP STATEMENT
In C, jump statements are used to transfer control within a program to a different location.
There are three main jump statements in C: break, continue, and goto.