How to use if-else statement in C Programming

In previous post we discussed about if statement in C Programming. If-else statement called as double selection structure in C Programming. In simple words it says “Do something when a condition is true, else (when condition is false) do some other thing”. Following simple c program explains how to use if-else statement in C Programming.

[js]

#include <stdio.h>

int main(void){
int number1; //defined variable named as number1
int number2; // defined variable named as number2

printf("Please Enter First Integer Number:n");
scanf("%d", &number1); // getting variable value from user and assiging value to variable named as number1
printf("n");
printf("Please Enter Second Integer Number:n");
scanf("%d", &number2); // getting variable value from user and assiging value to variable named as number2

if(number1 == number2){
printf("%d is equals to %dn", number1, number2); // first %d will hold the value of the variable mentioned after firs comma i.e number1.
//second %d will hold the value of the variable mentioned after second comma i.e number2

}else{
printf("%d is not equals to %dn", number1, number2); // this statement will always run when number1 is not equals to number2

if(number1 > number2){ // this condition will be true if number1 is greater than number2
printf("%d is greater than %dn", number1, number2);

}else if(number1 < number2){ // this condition will be true if number1 is lesser than number2
printf("%d is less than %dn", number1, number2);
}

}

return 0;
}
[/js]

Output:

2 thoughts on “How to use if-else statement in C Programming

Leave a Comment