| by Arround The Web | No comments

Else-If Statement in C++

When a piece of code needs to be evaluated based on several different conditions, the “if…else if…else” statement is used. While deciding between two options, the if-else statement is utilized to execute a segment of code. The “if…else if…else” statement is used, however, when we must choose amongst multiple possibilities. Although there is only one “if” and one “else” in this control framework, there can be several else-if blocks.

Syntax:
The syntax for the else-if statement is as follows:

if (cond-1)
    statement;
else if (cond-2)
    statement;
.
.
else
    statement;

Using an Else-If Statement to Check the Value of a Variable

The if-else-if statements can be used to check the value of a variable that is already declared in the program. The first if-statement contains the possible variable value as its condition. If the condition becomes true, the block of code within that if-statement is executed. If the condition does not evaluate to “True”, the control moves to the next else-if statement and checks the condition. In case none of the if-else-if conditions gets “True”, the last else-statement is executed.

#include <iostream>
using namespace std;
int main()
{
  int Num = 101;
  if ( Num == 100)
    cout << "Num is 100";
  else if (Num == 101)
    cout << "Num is 101";
  else if (Num == 102)
    cout << "Num is 102";
  else
    cout < "Num does not exist.";
}

For the given block of code, we first include the <iostream> library. This library allows us to work with the input/output operations. The next step adds the “namespace std” so that everything that we use in this program are available under the namespace standard library. Now, coming to our program, we have the “main()” function with the “int” type. This means that our program returns some integer value when executed.

Within our “main()” function, we declare an integer type of variable as “Num” and assign it with the value of “101”. The next line of the program defines our first if-statement where the condition is the value of “Num ==100”. The control checks this condition by putting the value of “Num” and comparing it with the condition. Since the value of “Num” is 101, the condition becomes false. The control does not execute the if-statement block of code and moves to the next else-if statement.

Here, the condition says that “Num” is equal to 101. The value of “Num” is checked. The condition becomes true. The control executes the set of statements that are defined within it which is “Num is 101”. As soon as the condition is true, the program terminates and skips all the other conditions that are specified after the “True” condition. Here, the other two “else-if” and the “else” parts of the program are ignored, and the control terminates the program without checking them since our condition is met at the first else-if statement successfully.

We can see the “NUM is 101” statement in the provided snapshot of the output:

Using an Else-If Statement to Check the Grade of the Student by Taking the Input from the User

For this illustration, we find out the grade of a student by taking marks as input. Then, the marks are checked for a corresponding grade with the if-else-if statements. The grade is determined if any of the if-else-if statement matches the condition. If none of the conditions meet, the last “else” statement is executed.

#include <iostream>
using namespace std;

int main() {
  int M;
  cout << "Please Enter Marks: ";  
    cin >> M;    

  if (M > 90) {
    cout << "Your grade is A.";
  } else if (M > 80) {
    cout << "Your grade is B.";
  } else if (M > 70) {
    cout << "Your grade is C.";
  } else {
    cout << "Your grade is D.";
  }
  return 0;
}

The prerequisite libraries are included in the program. The “main()” function initiates and we declare a variable “M” with the “int” datatype. Now, we take the value of this “M” variable which would be “marks” from the user. To take the input from the user, we first use the “cout” object that displays the following statement on the “Please Enter Marks:” console. Then, we use the “cin” object in the next line which takes the input from the user and stores it in the “M” variable.

Once the user enters the marks, the control moves to the next line of the code where the first if-statement begins. The first condition is checked against the user-provided input, i.e. if the marks are greater than 90. If the condition matches, the inner code block is executed which is “Your grade is A.” Since the input marks are not greater than 90, the condition fails and the control does not check the statement and moves to the next else-if statement which has an “M>80” condition.

Since the user entered “81”, the value of “M” is checked and the condition is set to “True”. The control moves to execute the inside code which requires it to display “Your Grade is B.” Since our condition is matched, no other statements are verified for the variable “M”. The program immediately terminates, providing the met condition’s output on the console.

Using If-Else-If with Multiple Conditions

We can use the logical operator “&&” to check the multiple conditions inside the if-statement. The “&&” operand takes two binary values as conditions. The program evaluates both conditions. If both of them are matched, the operand returns true and the statement within the if-block is executed. If one of the conditions is false, the operand returns false and the control moves to the next block of code, ignoring the statement within the if-block.

#include <iostream>
using namespace std;

int main()
{
    int len;
    cout << "Enter length between 1 to 200: ";
    cin >> len;

    if (len > 1 && len < 100)
       {
             cout << "Entered length is between 1 and 100";
}
else if (len > 100 && len < 200)
{  
      cout << "Entered length is between 100 and 200";
}
else
{
     cout << "The Entered Length is greater than 200";
}
    return 0;
}

The previously-generated example has an integer variable “len” which takes the length from the user. The “cout” object displays the “Enter length between 1 to 200” statement and the “cin” object takes the input value and stores it in the “len” variable.

We have our first if-statement which has two conditions. The first condition checks if the “len” is greater than “1”. The “&&” operator specifies that the next condition must also be met. The second condition is “len<100”. If both conditions match, the if-block is executed. If it gets false, the next else-if statement is checked which has the “len>100” conditions, the “&&” operand, and then “len<200”.

Again, both conditions are checked. Conditions do not meet so the control jumps to the last else statement which has the “The entered length is greater than 200” statement.

Since the user enteres the length “213”, both the if-statement and the else-if statements are evaluated as false. Ultimately, the last “else” statement is executed.

Conclusion

This article initially introduced the concept of if-else-if conditional statements. Then, the basic syntax to practically implement this concept is provided. Furthermore, to elaborate on the implementation and execution of the else-if statements, we carried out three illustrations, each with a complete and easy-to-understand explanation with the corresponding output snapshots.

Share Button

Source: linuxhint.com

Leave a Reply