Control flow statements in C are used to control the flow of execution in a program. They allow you to make decisions, repeat blocks of code, and jump to different parts of the program based on certain conditions. There are mainly three types of control flow statements in C
In C, there are primarily three types of control flow statements:
The if
statement is used to execute a block of code if a specified condition is true.
int num = 10;
if (num > 5)
{
printf("Number is greater than 5\n");
}
The else if
statement is used to specify multiple conditions to execute different blocks of code.
if (condition1)
{
// code to be executed if condition1 is true
}
else if (condition2)
{
// code to be executed if condition2 is true
}
The else
statement is used to execute a block of code if the condition in the if
statement is false.
if (condition)
{
// code to be executed if condition is true
}
else
{
// code to be executed if condition is false
}
Switch statements are used when you have a single expression that you want to evaluate against multiple possible values. It provides an alternative to a series of if-else if statements, making code more concise and readable.
switch (expression)
{
case value1:
// code to be executed if expression matches value1
break;
case value2:
// code to be executed if expression matches value2
break;
...
default:
// code to be executed if expression doesn't match any case
}
#include
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
In this example, we have a variable day
with the value 3
. The switch statement evaluates this variable against different cases. Since day
is 3
, it matches the case 3
, and the corresponding code block prints “Wednesday” to the console. The break
statement is crucial to exit the switch statement once a matching case is found, preventing execution from falling through to subsequent cases.
Using Default Case: If none of the cases match the value of the expression, the code inside the default
case is executed.
The for
loop is used to execute a block of code repeatedly for a fixed number of times.
for (initialization; condition; increment/decrement)
{
// code to be executed repeatedly
}
for (int i = 0; i < 5; i++)
{
printf("%d\n", i);
}
The while
loop is used to execute a block of code repeatedly as long as a specified condition is true.
while (condition)
{
// code to be executed repeatedly
The do-while
loop is similar to the while
loop, but it ensures that the block of code is executed at least once, even if the condition is false.
do
{
// code to be executed repeatedly
} while (condition);
int i = 0;
do
{
printf("%d\n", i);
i++;
} while (i < 5);
Jump statements allow you to transfer control within a program.
Used to exit the loop or switch statement.
while (condition) {
if (condition2) {
break; // exit the loop if condition2 is true
}
}
Used to skip the current iteration of a loop.
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // skip iteration if i equals 2
}
// code here will not be executed when i equals 2
}
Used to exit from a function and optionally return a value.
int add(int a, int b) {
return a + b; // exit function and return sum of a and b
}
Control flow statements are essential in C programming as they allow you to make decisions, repeat code, and control the flow of execution in your program. By using conditional statements, looping statements, and jump statements effectively, you can write efficient and structured code to solve various problems. Happy coding! ❤️