Curriculum
This article is about the if-else statement in C and how you can use it with-in your C program.
The if statement followed by an additional else statement is known as the if-else statement. If-else statement is used in a condition where two operations have to be performed. If the condition is True it executes the if statement and if the condition returns False it executes the else statement.
Syntax
if (boolean expression)
{
/statements executes if the expression is true
}
else
{
/statements executes if the expression is false
}
#include <stdio.h> int main() { int a; printf("Enter age: n "); scanf("%d", &a); if (a >=18) { printf("eligible to vote"); } else printf("not eligible to vote"); return 0; }
Output
Enter age: 16
not eligible to vote
In the above example, it checks for the value ’a’ if it is greater than or equal to 18 then it executes the statements inside the ‘if’ block and if it is lesser than 18 then it skips the ‘if’ block and executes the ‘else’ block alone. The if-else statement cannot execute both the if and else blocks simultaneously.