C if else Statement

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.

  • If statement
  • If-else statement
  • If else-if ladder
  • Nested if

if Statement

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.

Syntax
if (expression) {
    //statement (code to be executed)
}

Flowchart of if statement in C

if statement in c

Let's see a simple example of C language if statement.

snippet
#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;
}
Output
Enter a number:4 4 is even number enter a number:5

Program to find the largest number of the three.

snippet
#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"); 
    }
}
Output
Enter three numbers? 12 23 34 34 is largest

if-else Statement

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.

Syntax
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

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.

snippet
#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;
}
Output
enter a number:4 4 is even number enter a number:5 5 is odd number

Program to check whether a person is eligible to vote or not.

snippet
#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"); 
	}
}
Output
Enter your age?18 You are eligible to vote... Enter your age?13 Sorry ... you can't vote

if else-if ladder Statement

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.

Note
The final else is optional and, if it is missing, the default action is no action.
Syntax
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

if-else-if ladder statement in c

The example of an if-else-if statement in C language is given below.

snippet
#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;
}
Output
enter a number:4 number is not equal to 10, 50 or 100 enter a number:50 number is equal to 50

Program to calculate the grade of the student according to the specified marks.

snippet
#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 ..."); 
	}
}
Output
Enter your marks?10 Sorry you are fail ... Enter your marks?40 You scored grade C ... Enter your marks?90 Congrats ! you scored grade A ...
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +