0% found this document useful (0 votes)
27 views6 pages

Working of The If-Else-If Ladder

The document explains the if-else-if ladder in programming, which allows checking multiple conditions sequentially, executing the corresponding block of code for the first true condition. It also describes the switch statement, a control structure that executes different blocks based on the value of a given expression, with examples in C++. Both structures are fundamental for flow control in programming.

Uploaded by

revathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

Working of The If-Else-If Ladder

The document explains the if-else-if ladder in programming, which allows checking multiple conditions sequentially, executing the corresponding block of code for the first true condition. It also describes the switch statement, a control structure that executes different blocks based on the value of a given expression, with examples in C++. Both structures are fundamental for flow control in programming.

Uploaded by

revathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

It is an extension of the if-else statement.

If we want to check
multiple conditions if the first ifcondition becomes false, use the if else-
if ladder. If all the if conditions become false the else block gets
executed.

if (condition1)
statement 1;
else if (condition2)
statement 2;
.
.
else
statement;
Working of the if-else-if ladder
1. Control falls into the if block.
2. The flow jumps to Condition 1.
3. Condition is tested.
1. If Condition yields true, goto Step 4.
2. If Condition yields false, goto Step 5.
4. The present block is executed. Goto Step 7.
5. The flow jumps to Condition 2.
1. If Condition yields true, goto step 4.
2. If Condition yields false, goto Step 6.
6. The flow jumps to Condition 3.
1. If Condition yields true, goto step 4.
2. If Condition yields false, execute else block. Goto
Step 7.
7. Exits the if-else-if ladder.
int main()
{
int i = 20;

// Check if i is 10
if (i == 10)
cout << "i is 10";

// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";

// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";
// If none of the above conditions is true
// Then execute the else statement
else
cout << "i is not present";

return 0;
}

int main()
{
int i = 20;

// Check if i is 10
if (i == 10)
cout << "i is 10";

// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";

// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";

// If none of the above conditions is true


// Then execute the else statement
else
cout << "i is not present";

return 0;
}
int main()
{

#include<iostream.h>
#include<conio.h>
Main()
{
int i = 20;

// Check if i is 10
if (i == 10)
cout << "i is 10";

// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";

// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";

// If none of the above conditions is true


// Then execute the else statement
else
cout << "i is not present";

return 0;
}

The switch statement in C++ is a flow control statement that is


used to execute the different blocks of statements based on the
value of the given expression. We can create different cases for
different values of the switch expression. We can specify any
number of cases in the switch statement but the case value can
only be of type int or char.
switch (expression) {
case value_1:
// statements_1;
break;
case value_2:
// statements_2;
break;
.....
.....
default:
// default_statements;
break;
}

The following example demonstrate how to use the switch


statement syntax in C++.
Example: C Program to demonstrate the
syntax of switch in C++
 C++

// C++ program to demonstrate syntax of switch


#include <iostream>
using namespace std;
// Driver Code
int main()
{
// switch variable
char x = 'A';

// switch statements
switch (x) {
case 'A':
cout << "Choice is A";
break;
case 'B':
cout << "Choice is B";
break;
case 'C':
cout << "Choice is C";
break;
default:
cout << "Choice other than A, B and C";
break;
}
return 0;
}

You might also like