As you might know, an if statement is the one that allows the user to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user’s input.

Without a conditional statement such as the if statement, programs would run almost the exact same way every time. If statements allow the flow of the program to be changed, and so they allow algorithms and more interesting code.

When using an If statement it is important to know that the possible return will always be true or false, according to the parameters that the user gave to the program. This parameters will be declared with operators which are the ones who check if the condition is true or false.

For example:

>     Greater than              5 > 4 is TRUE

<     Less than                 4 < 5 is TRUE

>=    greater than or equal     4 >= 4 is TRUE

<=    less than or equal        3 <= 4 is TRUE

==    Equal to                  5 == 5 is TRUE

!=    not equal to              5! = 4 is TRUE

When a condition is true the result will be 1 and when the condition result to be negative the return will be 0.


Basic structure of an IF statement.

If (TRUE) {

Execute the next statement}


As you can see, the structure is pretty clear. Inside the parenthesis there are the conditions and outside there are the actions that the program will do if the statement is true.


This statement works when we want to give an alternative response when our IF condition results to be false. So this statement basically says what we want to do if our first statement isn’t true.

An ELSE structure will look like this:

If (TRUE) {

  // execute these statements if TRUE


Else {

  // execute these statements if FALSE



But what happen if we want to declare two or more conditions of IF? That is the situation when we use ELSE IF. When we use an “else if” statement following an if statement and its body; that way, if the first statement is true, the “else if” will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed.

For example:

If (<condition>) {

// execute these statements if <condition> is TRUE


Else if (<another condition>) {

// execute these statements if <another condition> is TRUE and

// <condition> is FALSE


Thank’s for the support of cprogramming.com, it really helped me a lot!

-The admin.

CC BY 4.0 Use of IF and ELSE in C++ by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.