In this tutorial you will learn about the C if else if Statement and its application with practical example.
C if else if Statement
In C programming, the if-else if statement allows us to add an alternative set of test conditions in the if..else statement using else-if and single else statements for the if condition. In such way if..else.if statement is used to select one among several blocks of code to be executed.
Table Of Contents−
C if..else..if Statement Flow Diagram
if else if Statement Syntax
Below is the general syntax of the if-else if statement in c programming:
Syntax:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(condition1) { // statement(s) } else if(condition2){ // statement(s) } . . else if(conditionN){ // statement(s) } else { // statement(s) } |
if-else if Statement Example
Below is a simple example to demonstrate the use of if-else if statement in c programming:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> void main() { int a = 10; int b = 10; printf("W3Adda - C if else if Statement\n"); if(a > b){ printf("a is greater than b\n"); } else if(a == b){ printf("a and b are equal\n"); } else { printf("b is greater than a\n"); } } |
Output:-