Conditional statements are an indispensable part of any programming language, and C programming is no exception. These statements allow a program to make decisions based on certain conditions, enabling it to execute different blocks of code depending on the situation. In C, two commonly used conditional statements are if-else
and switch-case
.
The if-else
statement in C is used to control the flow of a program based on a condition. It allows different blocks of code to be executed depending on whether a given condition is true or false. The basic syntax of the 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, condition
is an expression that evaluates to either true or false. If the condition is true, the block of code inside the curly braces after the if
statement is executed. Otherwise, if the condition is false, the block of code inside the else
statement is executed.
Let's consider an example to understand the if-else
statement in action:
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
printf("You are eligible to vote!\n");
}
else
{
printf("You are not eligible to vote!\n");
}
return 0;
}
In this example, the user is prompted to enter their age. The program then checks whether the age is greater than or equal to 18. If the condition is true, it prints "You are eligible to vote!"; otherwise, it prints "You are not eligible to vote!".
The switch-case
statement provides an alternative way to handle multiple possible conditions. It allows the program to evaluate an expression and perform different actions based on its value. The basic syntax of the switch-case
statement is as follows:
switch (expression)
{
case value1:
// Code to be executed if expression equals value1
break;
case value2:
// Code to be executed if expression equals value2
break;
...
default:
// Code to be executed if expression doesn't match any case
}
Here, expression
is the value that is evaluated, and the case
statements represent the possible values. If the value of expression
matches any of the case
statements, the corresponding block of code is executed. The break
statement is used to end each case block, ensuring that only the relevant code is executed.
Let's consider an example to understand the switch-case
statement in action:
#include <stdio.h>
int main()
{
char grade;
printf("Enter your grade: ");
scanf("%c", &grade);
switch (grade)
{
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
case 'C':
printf("Fair!\n");
break;
case 'D':
printf("Needs improvement!\n");
break;
case 'F':
printf("Failed!\n");
break;
default:
printf("Invalid grade!\n");
}
return 0;
}
In this example, the user is prompted to enter their grade. The program then evaluates the value of grade
using the switch-case
statement. Depending on the value, it prints an appropriate message. If the value doesn't match any of the cases, the program executes the code inside the default
block.
Conditional statements, such as if-else
and switch-case
, allow programs to make decisions based on different conditions. These statements greatly enhance the flexibility and control of a program by enabling it to execute specific code paths based on the values of certain variables or expressions. Understanding how to use these statements effectively is crucial in mastering the C programming language.
noob to master © copyleft