Precedence.java


#1

how do we complete the line for the code below
public class Precedence {
public static void main(String[] args) {

	**boolean riddle = ( 1< 8 ) (5 > 2 ) (3 < 5);**
	System.out.println(riddle);

}

}

this needs to print false as an outcome. how do you just use a boolean operator once each.
i am getting an error for the above according to precedence and i am using the operator more than once


#2

@csscoder10781,
You will have to use so-called conditional-operators
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
http://stackoverflow.com/questions/14058617/java-operators-precedence

( 1< 8 ) (5 > 2 ) (3 < 5) 
        ^        ^
        |        |
 conditional-operator  either  &&   or  ||

#3

Try this code

Public class precedence {
Public static void main (string[ ] args) {

      boolean riddle = !(1 < 8) && (5 > 2 || 3 < 5));
      System.out.println(riddle);

  }
}

#4

public class Precedence {
public static void main(String[] args) {

	boolean riddle = !( 1 < 8 && (5 > 2 || 3 < 5));
	System.out.println(riddle);

}

}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It prints false. That makes no sense to me. I think whats confusing me is the brackets. I understand the precedence.

I see this: FALSE && TRUE || TRUE

FALSE && TRUE = FALSE

So FALSE || TRUE = TRUE!

Whats the deal? Why is it printing false?


#5

Hey coldcasechristian if you leave the ! point in front of the entire thing then it will set the entire thing to false because even if everything in the parenthesis is true it will set it false


#6

@coldcasechristian: this is exactly how I saw that, too.
@piercebauer: thank you for the explanation. However, I find it a little unfair (no intended whining here) to apply all the brackets here. This exercise was supposed to teach precedence, and it makes the beginner evaluate the ! operator first, as it is supposed to be evaluated first. So working with the brackets suggests I evaluate the content of the brackets first and then translate to its opposite for the ! operator. Right?

Yes, I am a complete noob. But this is maybe hat this course is for, isn’t it?