if else if statement can be used to choose one block of statements from many blocks statements. It is used when there are many options but only one block can be executed on the basis of a condition.
Syntax
The syntax is as follows :
if (condition)
block 1;
else
if (condition)
block 2;
else
if (condition)
block 3;
.
.
.
else
block n;
Working :
When the first condition becomes true then first block gets executed.
When first condition becomes false then second block is checked against the condition. If second condition becomes true then second block gets executed.
if else if Flowchat :
Example :
#include <iostream> using namespace std; int main () { // declare local variable int marks = 55; // check the condition if( marks >= 80 ) { // if 1st condition is true cout << "U are 1st class !!" << endl; } else if( marks >= 60) { // if 2nd condition is false cout << "U are 2nd class !!" << endl; } else if( marks >= 40) { // if 3rd condition is false cout << "U are 3rd class !!" << endl; } else { // none of condition is true cout << "U are fail !!" << endl; } return 0; }
Output :
U are 3rd class !!
No comments:
Post a Comment