Control Statements in C
In C, decision control statements are used to control the flow of execution of a program based on certain conditions. These statements allow you to make decisions and execute different blocks of code based on the result of the decision.
Different Types of control statements are as follows
- If Statement
- If-else Statement
- Nested-If Statement
- Else-if Ladder Statement
- Switch Statement
If Statement
The if
statement is used to execute a block of code if a certain condition is true. The basic syntax is:
if (condition)
{
// code to be executed if condition is true
}
Sample program using if statement as follows
#include <stdio.h>
int main()
{
int x = 5;
if (x > 0)
{
printf("x is positive");
}
return 0;
}
If-else Statement
The if-else
statement is similar to the if
statement, but it provides an alternate block of code to be executed if the condition is false. The basic syntax is:
if (condition)
{
// code to be executed if condition is true
}
else
{
// code to be executed if condition is false
}
Sample Program using if-else statement as follows
#include <stdio.h>
int main()
{
int x = -5;
if (x > 0)
{
printf("x is positive");
}
else
{
printf("x is not positive");
}
return 0;
}
Nested-If Statement
A nested if
statement is an if
statement that is contained inside another if
statement. It allows you to test multiple conditions and execute different blocks of code based on the results.
Here is an example of a nested if
statement:
if (condition 1)
{
// code to be executed if condition 1 is true
if (condition 2)
{
// code to be executed if condition 1 is true and condition 2 is true
}
else
{
// code to be executed if condition 1 is true and condition 2 is false
}
}
else
{
// code to be executed if condition 1 is false
}
In this example, the inner if-else
statement is nested inside the outer if
statement. The inner if-else
statement is only executed if condition 1 is true. If condition 1 is false, the inner if-else
statement is skipped and the code in the outer else
block is executed.
Sample program using nested if as follows
#include <stdio.h>
int main()
{
int x = 5;
int y = -10;
if (x > 0)
{
if (y > 0)
{
printf("x and y are both positive");
}
else
{
printf("x is positive and y is not positive");
}
}
else
{
if (y > 0)
{
printf("x is not positive and y is positive");
}
else
{
printf("x and y are both not positive");
}
}
return 0;
}
Else-If Ladder Statement
The else-if ladder
is a series of if-else
statements that allows you to test multiple conditions. It is useful when you have more than two conditions to test.
if (condition 1)
{
// code to be executed if condition 1 is true
}
else if (condition 2)
{
// code to be executed if condition 1 is false and condition 2 is true
}
else if (condition 3)
{
// code to be executed if condition 1 and 2 are false and condition 3 is true
}
else
{
// code to be executed if all conditions are false
}
Sample program using else-if ladder as follows
#include <stdio.h>
int main()
{
int x = 5;
if (x > 0)
{
printf("x is positive
");
}
else if (x == 0)
{
printf("x is zero
");
}
else
{
printf("x is negative
");
}
return 0;
}
Switch Statement in C
In C, the switch
statement is a control statement that allows you to execute a block of code based on the value of a variable or expression. It is often used as an alternative to the else-if ladder
.
Here is the basic syntax of the switch
statement:
switch (expression)
{
case value 1:
// code to be executed if expression == value 1
break;
case value 2:
// code to be executed if expression == value 2
break;
...
default:
// code to be executed if expression does not match any of the values
}
The expression
is evaluated and compared to each of the case
values. If a match is found, the code associated with that case
is executed. The break
statement is used to exit the switch
statement and prevent the code for the following case
from being executed. If no match is found, the code in the default
block is executed (if it is present).
Sample program using switch statement as follows
#include <stdio.h>
int main()
{
int x = 5;
switch (x)
{
case 1:
printf("x is 1");
break;
case 2:
printf("x is 2");
break;
case 3:
printf("x is 3");
break;
default:
printf("x is not 1, 2, or 3");
}
return 0;
}
This program will print "x is not 1, 2, or 3" to the console because the value of x
is 5, which does not match any of the values in the case
clauses.
Note that the break
statements are important because they cause the switch
statement to exit after the code for the matching case has been executed. If you omit the break
statement, the code for all subsequent cases will also be executed.