Conditions Statements in C++

As you just know that C++ supports logical conditions from the mathematics like:

  • Less than a < b
  • Less than or equal to a <= b
  • Greater than a > b
  • Less than or equal to a >= b
  • Equal to a == b
  • Not equal to a != b

To perform different actions for different decition, we can use the above conditions.

C++ is no exception from other programming language, it has the following conditions:

  • You can use ifkayword to allow a block of code or statement to be executed, if only a certained condition is true.
  • You can use elsekeyword to allow a block of code or statement to be executed, only if a certain condition is false.
  • You can use else ifkeyword to add or specify a new condition or statement to be executed, only if the first condition is not fulfilled which is is false.
  • You can use switchkeyword to allow many selective blocks of code to be executed.

The if Statement

As i mentioned the ifstatement is allow the certain block of code to be executed only if the condition is true.

Syntax:

if (condition) {
// block of code to be executed if the condition is true
}

Let's take an example, where we will test two numbers to see if 10is greater than 5. Now if the condition fulfilled which is truethen we will print something on the screen.

// condition statement
#include <iostream>
using namespace std;

int main(){
  
  if(10 > 5){
    cout <<"10 is Greater Than 5 " << endl;
  }
  return 0;
}

Not only numeric values we can also test variables.

// condition statement
#include <iostream>
using namespace std;

int main(){
  int a = 10;
  int b = 5;

  if(a > b){
    cout <<"a is Greater Than b " << endl;
  }
  return 0;
}

In the above example, we test to see if a is greater than b using greater operator >. Because a is 10 and b is 5, so deffinately 10 is greater than 5, that is why we print on the screen that "a is Greater than b".

The else Statement

As I mentioned also the elsestatement is allow the certain block of code or statement to be executed only if the condition is false.

Syntax:

if (condition) {
  // block of code to be executed if the condition is true
}else{
  // block of code to be executed if the condition is false
}

Example:

// condition statement
#include <iostream>
using namespace std;

int main(){
  int time = 20;
  
  if(time < 18){
    cout <<"Good day!" << endl;
  }else{
    cout <<"Good evening!" << endl;
  }
  return 0;
}

In the above example, time has value of 20 and is greater than 18, hense the condition is false. Because of the condition is not fulfilled, so the programe move to the else section of the code and print "Good evening!" on the screen.

The else if Statement

As you know that else ifif adding new condition that will be executed if the first one is not fulfilled which has to be false.

Syntax:

if (condition1) {
  // block of code to be executed if the condition is true
}else if(condition2){
  // block of code to be executed if the condition1 is false and condition2 is true
}else{
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example:

// condition statement
#include <iostream>
using namespace std;

int main(){
  int time = 22;
  
  if(time < 10){
    cout <<"Good morning!" << endl;
  }if(time < 20){
    cout <<"Good day!" << endl;
  }else{
    cout <<"Good evening!" << endl;
  }
  return 0;
}

In the above example, time has value of 22 and is greater than 10, hense the first condition is false.Then it will move to the second condition which is else if statement, which is also false. Then the program has to move to the else section of the code and print "Good evening!" on the screen.

Ternary Operator

The short-hand form of if elseif called Ternary Operator and it consist of three operands.

The power of Ternary Operator is it can be use to replace multiple line of code in a single statement.

Syntax:

variable identifier = (condition) ? expressionTrue : expressionFalse;

Instead of to write code like this:

// condition statement
#include <iostream>
using namespace std;

int main(){
  int time = 20;
  
  if(time < 18){
    cout <<"Good day!" << endl;
  }else{
    cout <<"Good evening!" << endl;
  }
  return 0;
}

We can maximize it like thie:

// condition statement
#include <iostream>
using namespace std;

int main(){
  int time = 20;
  
  string greeting = (time < 18) ? "Good day!" : "Good evening!";
  cout << greeting;
  return 0;
}

Real World Examples

A programe to find if the number is positive or negative using if else statement.

// Condition statement
#include <iostream>
using namespace std;

int main(){
  int num = 10;
  
  if(num > 0){
    cout << "The Value is Positive " << endl;
  }else if(num < 0){
    cout << "The Value is Negative " << endl;
  }else{
    cout << "The Value is 0 " << endl;
  }

  return 0;
}

A programe to find the door Pin Code using if elsestatement.

// Condition statement
#include <iostream>
using namespace std;

int main(){
  int pinCode = 1032;
  
  if(pinCode == 1032){
    cout << "Access Granted \n The Door is Open now \n";
  }else{
    cout << "Access Denied \n The Door remains closed \n";
  }
  return 0;
}

A programe to find out if the number is Even or Odd using if elsestatement.

// Condition statement
#include <iostream>
using namespace std;

int main(){
  int myNum = 5;
  
  if(myNum % 2 == 0){
    cout << myNum <<" is Even.\n";
  }else{
    cout << myNum <<" is Odd.\n";
  }
  return 0;
}

Let's learn about other type of condition statement, which is Switch statement in next chapter.