The if-else statement in C is used to perform the operations based on some specific condition. The statements specified in if block are executed if and only if the given condition is true.
In this tutorial, you will learn about the if statement (including if…else and nested if..else) in C programming with the help of examples.
Different types of if statement in C language
- If statement
- If-else statement
- If else-if ladder
- Nested if
Contents
hide
If Statement
The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition.
Syntax
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
- The
if
statement evaluates the test expression inside the parenthesis()
. - If the test expression is evaluated to true, statements inside the body of
if
are executed. - If the test expression is evaluated to false, statements inside the body of
if
are not executed.
Flowchart
Example
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
if(number%2!=0){
printf("%d is odd number",number);
}
return 0;
}