Decision Making Statements – If else if statement

October18, 2017
by admin

Syntax:


  if(<conditional expression 1>)
  {
    // statement or statements to be executed
    // when conditional expression 1 returns 
    // true
  }
  else if(<conditional expression 2>)
  {
    // statement or statements to be executed
    // when conditional expression 2 returns 
    // true
  }
  ........
  ........
  else if(<conditional expression n>)
  {
    // statement or statements to be executed
    // when conditional expression n returns 
    // true
  }
  else
  {
    // statement or statements to be executed
    // when NONE of the above conditional expressions returns 
    // true
  }

Ex:

Problem:

Get the marks obtained in the exam from the user. Display the appropriate message to enter the marks between 0 to 100. Find the grade obtained by a student based on the marks entered and display appropriate message to console output showing the grade.

Solution:


  int marks;

  System.Console.WriteLine("Enter Marks obtained in the exam (0 to 100): ");
  marks = Convert.ToInt32(Console.ReadLine());

  if(marks >= 75)
  {
    System.Console.WriteLine("You have passed with first grade");
  }
  else if(marks >= 60 && marks <75)
  {
    System.Console.WriteLine("You have passed with second grade");
  }
  else if(marks >= 40 && marks <60)
  {
    System.Console.WriteLine("You have passed with third grade");
  }
  else
  {
    System.Console.WriteLine("Sorry... You have failed in the examination");
  }

  System.Console.WriteLine("Press any key to exit");
  System.Console.ReadKey();

Output 1:

Enter Marks obtained in the exam (0 to 100): 80
You have passed with first grade

Output 2:

Enter Marks obtained in the exam (0 to 100): 70
You have passed with second grade

Output 3:

Enter Marks obtained in the exam (0 to 100): 50
You have passed with third grade

Output 4:

Enter Marks obtained in the exam (0 to 100): 38
Sorry... You have failed in the examination

Let’s understand the code example step by step.

In the first line of code, we have declared integer type variable named ’marks’ to store the marks entered by the user.

In the next two lines of code, we are getting the input from the user for marks. We are showing an appropriate message to the user to enter marks between 0 and 100. We have used System.Console.ReadLine() method to read the input from the user and then assign entered value to the ’marks’ variable.

After getting marks from the user, in the next lines of code, we are using ’if… else… if’ statement to find the grade obtained by the student based on the marks and displaying an appropriate message showing the grade obtained.

Let’s assume that there is a three-grade system. First grade is for marks 75 and above. Second grade is for marks between 60 and 75 (including 60 up to 74). Third grade is for marks between 40 and 60 (including 40 up to 59). If the student got marks less than 40 then he is failed and no grade will be assigned.

To achieve this, in the first if statement we have used the condition to check whether the marks entered are more than or equal to 75. If this condition is true then we have statement within the if code block to display the appropriate message and all other else if and else conditions won’t be checked.

But if this condition returns false, that means marks are not more than or equal to 75, in ’else…if’ we have checked the condition for the second grade and if this condition returns true then in respective code block we are displaying the appropriate message.

If this condition returns false then we have written the next ’else…if’ and checked the condition of the third grade and display the appropriate message to show the user that he is passed in the third grade.

If this condition returns false, that means the student could not get any grade and his marks are less than 40. So we have the else code block that informs the user that he or she failed the exam by displaying the appropriate message.

After checking all the conditions and displaying an appropriate message, in the next line of code, we have asked the user to press any key to exit the program.

As you can see we have four outputs based on the marks entered. The first output is for the first grade when a student got more than 75 marks. The second output is for the second grade. The third output is for third grade and fourth output is for failed student.

C# Fundamentals