if else statement in C++ with examples

Moving forward with this tutorial today we will lean about if-else statement. before this we should learn comparison operator hence let us see this 1st below. As we have seen some operators earlier like +, - ... Comparison operator given Boolean output, meaning that they tell if statement is true or false.
  • < and > : Eg 3 > 1 given true but 3 < 1 gives false. Similarly 3 > 3 gives false because 3 is not greater than 3. 3 < 3 is also false.
  • <= and >= : They are similar as above but 3 >= 3 and 3 <= 3 will give true. Note that =< can not be used in place of <=. = should come after > or <.
  • == : Tells if 2 valued are equal. This operator can compare int, char... every datatype. Remember that == is not same as =. = is assignment operator which assigns value on right side to variable on left sidebut == compares 2 values.

Now we will see how to use them in if else.
Format of if statement is following.

if(booelan expression1) {
statement1;
} else if(booelan expression2) {
statement2;
}
.....
....
....
else {
statementN;
}

boolean expression evaluates to true or false. If booelan expression1 evaluates to true then only statement1 will be executed. If booelan expression1 is false we see booelan expression2. If this is true then statement2 will be execute. Similarly we will ahead. Wherever booelan expression evaluates to true, related statement will be execute, nothing else. If none of booelan expression evaluates to true then statements under else will be executed. Lets see this by an example
In the example given below based on total marks and obtained marks we will calculate percent then grade and print it.
#include <stdio.h>

int
main() {

int
total_marks = 500;
int
obtained_marks = 272;

int
percent = obtained_marks*100/total_marks;

if
(percent >= 60) {
printf("Congrats!! You passed in 1st division.\n");
printf("Your percentage is %d.\n",percent);
}

else if
(percent >= 45) {
printf("You passed in 2nd division.\n");
printf("Your percentage is %d.\n",percent);
}

else if
(percent >= 33) {
printf("You just passed in 3rd division.\n");
printf("Your percentage is %d.\n",percent);
}

else
{
printf("Sorry! you failed.\n");
printf("Your percentage is %d.\n",percent);
}


scanf("%s");
return
1;
}

Here value of percent will be calculated as 54.(Note that it's value won't be 54.4 because in our progrm it's not int) percent is below 60 hence 1st condition (percent >= 60) will give false hence we will check next condition(percent >= 45) which will be true hence on screen this will be printed.
You passed in 2nd division.
Your percentage is 54

If any of the condition is true, only related statement will be executed, subsequent conditions will not be checked, hence this program will not print anything else. One more thing to note that we have used \n inside printf which is not printed because meaning of \n is new line hence whatever comes after \n will be printed on new line. You can try to remove \n and run program again. Similarly try to change value of obtained_marks in given example and run C/C++ program and see output carefully. If you have any question related to this, comment below to ask.

In next topic we will see some other examples related to if-else.
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.