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++, if..else..if statement allows us add alternative set of test conditions in if..else statement using else-if and single else statements for if condition. In such way if..else..if statement is used to select one among several blocks of code to be executed.
C++ if..else..if Statement Flow Diagram
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) } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { int a = 10; int b = 10; cout<<"\nW3Adda - C++ If else if Statement"; if (a > b) { cout<<"\na is greater than b"; } else if(a == b){ cout<<"\na and b are equal"; } else{ cout<<"\nb is greater than a"; } return 0; } |
Output:-