Sometimes, we want to carry out one set of instructions only when a certain condition is satisfied, and another set only when that condition is not. A decision control system is used in C to handle situations of this nature.
The decision control instruction is implemented in C using the if keyword, just like in any other programming language. The if statement’s condition is always encased in a pair of parentheses. The set of statements that follow the if statement will be executed if the condition is met. Additionally, the statement will not be executed if the condition evaluates to false; instead, the programme will skip the enclosing portion of code.
If the expression after the if evaluates to true, the statement contained in the if block will be carried out. However, if the if block is followed by an else block, a sequence of statements in the else block will be executed when the if block’s condition is false.
The syntax for if-else statements is as follows:
if ( condition ){
statements;}
else {
statements;}
One scenario where the if-else phrase could be utilised is
#include <stdio.h>
int main()
{
int num = 10;
if (num <= 10)
{
printf("Number is less than equal to 10.");
}
else
{
printf("Number is greater than 10.");
}
return 0;
}
Output: Number is less than equal to 10.