C++ Programming Code Examples
Learn C++ Language
If Else If Ladder in C++ Programming Language
If Else If Ladder in C/C++
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements.
In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax of if...else Ladder in C++
if (Condition1)
{ Statement1; }
else if(Condition2)
{ Statement2; }
.
.
.
else if(ConditionN)
{ StatementN; }
else
{ Default_Statement; }
/* write a C program which demonstrate use of if-else-if ladder statement */
/* Program to Print Day Names using Else If Ladder in C++*/
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Enter Day Number: ";
cin >> day;
cout << "Day is ";
if (day == 1)
cout << "Sunday" << endl;
else if (day == 2)
cout << "Monday" << endl;
else if (day == 3)
cout << "Tuesday" << endl;
else if (day == 4)
cout << "Wednesday" << endl;
else if (day == 5)
cout << "Thursday" << endl;
else if (day == 6)
cout << "Friday" << endl;
else
cout << "Saturday" << endl;
return 0;
}
To declare such a function, precede its name with * and &. In the body of the function, you can do whatever is appropriate. An important rule with this type of "function" is that it must
To convert octal number to decimal number in C++ programming, you have to ask to the user to enter the octal number to convert it into decimal number to print the equivalent
To find the largest element in an array in C++ programming, enter the array size, enter the array elements. Start finding for the Largest element in the array to "Display the Largest"
In sometimes you have a "condition" and you want to execute a block of code if condition is true and execute another piece of code if the same condition is false. This can be achieved