a<b<c in if

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear all,

if(a<b<=c) {statement}   is it OK?

it means condition statement a<b<c in if is OK or should be if(a<b && b<c) ???

Last Edited: Sat. Mar 14, 2020 - 02:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hasanzadeh99 wrote:
if(a<b<=c) {statement}   is it OK?

It is syntactically valid - but probably not what you want.

 

hasanzadeh99 wrote:
or should be if(a<b && b<c) ???

That's probably what you want.

 

You could always try it on a PC ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
hasanzadeh99 wrote:

if(a<b<=c) {statement}   is it OK?

 

It is syntactically valid - but probably not what you want.

Do not bother.  It means either (a<b)<=c or a<(b<=c) .

It depends on the grouping (?) order of < and <=.

In the unlikely event you want either one of those,

insert the parentheses and add a comment so that others will not have to guess whether it is correct.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

< is a boolean. The result is either 0 or 1. So a < b is evaluated and is either 0 or 1, then that result is used to evaluate result < c. If c is 2 or more, this will always be true. If c is 0 or less, this will always be false. Regardless of a or b in either case. As awneil says, not what you want.

 

EDIT:  Ah, I was working from the thread title "a<b<c" rather than the code in the OP "a<b<=c".  The outcome is similar, except w.r.t. "If c is 2 or more" which becomes "If c is 1 or more".

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Fri. Mar 13, 2020 - 03:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In Python, a< b<=c would do what OP seems to want.

It has the effect of turning n comparisons into an n+1-ary operation.

C does not do that.

Comparisons are purely binary operations that result in (int)0 or (int)1 .

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is wrong with 

 

if ( (a < b) && (b <= c) ) {... }

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

What is wrong with 

Nothing.  The OP asked about it:
hasanzadeh99 wrote:
or should be if(a<b && b<c) ???
... and Andy confirmed.

 

Although you've added parenthesis for clarity, they aren't necessary because C operator precedence favours < and friends over &&.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Fri. Mar 13, 2020 - 04:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
What is wrong with 

 

if ( (a < b) && (b <= c) ) {... }

Probably nothing,

though if b is an expression, even parenthesized, it might not work.

In languages for which a< b<=c does what OP wants,

it is more clear.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I opt for absolute certainty in execution and for clarity in mental interpretation. Why ponder over the OP's expression when you can be certain without even having to look it up? I don't have to remember the precedence rules with my expression. Win all around, as far as I can see.

 

Jin

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do the same and then, when I go back to it later, I never have to remember/wonder what the intention was.

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MHO, real expertise comes not from knowing the precedence table, but from knowing you don't want to need to know the precedence table.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

the_real_seebs wrote:

MHO, real expertise comes not from knowing the precedence table, but from knowing you don't want to need to know the precedence table.

I see you've never had to maintain anyone else's code ;-)

 

I don't want to.  I have to.  When I clean up existing code, I do the same as everyone else here.  I add parenthesis for clarity.

 

But you have to start from a place of knowing the rules of the language.  Or at least knowing that there >>are<< rules, and how to find them.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

C has what, seventeen levels of precedence? That's about fourteen too many, in my not so humble opinion.

 

I don't even rely on precedence levels for adds, takeaways, timeses, and guzintas... if you want future reviewers to understand your intentions; if you want to understand your own code by next thursday, use brackets.

 

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

barnacle wrote:

C has what, seventeen levels of precedence? That's about fourteen too many, in my not so humble opinion.

 

When it comes to C, one basically just needs:

 

1. Postfix operators have higher precedence than prefix operators

2. Unary operators have higher precedence than binary operators

3. The usual mathematical rules apply, e.g. multiplication has higher precedence than addition

4. Arithmetic has higher precedence than comparisons and assignment (but bitwise have lower precedence than comparisons)

5. Comparisons have higher precedence than logic operators

6. With almost everything else: just use parentheses

Dessine-moi un mouton

Last Edited: Wed. Mar 18, 2020 - 02:54 PM