Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ EL Syntax on pages 393-94

 
Ken Duncan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can figure out that EL allows for the equivalent of if statements without the word if. I also know that, at least in Java, an if has to resolve to a boolean value. Yet some of these statements have me confused. For example, the fourth one down

would be converted to

I don't understand how to figure out what the result of this will be. How do I know it should result in true? In Java this is

Would that even compile? It doesn't make much sense to me. How do you know it will return true? Thanks.

Ken
[ January 22, 2008: Message edited by: Ken Duncan ]
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure it compiles.

EL operator order of precedence

true || false && true

So, the && operator evaluates first resulting in:

true || false

Which then evaluates to

true
 
Ken Duncan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Peabody:
Sure it compiles.

EL operator order of precedence

true || false && true

So, the && operator evaluates first resulting in:

true || false

Which then evaluates to

true


Would you please explain why it evaluates to true? Is this solely because the interpreter says "the first thing I come to is "true" and since "true" is a true condition, the result is true? What is the effect of the "&& true" in this statement? You've shown that it basically disappears from the statement once it is evaluated. If the "&& true" disappears, if the original statement had evaluated to "false || true," would this statement have returned false because false came first? Thanks.

Ken
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you please explain why it evaluates to true? Is this solely because the interpreter says "the first thing I come to is "true" and since "true" is a true condition, the result is true?
Not at all. The interpreter says "What is my most important operator?" If you reference the chart in the link I posted, you'll see that && precedes ||. The && operator is evaluated first. After it resolves, the resulting || operator is evaluated.

What is the effect of the "&& true" in this statement? You've shown that it basically disappears from the statement once it is evaluated.
&& true doesn't disappear. The false && true simply evaluates to false - a brand new false.

If the "&& true" disappears, if the original statement had evaluated to "false || true," would this statement have returned false because false came first? Thanks.
Again, it doesn't disappear. However if the original statement was nothing more than false && true then it does evaluate to false, but not because false comes first. true && false would also evaluate to false. The && operator only evaluates to true if both sides are true.

This is not unique to EL. It is a fairly basic programming concept, understanding how the logical operators work as well as the order of precedence.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic