C supports two selection statements: if
and switch
. In addition, the ?
operator is an alternative to if in certain circumstances. The if-else
statement in C is used to perform the operations based on some specific condition. The operations specified in if
block are executed if and only if the given condition is true.
Below are the variants of if statement in C language.
The if
statement evaluates an expression. If that expression is true, then a statement is executed. The syntax of the if
statement is given below.
if (expression) { //statement (code to be executed) }
Flowchart of if statement in C
Let's see a simple example of C language if statement.
#include<stdio.h> int main() { int number = 0; printf("Enter a number:"); scanf("%d", & amp; number); if (number % 2 == 0) { printf("%d is even number", number); } return 0; }
#include <stdio.h> int main() { int a, b, c; printf("Enter three numbers?"); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) { printf("%d is largest",a); } if(b>a && b > c) { printf("%d is largest",b); } if(c>a && c>b) { printf("%d is largest",c); } if(a == b && a == c) { printf("All are equal"); } }
The if
statement evaluates an expression. If that expression is true, then a statement is executed. If an else
clause is given and if the expression is false, then the else
's statement is executed. The syntax of the if statement is given below. The syntax of the if-else
statement is given below.
if (expression) { //statement (code to be executed if condition is true) } else { //statement (code to be executed if condition is false) }
Flowchart of the if-else statement in C
Let's see the simple example to check whether a number is even or odd using if-else statement in C language.
#include<stdio.h> int main() { int number = 0; printf("enter a number:"); scanf("%d", & amp; number); if (number % 2 == 0) { printf("%d is even number", number); } else { printf("%d is odd number", number); } return 0; }
#include <stdio.h> int main() { int age; printf("Enter your age?"); scanf("%d",&age); if(age>=18) { printf("You are eligible to vote..."); } else { printf("Sorry ... you can't vote"); } }
The if-else-if ladder statement is an extension to the if-else statement. The if-else
statement can also command multiple statements by wrapping them in braces. Statements
so grouped are called a compound statement, or block, and they are syntactically equivalent to a single if-else statement.
This chain is evaluated from the top and, if a particular if-conditional is TRUE, then its statement is executed and the chain is terminated. On the other hand, if the conditional is FALSE, the next if-conditional is tested. If all the conditionals evaluate to FALSE, then the final else statement is executed as a default.
if (expression1) { //statement1 (code to be executed if expression1 is true) } else if (expression2) { //statement2 (code to be executed if expression2 is true) } else if (expression3) { //statement3 (code to be executed if expression3 is true) } ... else { //optional //statement (code to be executed if all the conditions are false) }
Flowchart of else-if ladder statement in C
The example of an if-else-if statement in C language is given below.
#include<stdio.h> int main() { int number = 0; printf("enter a number:"); scanf("%d", & amp; number); if (number == 10) { printf("number is equals to 10"); } else if (number == 50) { printf("number is equal to 50"); } else if (number == 100) { printf("number is equal to 100"); } else { printf("number is not equal to 10, 50 or 100"); } return 0; }
#include <stdio.h> int main() { int marks; printf("Enter your marks?"); scanf("%d",&marks); if(marks > 85 && marks <= 100) { printf("Congrats ! you scored grade A ..."); } else if (marks > 60 && marks <= 85) { printf("You scored grade B + ..."); } else if (marks > 40 && marks <= 60) { printf("You scored grade B ..."); } else if (marks > 30 && marks <= 40) { printf("You scored grade C ..."); } else { printf("Sorry you are fail ..."); } }