×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

A simple example of nested if-else statement in C++

Nested if-else statement in C++: In this example, we are taking a character from keyboard and checking whether it is Vowel or Consonant, before it we are checking it is valid alphabet or not?

C++ Nested if-else statement

When we validate conditions within the condition, it is called Nested condition checks; here we are using Nested if-else statement.

Let suppose, if you have two conditions: CON_1 and CON_2 and you want to validate CON_2, when CON_1 is true, in such case we must have to use Nested if-else.

Example of nested if-else statement

Here we are reading a character from the user and validating that it is valid alphabet or not, if the character if valid alphabet then we are validating it is VOWEL or not and printing the appropriate message for the input character.

Consider the program:

// EXAMPLE of Nested if else
// Read a character a check whether it is VOWEL or CONSONANT

#include <iostream>
using namespace std;

int main() {
  char ch;

  // reading a character
  cout << "Enter an alphabet: ";
  cin >> ch;

  // condiion to check character is alphabet or not
  if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
    // conditions to check character is VOWEL or not
    if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' ||
        ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u')
      cout << "\"" << ch << "\" is a VOWEL" << endl;
    else
      cout << "\"" << ch << "\" is a CONSONANT" << endl;
  } else {
    cout << "\"" << ch << "\" is not an alphabet\n";
  }

  return 0;
}

Output

First run:
Enter an alphabet: D
"D" is a CONSONANT

Second run:
Enter an alphabet: e
"e" is a VOWEL

Third run:
Enter an alphabet: 8
"8" is not an alphabet

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.