Website For Developers

If else in C++



if else statements are the decision making statements in C++.
if else in C++ is followed by one or more Boolean expression statements.


if statement in C++

If the condition or Boolean expression evaluates to true, then the code block inside the if statement will be executed.
If condtion is false then compiler skip the if body.

Syntax

if(condition) {
   Statement 1;
   Statement 2;
   .
   .
   Statement N;
}


Example of if in C++

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  int marks;
  cout<< "Enter your marks you get in the exams"<< endl;
  cin>> marks;
  if (marks>=40) {
    cout<< "Congratulation, you have passed the exam. ";
  }
  getch();
  return 0;
}

Output

Enter your marks you get in the exams
87
Congratulation, you have passed the exam.

Explanation

In the above example, if user enter the number which is equal to or greater than 40 then if body executed but if user enter the number which is less than 40 compiler simply ignore the if body and move to very next line.



if else in C++

If statement with an optional else statement.
In which, if statement is executed only when the condition is true. And else statement is executed only when the condition is false.

Syntax

if (Condition) {
// Executes when Condition is true
}
else {
// Executes when Condition is false
}


Example of if else in C++

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  int age;
  cout<< "Enter your age: ";
  cin>> age;
  if (age<18) {
    cout<< "Sorry, your are under age." ;
    cout<< "You can't drive.";
  }
  else {
    cout<< "YahOOO! You can drive.";
  }
  getch();
  return 0;
}

Output

Enter your age: 18
YahOOO! You can drive.


Write a program to check the number is even or odd?

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  int number;
  cout<< "Enter an Integer value: ";
  cin>> number;
  if (number%2==0) {
    cout<< number<< " is an even number." ;
  }
  else {
    cout<< number<< " is an odd number.";
  }
  getch();
  return 0;
}

Output

Enter an Integer value: 78
78 is an even number.


else if in C++

When there are more than two options then we use else-if statement in C++ to cover all conditions.

Syntax

if (Condition 1) {
  // Execute when Condition 1 is true
}
else if (Condition 2) {
  // Execute when Condition 2 is true
}
else if (Condition 3) {
  // Execute when Condition 3 is true
}
else {
  // Executes when all Conditions are false
}

else if in C++ example

#include<iostream>
#include<conio.h>
using namespace std;
int main() {

  int score;
  cout<< "Enter your examination score"<< endl;
  cin>>score;

  if(score>=80) {
    cout<< "Your grade is A";
  }
  else if(score>=70 && score<80) {
    cout<< "Your grade is B";
  }
  else if(score>=60 && score<70) {
    cout<< "Your grade is C";
  }
  else if(score>=50 && score<60) {
    cout<< "Your grade is D";
  }
  else {
    cout<< "You have not passed the examination better luck next time. ";
  }

  getch();
  return 0;
}

Output

Enter your examination score
50
Your grade is D