Curriculum
This article is about the if statement in C programming and how and where it can be used in the program.
The If statement is used when you have to execute a block of statements only when the condition is true. There are various If statements like if else, else if, else and nested if.
Syntax if(condition) { … //set of statements … }
Here the foremost thing is that the compiler will check the condition given. If the condition is true then the set of statements inside ‘if’ will be executed but if the condition returns false then the statements inside ‘if’ will be skipped.
#include<stdio.h> int main() { int age = 18; if (age>=18) { printf("eligible to vote"); } return 0; }
Output
eligible to vote
In this example, the condition ‘ age>=18 ’ is checked first, since it is 18 the statement inside ‘if’ is executed.
Multiple If statements can also be used to check more than one condition.
#include<stdio.h> int main() { int a, b; printf("Enter value a:n"); scanf("%d", &a); printf("Enter value b:n"); scanf("%d", &b); if (a>b) { printf("a is greater than b"); } if (a<b ) { printf("a is lesser than b"); } if (a==b) { printf("a equals b"); } return 0; }
Output
Enter value a:10
Enter value b:20
a is lesser than b