-1

I am getting different results: -1 if I use parentheses and 1 if I don't use parentheses.

For test case "30/10/2019" I am getting -1 if I use parentheses and 1 if I don't use parentheses in the following line:

else if((mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12) && mm<13 && mm>0 && dd>0 && dd<32) return 1;

What is the difference between above line and below line?

else if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12 && mm<13 && mm>0 && dd>0 && dd<32) return 1;
public class Utility {
   public static int checkDate(String date){
     String []st;
   if((date.charAt(2)=='.' && date.charAt(5)=='.')||(date.charAt(2)=='/' && date.charAt(5)=='/')||(date.charAt(2)=='-' && date.charAt(5)=='-'))
   {
       String token = Character.toString(date.charAt(2));
       if(date.charAt(2) == '.') st = date.split("\\.");
       else st = date.split(token);

       int dd = Integer.parseInt(st[0]);
       int mm = Integer.parseInt(st[1]);
       int yy = Integer.parseInt(st[2]);

        if(mm == 2 && dd>0 && dd<30 && mm<13 && mm>0) return 1;

        else if((mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12) && mm<13 && mm>0 && dd>0 && dd<32) return 1;

        else if((mm==4||mm==6||mm==9||mm==11) && dd>0 && dd<31 && mm<13 && mm>0) return 1;

        else return -1;
   }
   else return -1;
 }
}

import java.util.Scanner;


public class DateValidation {

    public static void main(String[] args) {

       // STUDENT CODE BEGINS HERE
        Scanner sc = new Scanner(System.in);
    String dt=sc.next();
    Utility ut = new Utility();
    int flag = ut.checkDate(dt);
    if(flag==1)
        System.out.println("Valid");
    else
        System.out.println("Invalid");
       // STUDENT CODE ENDs HERE

    }
}
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
  • 2
    Possible duplicate of [In Java, what are the boolean "order of operations"?](https://stackoverflow.com/questions/2263660/in-java-what-are-the-boolean-order-of-operations) – Turamarth Oct 21 '19 at 14:23
  • 1
    same as with any operator `(1 + 2) * 3 == 9` and `1 + 2 * 3 == 7` – user85421 Oct 21 '19 at 14:37

3 Answers3

1

It will execute the following (in highest order of precedence):

  • Parentheses ()
  • Not !
  • And &&
  • Or ||
else if((mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12) && mm<13 && mm>0 && dd>0 && dd<32) 
    return 1;

It will evaluate (mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12) first, then && mm<13 && mm>0 && dd>0 && dd<32

else if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12 && mm<13 && mm>0 && dd>0 && dd<32) 
    return 1;

As for the other, it will evaluate mm<13 && mm>0 && dd>0 && dd<32 first, then mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12 &&

Prince Vegeta
  • 719
  • 6
  • 21
0

Parentheses decide the order these booleans are resolved. And binds stronger than Or. In your case you ask for:

mm==12 && mm<13

So left comparison and right comparison must resolve to true. They don't, as you are in month 10. With parenthesis you decide to check if your month value is valid at all, this resolves to true, and then the && check resolves to true as well.

Koenigsberg
  • 1,726
  • 1
  • 10
  • 22
0

Consider this example.


      int a = 5;

      System.out.println((a == 5 || a == 10) && a == 8 || a == 9); // false
      // evaluates to    true && false || false which is false

      System.out.println(a == 5 || a == 10 && a == 8 || a == 9);  // true
      // evaluate to true || false && false && false
      // the first true, wins because it is followed by an || so the whole
      // expression evaluates to true.

With a && b both both a and b must be true for the statement to be true With a || b only one of a and b must be true.

This also goes for grouped expressions.

If one expression is true and the other is false, the complete expression is false for && and true for ||. These operators work from left to right so you need to group appropriately, just like arithmetic expressions.

The best advice is always use parentheses to ensure proper evaluation of expressions.

WJS
  • 36,363
  • 4
  • 24
  • 39