As we have discussed decision making in detail in our previous post, we are now able to apply those concepts to write simple c++ programs. In this post, we will write a c++ program to find largest number among three numbers by nested if-else and compound conditional statements to compare them. In our comparison, we will not only compare the code but also the effective use of nested if-else and compound conditional statements.
C++ program to find largest among three numbers using a nested if-else statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; int main() { int num1, num2, num3; //----------taking input------------ cout << "enter first number" << endl; cin >> num1; cout << "enter second number" << endl; cin >> num2; cout << "enter third number" << endl; cin >> num3; //--------comparison---------------- if (num1 > num2) { if (num1 > num3) cout<<num1 <<" is largest"<<endl; else cout << num3 << " is largest" << endl; } else if (num2 > num3) cout << num2 << " is largest" << endl; else cout << num3 << " is largest" << endl; system("pause"); return 0; } |
We have three variables num1, num2, and num3 for storing three numbers to write our program. We can use hard code values but I am taking input from the user in this program. As we have three numbers, we must compare all of them to find which one is the largest. Firstly, we are comparing num1 with num2 using the outer if of nested if-else statement(line 14). It has two possibilities.
- If num1 is greater than num2 then the control of the program is passed to the inner if statement where num1 is compared with num3. Here if inner if condition(num1>num3 line 16) is satisfied then num1 is declared as the largest of all numbers. But if the condition(num1>num3 line 16) is false, the program selects num3 as the largest number.
- If num1 is not greater than num2 then the else-if statement(line 21) is used to compare num2 with num3. If num2 is larger than num3 then the program displays num2 as the largest number. Else, num3 is printed(line 24) as the largest of all numbers.
C++ program to find largest among three numbers using compound conditional statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> using namespace std; int main() { int num1, num2, num3; //---------taking input----------- cout << "enter first number" << endl; cin >> num1; cout << "enter second number" << endl; cin >> num2; cout << "enter third number" << endl; cin >> num3; //----------comparison----------- if (num1 > num2 && num1>num3) { cout<<num1 <<" is largest"<<endl; } else if (num2 > num3) cout << num2 << " is largest" << endl; else cout << num3 << " is largest" << endl; system("pause"); return 0; } |
We have already written the same program using the nested loop above. Now we are writing it using compound conditional statements to compare which one is better and why. The code up to line 13 is exactly the same. To find the largest among three numbers, num1 is compared with num2 and num3 using AND operator. It has two possibilities.
- If both conditions at line 14 are true then num1 is displayed as the largest number of all three numbers.
- If any of the conditions is false then num2 is compared with num3 (line 18) using else-if statement. If num2 is larger than num3 then num2 is declared as the largest number(line 19). Otherwise, num3 is printed as the largest.
Nested if-else Vs compound conditional statements
The effective use of both methods varies according to the scenario but compound conditional statements are better than nested if-else. It reduces lines of code, decreases complexity, and makes the code easy to understand. The above-stated example was a very simple program for demonstration. The difference between lines of code will differ prominently when we handle complex programs. From above simple example, we can make following conclusions.- Compound conditional statements reduce lines of code.
- It makes the code easy to understand and readable.
- It reduces the complexity during implementing your logic.
Output of both programs
Related post
c++ program to check whether a character is vowel or consonant
0 Comments