If-else in C++

Introduction

Suppose you are writing a program and want to execute something only when a condition is satisfied. You would imagine how to do it. Well, C++ and other various other programming languages offer if-else which can be used to run a block of code only when a condition is met.

What is if-else?

If else is simply a control statement that controls your program by running a code block only when a specified condition is satisfied.

It means the same as “if this, then this, otherwise this”.

How to use it?

The syntax for if-else is as follows

if (condition) {
  // code
} 
else {
  // code
}

Let’s assume a simple problem statement. You are given a number, you need to print “YES” if it’s divisible by 9 otherwise “NO”.

Let’s try writing pseudocode for this

Start
input number
if number is divisible by 9
    print "YES"
else print "NO"
End

Now let’s try coding this pseudocode

Code:

C++ Code

#include<iostream>
using namespace std;

int main() {
  int number;
  cin >> number;
  if (number % 9 == 0) {
    cout << "YES" << "\n";
  } else {
    cout << "NO" << "\n";
  }
  return 0;
}

Using multiple if-else statements

For example, given a character, you need to check if it’s equal to ‘A’ or ‘Z’. If it is, then you print “Yes, it matches” or else you print “No, it doesn’t match”.

Here if the character matches with ‘A’ we print the statement and also print the same statement if it matches ‘Z’ else we print the other statement.

The following code explains the solution of the above approach

C++ Code

#include<iostream>
using namespace std;

int main() {
  char ch;
  cin >> ch;
  if (ch == 'A') {
    cout << "Yes, it matches" << "\n";
  } else if (ch == 'Z') {
    cout << "Yes, it matches" << "\n";
  } else {
    cout << "No, it doesn't match" << "\n";
  }
  return 0;
}

Using logical operators with if-else

We can also use logical operators with if-else. This can be a great solution when sometimes 2 conditions should satisfy simultaneously or either of the two conditions should satisfy. This can also work as a great alternative for multiple if-else statements.

Assuming the same problem as above – given a character, you need to check if it’s equal to ‘A’ or ‘Z’. If it is, then you print “Yes, it matches” or else you print “No, it doesn’t match”.

Here in this problem, we need to check either of the condition. If the character is either equal to ‘A’ or ‘Z’, then we execute a block of code. Hence we are performing an OR operation.

The code for this problem would look like this

Code:

C++ Code

#include<iostream>
using namespace std;

int main() {
  char ch;
  cin >> ch;
  if (ch == 'A' || ch == 'Z') {
    cout << "Yes, it matches" << "\n";
  } else {
    cout << "No, it doesn't match" << "\n";
  }
  return 0;
}

Nested if-else

Suppose we want to check for multiple conditions at a time and execute a specific code block, then we can use an if-else block inside an if block or nested if-else.

Assuming a problem statement, given a number, we need to check if that number is divisible by 9. If it is divisible by 9, then we need to check if it’s even or odd and print “Divisible by 9 and even” or “Divisible by 9 and odd”. If it’s not divisible by 9, we print “Not divisible by 9”.

Here first we check if the number is divisible by 9. If it is divisible, then we check if it is even or odd. 

Let’s check the code for this problem

C++ Code

#include<iostream>
using namespace std;

int main() {
  int number;
  cin >> number;
  if (number % 9 == 0) {
    if (number % 2 == 0) {
      cout << "Divisible by 9 and even" << "\n";
    } 
    else {
      cout << "Divisible by 9 and odd" << "\n";
    }
  } 
  else {
    cout << "Not divisible by 9" << "\n";
  }
  return 0;
}

Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article