If else Programs in C Programming
If else Programs in C programming
The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in if block is executed.
If Statement
Depending on the validity of that condition, the if statement is used to test the condition and perform the operations. It is mostly used in the scenario for which there exist different conditions; we need to perform the different operations. The if statement syntax is provided below.
if(expression){ //code to be executed } Let's see a simple example of C language if statement. #include <stdio.h> int main() { int x = 50; int y = 52; if (x<y) { printf("Variable x is less than y"); } return 0; }
Output:
Variable x is less than y
Program to use multiple if statement to check more than one conditions.
#include <stdio.h> int main() { int x, y; printf("Enter the value of x:"); scanf("%d", &x); printf("Enter the value of y:"); scanf("%d", &y); if (x>y) { printf("x is greater than y\n"); } if (x<y) { printf("x is less than y\n"); } if (x==y) { printf("x is equal to y\n"); } printf("End of Program"); return 0; }
Output:
Enter the value of x: 12 Enter the value of y: 12 x is equal to y End of Program
If-else Statement
The if-else statement is used to achieve two single condition operations. The if-else statement is an extension of the if statement that allows one to perform two different operations, one for the correct condition, and the other for incorrect condition. Here we must remember that if block can't be executed simultaneously, then otherwise.
if(expression){ //if condition is true }else{ //if condition is false }
Example of simple if-else statement
#include<stdio.h> void main() { int num; printf("Enter the number: "); scanf("%d",&num); if (num>0) printf("%d is Positive."); else printf("%d is Negative."); }
Output:
Enter any number: 24 24 is Positive
Program to check whether a person is eligible to vote or not.
#include <stdio.h> int main() { int age; printf("Enter your Original age:"); scanf("%d",&age); if(age>=18) { printf("You are legal to vote"); } else { printf("Sorry ..you can't vote Now"); } }
Output:
Enter your Original age: 20
You are legal to vote...
Enter your Original age: 14
Sorry ..you can't vote Now.
If else-if ladder Statement
The ladder expression if-else-if is an extension of the state if-else. It is used in a scenario where there are multiple cases for different conditions to be performed. If a condition is true, then the statements specified in the if block will be executed in the if-else-if ladder statement, and if any other condition is true, then the statements specified in the other block will be executed. When the last condition is not valid, the statements specified in the other block are executed. Many other blocks-if possible. It is similar to the switch case statement, where if none of the cases match, the default is executed instead of another row.
if(condition1){ //code to be run if condition1 is true }else if(condition2){ //code to be run if condition2 is true } else if(condition3){ //code to be run if condition3 is true } ... else{ //code to be run if all the conditions are false }
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",&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.
#include <stdio.h> int main() { int marks; printf("Enter your marks:"); scanf("%d",&marks); if(marks > 85 && marks <= 100) { printf("You got grade A"); } else if (marks > 60 && marks <= 85) { printf("You got grade B +"); } else if (marks > 40 && marks <= 60) { printf("You got grade B"); } else if (marks > 30 && marks <= 40) { printf("You got grade C"); } else { printf("Fail"); } }
Output:
Enter your marks:10 Fail Enter your marks:40 You got grade C Enter your marks:90 you got grade A
One more example
#include <stdio.h> int main () { /* local variable definition */ int a = 100; /* check the boolean condition */ if( a == 10 ) { /* if condition is true then print the following */ printf("The value of a is 10\n" ); } else if( a == 20 ) { /* if else if condition is true */ printf("The value of a is 20\n" ); } else if( a == 30 ) { /* if else if condition is true */ printf("The value of a is 30\n" ); } else { /* if none of the conditions is true */ printf("There is No value Matching\n" ); } printf("The total value of a is: %d\n", a ); return 0; }
Output:
There is NO value Matching The total value of a is: 100