Home > OS >  What's the difference between these 2 simply if statements in java
What's the difference between these 2 simply if statements in java

Time:05-28

As the question asked, I was working on an assignment about creating the bounding box of shapes. I was trying to make a comparison for the maximum X coordinate but it won't work as expected in the following code.

public class Test{
    public static void main(String []args) {
        Scanner in = new Scanner(System.in);
        double x1;
        double x2;
        double x3;
        double maxX= 0;
        System.out.println("input x1");
        x1 = in.nextDouble();
        System.out.println("input x2");
        x2 = in.nextDouble();
        System.out.println("input x3");
        x3 = in.nextDouble();

            if(x1>=x2) {
                 if(x1>=x3) {
                     maxX = x1;
                 }
                }
             else if(x2>=x1) {
                 if(x2>=x3)
                     maxX = x2;
             }
             else {
                 maxX = x3;
             }

If I put x1 =2,x2=3,x3=5. The returned value would equal zero. Which is the initialized value. But the program would run correctly with the following code.

public class Test{
    public static void main(String []args) {
        Scanner in = new Scanner(System.in);
        double x1;
        double x2;
        double x3;
        double maxX= 0;
        System.out.println("input x1");
        x1 = in.nextDouble();
        System.out.println("input x2");
        x2 = in.nextDouble();
        System.out.println("input x3");
        x3 = in.nextDouble();

           
            
        
        if((x1>=x2)&&(x1>=x3)){
            maxX=x1;
                }
        else if((x2>=x1)&&(x2>=x3)){
            maxX=x2;
            }
        else
            maxX=x3;
        System.out.println(maxX);
    }
}

This time the system would print 5, but I really don't get it. For the other three coordinates, I used the first method and it worked out perfectly. I don't know why this specific maxX method would get a problem.

CodePudding user response:

In the first example your second statement does not do anything for the case in which x2 is greater than one, but lower than x3. You also have the same problem a statement before:

    if(x1>=x2) {
       if(x1>=x3) {
           maxX = x1;
       }
       else {
          //x1 >= x2 && x1 < x3 => x3 is the greatest
       }
    }
    else if(x2>=x1) {
       if(x2>=x3)
          maxX = x2;
       }
       else {
          // x2 >= x1 && x2 < x3 => x3 is the greatest
       }
    else {
       // Can never be reached, as either x1 >= x2 or x2 >= x1
       maxX = x3;
    }

I hope this answer was helpful, I never gave one before.

CodePudding user response:

In the first example you've nested the if-else statements while in the second you're using logic operators. Let's just follow the flow.

In the first example, is x1>=x2? It is not so we drop to the else. Is x2>=x1? It is, so we execute the following statement. Is x2>=x3? It is not so we're done. maxX is not assigned a value. You do not execute the final else because x2>=x1 returned true.

In the second example, is (x1>=x2)&&(x1>=x3) true or false. We know x1>=x2 is false and so the expression is false, which means to drop to the else. Is (x2>=x1)&&(x2>=x3) true or false? x2>=x1 is true but x2>=x3 is false, and so the expression is false. This means we drop to the final else and maxX=x3.

It the difference between evaluating the conditional one after the other, or at the same time.

  •  Tags:  
  • java
  • Related