C++ nested-if Statements :

The nested-if Statement :

It is always legal to nested if-else statements, which means you can use one if or else statements inside another if or else statements (C++ else if is a like doing another if condition for a true or false value).

Syntax :

  1. if(condition)
  2. {
  3. //execute your code
  4. }
  5. else if
  6. {
  7. //execute your code
  8. }
  9. else
  10. {
  11. //execute your code
  12. }

Example :

  1. /* Description: Simple nested if-else statement program in C++ language */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int a=15,b=20;
  7. if(b>a)
  8. {
  9. if(b>a)
  10. {
  11. cout << "\n b is greater than a."<<endl;
  12. }
  13. else if(a>b)
  14. {
  15. cout << "\n a is greater than b."<<endl;
  16. }
  17. else
  18. {
  19. cout << "\n Both are equal."<<endl;
  20. }
  21. }

Output :

C++ language Simple nested if-else statement program in C++ language

Note : if here we set same value on a or b ( a=10,b=10 ) than consol output result is - Both are equal.


Computer Science Engineering

Special Notes

It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.

CSE Notes