Decision-making or control statements are used in programming to control the flow of program execution based on certain conditions. These statements allow the programmer to write flexible and efficient code that can make decisions and respond to different inputs or conditions.
There are three main types of control statements in C programming language:
All of these control statements are essential tools in any programmer’s toolbox and are used extensively in the development of complex software applications.
The “if statement” is a decision-making control statement in C programming that is used to execute a block of code if a condition is true.
The syntax of an if statement is as follows:-
if(Condition)
{
/ / Block of code to be executed if the condion is true.
}
Here, the “condition” is an expression that returns a boolean value (either true or false). If the condition is true, the block of code inside the curly braces will be executed. If the condition is false, the block of code will be skipped.
For example, let’s say we want to write a program that checks if a given number is Graterthen. We can use an if statement to accomplish this:
Example:-
#include <stdio.h>
int main()
{
int num = 5;
if (num > 0)
{
printf(“The number is positive\n”);
}
return 0;
}
In this program, the if statement checks if the value of “num” is greater than 0. Since 5 is indeed greater than 0, the block of code inside the if statement is executed and the message “The number is positive” is printed to the console. If the value of “num” were negative or zero, the block of code would be skipped and nothing would be printed.
The “if-else statement” is a decision-making control statement in C programming that is used to execute one block of code if a condition is true and another block of code if the condition is false.
The syntax of an if-else statement is as follows:-
if (condition)
{
// block of code to be executed if the condition is true.
}
else {
// block of code to be executed if the condition is false.
}
Here, the “condition” is an expression that returns a boolean value (either true or false). If the condition is true, the block of code inside the first set of curly braces will be executed. If the condition is false, the block of code inside the second set of curly braces will be executed.
For example, let’s say we want to write a program that checks if a given number is positive or negative. We can use an if-else statement to accomplish this:
Example :-
#include <stdio.h>
int main() {
int num = -2;
if (num > 0) {
printf(“The number is positive\n”);
}
else {
printf(“The number is negative\n”);
}
return 0;
}
In this program, the if-else statement checks if the value of “num” is greater than 0. Since -2 is less than 0, the block of code inside the else statement is executed and the message “The number is negative” is printed to the console. If the value of “num” were positive or zero, the block of code inside the if statement would be executed instead.
The ladder if-else statement in C programming is used to test a series of conditions sequentially, and each condition is tested only if all the previous conditions in the if-else ladder are false. If any of the conditional expressions evaluate to be true, the corresponding code block will be executed, and the rest of the ladder will be skipped.
The syntax of a ladder If-Else statement is as follows :-
if (Condition)
{
// block of code to be executed if the condition is true.
}
else if (Conditions) {
// block of code to be executed if the condition is true.
}
else if (Conditions) {
// block of code to be executed if the condition is true.
}
else {
// block of code to be executed if the condition is False.
}
Exmapmle :-
#include <stdio.h>
int main()
{
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num > 0)
{
printf(“The number is positive.\n”);
}
else if (num < 0)
{
printf(“The number is negative.\n”);
}
else if (num == 0)
{
printf(“The number is zero.\n”);
}
else {
printf(“Invalid input.\n”);
}
return 0;
}
The ladder if-else statement allows you to handle multiple cases and choose the appropriate code block to execute based on the first true condition encountered in the sequence.
A nested if-else statement in C programming allows you to have if-else statements within other if-else statements. This allows for more complex conditional logic and multiple levels of decision-making.
The syntax of a Nested If-Else statement is as follows :-
if (Conditions) {
if (Conditions) {
// block of code to be executed if the condition is true.
}
else {
// block of code to be executed if the condition is False.
}
}
else {
// block of code to be executed if the condition is False.
}
Here, the “expression” is an expression that returns an integer value. The switch statement evaluates the expression and executes the block of code that corresponds to the matching “case”. If no matching cases are found, the block of code in the “default” section is executed.
For example, let’s say we want to write a program that prints the name of a day of the week based on its corresponding number (1 for Monday, 2 for Tuesday, etc.). We can use a switch statement to accomplish this:
Exmapmle :-
#include <stdio.h>
int main()
{
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num >= 0)
{
if (num == 0)
{
printf(“The number is zero.\n”);
}
else
{
printf(“The number is positive.\n”);
}
}
else
{
printf(“The number is negative.\n”);
}
return 0;
}
The “switch statement” is a decision-making control statement in C programming that is used to execute different blocks of code depending on the value of an expression.
The syntax of a switch statement is as follows :-
switch (expression)
{
case value1:
// block of code to be executed if expression equals value1.
break;
case value2:
// block of code to be executed if expression equals value2.
break;
.
.
\/
default:
// block of code to be executed if expression doesn’t match any of the cases.
}
Here, the “expression” is an expression that returns an integer value. The switch statement evaluates the expression and executes the block of code that corresponds to the matching “case”. If no matching cases are found, the block of code in the “default” section is executed.
For example, let’s say we want to write a program that prints the name of a day of the week based on its corresponding number (1 for Monday, 2 for Tuesday, etc.). We can use a switch statement to accomplish this:
Exmapmle :-
#include <stdio.h>
#include <conio.h>
int main() {
int day = 4;
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”);
break;
}
return 0;
}
In this program, the switch statement evaluates the value of “day” and executes the block of code that corresponds to the matching case. Since the value of “day” is 4, the block of code inside the “case 4” section is executed and the message “Thursday” is printed to the console. If the value of “day” were 8 or any other value that doesn’t match the cases, the block of code in the “default” section would be executed instead, printing the message “Invalid day”.