C++

Else if C++

While using the C++ programming language, there come such situations where you need some contradictory options, for instance. If you are applying any condition according to the scenario, you will proceed with two or more options. If the condition is satisfied, it will lead to one task; otherwise, some other function will be performed in the second condition. This article is based on the else-if statement in the C++ programming language.

In C++ programs, the Else-if statement is executed in the form of blocks. We utilize an else-if statement to execute one block of source code satisfying certain conditions and other code satisfying other conditions. An else-if statement is said to be a conditional statement as it is used to check the given condition, and according to this condition, the loops are executed.

Difference between if-statement and if-else statement

If- statement and else-if statements are the same. Only the discrimination lies in if statement is that this statement is only executed when the condition is true, and when the condition is false, the program gets stopped automatically. Whereas in the else-if statement, the statement is executed when it is true, and if the condition is false, it executes the statement that is written in the else part of the statement.

Else-if statement in C++ and its working

  • When the program starts, the compiler executes the earlier code; when the conditional statement is started, the control first falls into the block “if.”
  • Then the flow of the compiler leads it to the condition.
  • Then the given condition is tested. If the condition is satisfied and provides a true response, move to the next step. And if the condition is not satisfied, it yields a false result.
  • According to the true condition, the if-block or the code inside the body of the ‘if’ is executed.
  • On the other hand, the else part of the code inside it is executed.
  • The compiler terminates the else-if statement.

Implementation of else-if

Use Ubuntu Linux operating system to execute else-if statement in C++ programming language. To accomplish the task, we need a text editor and an execution portal for the execution of the source code.

Example 1

Now we have used a simple example to demonstrate the working of the Else-if statement in the C++ language. We will find if the number entered by the user is positive or a negative number. As this program deals with user interaction, we need to use the library of an input-output stream as a header file.

#include <iostream>

Then in the main program, the user is asked to enter any number of their choice.

Here the working of the else-if statement starts. First, the control is on the “if” part; if the number is equal or greater than 0, it means that the user has entered a positive number, so the statement is displayed to ensure the entry of a positive number. And if the number entered is smaller than 0, then the control will go to the else part, and a message is displayed that the number entered is negative. Then the program will be terminated.

Now we will execute the code. For the execution, we need a compiler for C++ codes. So we will install a G++ compiler in Ubuntu through the terminal. “Code. C” is the name of the file.

$ g++ -o code code.c
$ ./code

When the code is executed successfully, the message will be prompted. Then we will enter a positive number first to execute the if-block of the statement. And then, we will again execute the program to enter a negative number. In this case, the control will be transferred to the else part, and then the message will be displayed that the number entered is negative.

Example 2

The second example works on the same logic as the first example did. But the difference is that we have applied a nested if-else-if statement. In which an if-else statement contains another if-else statement inside it. After initiation of a library inside the main program, the number is taken from the user, and then we have directly used the “if” statement. That checks that if the number is not equal to zero, it is either a positive or negative number. If this is true, the control will transfer to the inner “if” part of the statement. This “if” part checks if the entered number is positive, means if the number is greater than 0, then a message of a positive number is displayed. And if the inner condition is false, the control is transferred to the inner else part. It only displays a message that the number is negative.

If the outer “if” condition described at the start is not satisfied, then the control is transferred to the outer else condition. This will display that the entered number is zero.

Now execute the code in the terminal. First, we will enter a positive number. It means the outer “if” part and the inner “if” part is executed, and the message is displayed, respectively. A line outside the nested statement is written, which shows that this line will be displayed in every situation.

Next time on execution, we have entered 0. So the outer else part will be operated. Whereas on using a negative number, the outer “if” and then the inner “else” block of code will be used.

Example 3

Here is another simple example that checks if the number is an even number or odd number. The user enters the number, and then the “if” statement checks the condition. The logic is that the modulus of 2 should be zero to declare the number as an even number. If the part is satisfied, the message is displayed that the number is even. And on the “else” part, a message is displayed that the number is odd.

Execute the code and then enter even and odd numbers randomly to check the working of the else-if statement.

Example 4

This example uses an else-if statement to check the eligibility criteria for voting purposes. The criterion set is to be an age of 18 or above.

First, we get the age from the user. The if-else statement checks the age. If part contains the condition, if the entered number is greater or equals 18, then display the message that the user is eligible. And the else part displays that the user is illegible.

Execute the source code file. First, we have entered a float number; then, the control is on the “else” part.

While entering 18, the control is on the “if” part.

Conclusion

“Else if C++” is an article that contains the basic knowledge of the if-else statement in the C++ programming language. This is a conditional statement that is easily applicable in all situations. It’s working with some easy examples is explained in the article. All examples are implemented in Ubuntu 20.04. If-else and nested else-if statements play an important role in using more than one option in daily life program usage.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.