C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C if else StatementThe 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. There are the following variants of if statement in C language.
If StatementThe if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below. if(expression){ //code to be executed } Flowchart of if statement in C Let's see a simple example of C language if statement. #include Output Enter a number:4 4 is even number enter a number:5 Program to find the largest number of the three.#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 StatementThe if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition. The syntax of the if-else statement is given below. if(expression){ //code to be executed if condition is true }else{ //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 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.#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 StatementThe if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched. if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //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 Output
Output |