Tracing a program with if-else if-else statements

An exercise, its solution, and tracing: if-else if-else in Java: Video Lecture 8

In the previous video-lecture, we described how if-else if-else statements work. We provided an exercise with the video-lecture. In the video with this article, we provide a solution to the exercise.

Tracing a program with if-else if-else statements

In the following video, we introduce a technique called tracing that we did not cover before. Tracing is way to analyze a program. Sometimes, the variable space is so large that correctness of the code needs to be checked conceptually. Tracing by hand is a great way to analyze code for correctness.

The video: An exercise and the solution

The video does not only show how I wrote the code to solve a programming problem, but it also shows how I analyzed the problem before starting to code, and how I analyzed the code after writing the code.

Solution using if-else if-else statements in Java.

The if-else if-else exercise (from the previous video post)

Write a program that asks the user for a GPA on a scale of 0.0 to 4.0. If the user enters a number larger than 4.0 or less than 0.0, the program must output “Invalid GPA.” If the user enters a valid number, the program should display the corresponding letter grade based on the table below.

GPA rangeLetter Grade
4.0A+
3.75 to less than 4.0A
3.5 to less than 3.75A-
3.25 to less than 3.5B+
3.0 to less than 3.25B
2.75 to less than 3.0B-
2.0 to less than 2.75C
0 to less than 2.0F

The solution program using if-else if-else statements

We always say that it is better to type your own code because the practice of writing the code connects you with the solution. We are still providing the code below as a reference. If you decide to use the code, please save the code in a file named Exercise.java because the class name is Exercise.

import java.util.Scanner;
class Exercise{
    public static void main(String[] args){
      double gpa;
      Scanner scan = new Scanner (System.in);
      System.out.println("Enter the GPA, please: ");
      gpa= scan.nextDouble();
      
      if (gpa>4.0){
        System.out.println("Invalid GPA.");
      }else if (gpa==4.0){
        System.out.println("A+");
      }else if (gpa>=3.75){
        System.out.println("A");
      }else if (gpa>=3.5){
        System.out.println("A-");
      }else if (gpa>=3.25){
        System.out.println("B+");
      }else if (gpa>=3.0){
        System.out.println("B");
      }else if (gpa>=2.75){
        System.out.println("B-");
      }else if (gpa>=2.0){
        System.out.println("C");
      }else if (gpa>=0.0){
        System.out.println("F");
      }else{
        System.out.println("Invalid GPA.");
      }      
    }
}

Transcription of the Audio of the Video

Hi,

This is Dr. Shahriar Hossain. In the last video lecture, we discussed “if-else if-else” statements. On our website, we provided an exercise. Today, we are going to solve that exercise. If you have already solved the exercise that we provided with the last video lecture, I suggest, please still watch this video till end because it contains some analysis using tracing. Tracing is a technique that helps us analyze our code without running it enormous amount of time with different inputs.

Oh, one thing before we start working on the exercise, please subscribe to our website Computing4All.com and to this YouTube channel to receive notifications about our articles and video lectures.

I will not keep you waiting anymore. Let us work on the exercise.

Problem description

You can see the description of the exercise on the screen now. The exercise states the following: Write a program that asks the user for a GPA on a scale of 0.0 to 4.0. If the user enters a number larger than 4.0 or less than 0.0, the program must output “Invalid GPA.” If the user enters a valid number, the program should display the corresponding letter grade based on the table below.

Then the table provides bunch of ranges for different grades.

We will analyze the problem a bit now.

Problem analysis for if-else if-else statements

At first, I am outlining the range of GPA. Valid GPA may vary between 0 to 4.0. I am drawing the ranges that are mentioned in the description of the exercise. As described in the problem, any GPA greater than 4.0 should be “Invalid.” Also, any GPA smaller than 0 is “Invalid.”

Variable space analysis for if-else if-else statements in a Java program

Now recall, from the table that the letter grade A+ is assigned to GPA 4.0 only.

A GPA in the range from 3.75 to anything less than 4.0 should have a letter grade “A”.

From 3.5 to anything less that 3.75 should be designated as A- (minus).

GPA B+ is in the range starting from 3.25 and should be less than 3.5.

B starts from 3.0. It is less than 3.25.

The range of B- starts from 2.75. B- is less than 3.0.

The next letter grade is C. It starts from 2.0 and is less than 2.75.

Valid grades less than 2.0 result in an F.  

The idea of the solution

The idea of the solution is that we will use if-else if-else statements to display the letter grade corresponding to a given gpa.

Starting of the code

Let us write the code. I have already written the class name and the header for the main method. The file name is Exercise.java.

I will write the code to collect the GPA from the user. I will keep the GPA in a variable named gpa, which is of “double” data type.

Based on what is inside the variable, gpa, I want to display the corresponding letter grade. In our figure, we started to draw the ranges from right side. That is, from larger gpa to smaller gpa. I will write the code in the same order. That is, I will first write an “if” statement to cover the gpa greater than 4.0. Notice that all gpa’s greater than 4.0 should be marked as “Invalid GPA” because the valid gpa range starts at 0.0 and ends at 4.0.

gpa>=4.0

All I have to write is, if gpa is greater than 4.0, then print “Invalid GPA.”

GPA: A+

Now, the next item in the right side of the figure is, 4.0. At 4.0, the letter grade A+ should be printed. The idea is, I will use an “else if” statement to cover 4.0. I am writing else if, gpa is equal to 4.0, then print “A+”. Notice that the equality comparison has to be with two “equals” symbols. We discussed in an earlier video that the equality comparison is always with two equals symbol, not one. A single equals symbol is used for the assignment operations.

Anyway, if we run the program right now, then for any gpa greater than 4.0, the program will print “Invalid GPA”. For gpa equal to 4.0, the program will print “A+”. For any other gpa, the program will print nothing.

We are not going to run the program now. Rather, we will compete the code and then run the program.

GPA: A

The next grade letter is A. GPA A may start at 3.75 and must be less than 4.0. Therefore, I add another else if statement, in which I write the condition gpa>=3.75. When the condition is satisfied, we print the letter grade “A”.

At this point, if we run the program with a gpa, say 3.8, the first condition is not satisfied; the second condition gpa==4.0 is not satisfied either. The third condition gpa is greater than 3.75 is satisfied. Therefore, the program will print the letter grade “A”.

If the first condition is satisfied, which is an invalid situation in this program, the program prints “Invalid GPA” and does not check any other condition in the if-else if sequence. A condition in the if-else if -else sequence is checked only if all the preceding if or else if conditions resulted in “false”. Again, once an “if” or “else if” condition is satisfied, no other condition in the sequence will be checked.

GPA: A-

Let us get back to the code. The next letter grade is A-, which starts from gpa 3.5 and is less than 3.75. Therefore, the condition in the “else if” statement for A- is “gpa>=3.5.”

GPA: B+, B, B-

Let me quickly write the code up to B-, using concepts similar to what we have discussed so far.

GPA: C

Now, notice that the range for C is a bit large. It starts from 2.0 and is less than 2.75. In our code, we provide that condition in the “else if” statement for the letter grade C.

GPA: F

The next letter grade is “F”, which starts at gpa 0.0 and is less than 2.0.

gpa<=0.0

So far, we have all the letter grades in the code. We also have the invalid grade that is greater than 4.0. The only item that is left out is any invalid gpa that is smaller than 0.0. Notice that we do not need another “else if” for that because it is the only situation that is left out. Therefore, we can use “else” without any condition for any gpa lesser than 0.0. We write “Invalid GPA” on the terminal if the user enters any gpa that is negative.

Running the program

The code is complete. Let us save the file and compile it. I will run the program several times with different gpa values. Notice that all the grade letters are correctly printed.

Tracing

Let us manually trace the code a bit for clarity. When you run the program, the Java virtual machine will attempt to execute each line in the main method in a sequence as they appear.

A variable named GPA is created. The Scanner object named “scan” is created. Then, on the terminal “Enter the GPA, please” — this sentence is printed. Then the user enters a GPA and hits the enter button on the keyboard. The computer receives the value of the GPA in the variable named “gpa”. Now the “if-else if” statements of the code will execute, as required. Consider that the user entered a GPA of 3.3.

Tracing with GPA 3.3

Now the computer will check if the variable “gpa” is holding anything greater than 4.0. Since 3.3 is not greater than 4.0, the statement “if gpa>4.0” results in false. Therefore, the corresponding System.out.println “Invalid GPA” will not be executed. Since, “if gpa>4.0” is false, the computer now will check the next “else if” statement, which is gpa==4.0. GPA 3.3 is not equal to 4.0, therefore, the statement “else if (gpa==4.0)” is false. Since, it is false the execution will not go inside its scope. Therefore, “A+” will NOT be printed.

The next item the computer will check if gpa>=3.75. GPA 3.3 is not greater than or equal t 3.75. Therefore, this condition results in false.

The next check is if gpa is greater than or equal to 3.5, which also results in false. Therefore, the computer will check the next “else if” condition, which is if gpa is greater than or equal to 3.25. Now, GPA 3.3 is greater than 3.25. Hence, the condition “gpa>=3.25” results in true. Since it results in true, the execution will go inside the curly brackets, where it is written System.out.println “B+”. Therefore, B+ will be printed. Once one condition is satisfied, that is, once a condition results in true, after executing the corresponding scope, the computer does not check for any other condition.

The execution will jump to the end of this entire “if-else if-else” sequence. That would be the end of the program. That is, for gpa 3.3, the program has printed “B+”.

Tracing with GPA -5.0

Let us quickly trace, another execution of the program, where the user enters -5.0 (negative 5 point 0) as the gpa. Notice that negative 5 point 0 is an invalid GPA because it is less than zero. Let us check how the execution of the program will work now.

We are tracing for the variable gpa, when it is negative 5 point 0. In the first “if” statement, gpa>4.0, the condition is false because -5.0 is not greater than 4.0. Therefore, the corresponding System.out.println is not executed.

For the next “else if” condition, gpa==4.0, the resultant Boolean value is “false” too.

Notice that, for gpa negative 5 point zero, none of these conditions will result in “true”. Therefore, the execution will end up going to the “else” statement. Once, the execution goes to the “else” statement, the computer executes every instruction under the “else” scope. In our code, the only statement written here is System.out.println “Invalid GPA”. Therefore, the computer will execute it and “Invalid GPA” will be printed on the terminal.

Notice again that when, all the “if” and “else if” conditions result in false, only then the last “else” is executed.

Tracing with GPA 3.6

Let us quickly do another tracing for GPA 3.6. gpa>4.0 is false because 3.6>4.0 is false. The next, condition gpa==4.0 is false too. Even the next one, gpa>=3.75 is false. The next “else if” condition gpa>=3.5 is true because 3.6 is greater than 3.5. Therefore, the execution will go inside the scope of “gpa>=3.5”. The computer will execute System.out.println “A-”. No other condition will be checked because already a condition in this sequence of “if – else if” statements is true. The execution will jump to the end of this “if-else if-else” sequence.

Conclusion

I hope the “if-else if- else” statements are clearer with the video lecture today. Please do not hesitate to ask any question you may have via the comments section below. We will be happy to be with you in your journey to learn programming. If you haven’t already done so, please subscribe to our website Computing4All.com and our YouTube channel where you are watching this video.

4 replies
    • M. Shahriar Hossain
      M. Shahriar Hossain says:

      Hi Aditya,
      Are you receiving an error, when you run or compile? Please use javac to compile and java to run the code. The code should not have any error during compilation and when you execute it. My Hindi is not good. Sorry about it.

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *