Prev: nike air max 2003 ney style hot sell ....free shipping to worldwide
Next: cluttered code prevention?
From: Thomas 'PointedEars' Lahn on 19 Oct 2009 21:37 dsmithy wrote: > I came across this line of code in a book. It appears to use the > alternative if syntax, but it doesn't quite make sense to me: > > res += (i * j) % 8 ? " " : "*"; That is _not_ "alternative if syntax". See below. > where res is a string variable, and i and j are number variables (i > increments from 1 to 7 and j increments from 1 to 15). > > What's confusing me is that I thought the expression preceding the ? > had to be a conditional (i.e., true or false) statement in order for > the computer to choose between " " and "*". You were mistaken. > But (i * j) % 8 isn't a conditional. It just evaluates to a number. Exactly. > So how is the choice made between " " and "*"? ECMAScript implementations until including that of Edition 3 (so all that are relevant client-side to date) use loose typing. Operators and methods implicitly convert operands and arguments to values of defined types for internal processing. With the Conditional Operator, the /ConditionalExpression/ is evaluated and converted to Boolean. If converted to `true', the result is " ", otherwise "*": ,-[ECMAScript Language Specification, Edition 3 Final] | | 11.12 Conditional Operator (?:) | | [...] | The production | | ConditionalExpression : | LogicalORExpression ? AssignmentExpression : AssignmentExpression | | is evaluated as follows: | | 1. Evaluate LogicalORExpression. | 2. Call GetValue(Result(1)). | 3. Call ToBoolean(Result(2)). | 4. If Result(3) is false, go to step 8. | 5. Evaluate the first AssignmentExpression. | 6. Call GetValue(Result(5)). | 7. Return Result(6). | 8. Evaluate the second AssignmentExpression. | 9. Call GetValue(Result(8)). | 10. Return Result(9). To make a long story short, Number values are converted to Boolean so that (±)`0' and `NaN' are converted to `true', and all other values to `false'. This means in your case -- (i * j) % 8 ? " " : "*" -- that if the product of the values of `i' and `j' is divisible without remainder by 8 (or, IOW, the product of the values is a multiple of 8), and so the remainder is 0, the converted value is `false', and the result of the Conditional Operation is "*"; in all other cases the converted value is `true', and the result is " ". (This is somewhat counter-intuitive. Human intuition suggests that a division without remainder to be a success, that should result in `true'; however, the result of the `%' [modulo] operation is not the success status of the division without remainder, but the remainder itself.) However, if `res' is a string variable, you might want to consider using another operator than `+='. > Thank you, You're welcome, but please read the FAQ now, and ultimately RTFM. <http://jibbering.com/faq/#posting> PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: dsmithy on 19 Oct 2009 22:20 On Oct 19, 9:37 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > dsmithy wrote: > > I came across this line of code in a book. It appears to use the > > alternative if syntax, but it doesn't quite make sense to me: > > > res += (i * j) % 8 ? " " : "*"; > > That is _not_ "alternative if syntax". See below. > > > where res is a string variable, and i and j are number variables (i > > increments from 1 to 7 and j increments from 1 to 15). > > > What's confusing me is that I thought the expression preceding the ? > > had to be a conditional (i.e., true or false) statement in order for > > the computer to choose between " " and "*". > > You were mistaken. > > > But (i * j) % 8 isn't a conditional. It just evaluates to a number. > > Exactly. > > > So how is the choice made between " " and "*"? > > ECMAScript implementations until including that of Edition 3 (so all that > are relevant client-side to date) use loose typing. Operators and methods > implicitly convert operands and arguments to values of defined types for > internal processing. With the Conditional Operator, the > /ConditionalExpression/ is evaluated and converted to Boolean. If converted > to `true', the result is " ", otherwise "*": > > ,-[ECMAScript Language Specification, Edition 3 Final] > | > | 11.12 Conditional Operator (?:) > | > | [...] > | The production > | > | ConditionalExpression : > | LogicalORExpression ? AssignmentExpression : AssignmentExpression > | > | is evaluated as follows: > | > | 1. Evaluate LogicalORExpression. > | 2. Call GetValue(Result(1)). > | 3. Call ToBoolean(Result(2)). > | 4. If Result(3) is false, go to step 8. > | 5. Evaluate the first AssignmentExpression. > | 6. Call GetValue(Result(5)). > | 7. Return Result(6). > | 8. Evaluate the second AssignmentExpression. > | 9. Call GetValue(Result(8)). > | 10. Return Result(9). > > To make a long story short, Number values are converted to Boolean so that > (±)`0' and `NaN' are converted to `true', and all other values to `false'. > > This means in your case -- (i * j) % 8 ? " " : "*" -- that if the product of > the values of `i' and `j' is divisible without remainder by 8 (or, IOW, the > product of the values is a multiple of 8), and so the remainder is 0, the > converted value is `false', and the result of the Conditional Operation is > "*"; in all other cases the converted value is `true', and the result is > " ". (This is somewhat counter-intuitive. Human intuition suggests that a > division without remainder to be a success, that should result in `true'; > however, the result of the `%' [modulo] operation is not the success status > of the division without remainder, but the remainder itself.) > > However, if `res' is a string variable, you might want to consider using > another operator than `+='. > > > Thank you, > > You're welcome, but please read the FAQ now, and ultimately RTFM. > > <http://jibbering.com/faq/#posting> > > PointedEars Well you're a testy one, aren't you. I've been "RTFM" as you so crudely put it and trying to understand (in truth I feel like I've figured out quite a bit on my own without bugging the likes of you). I read about type conversions (the book I'm reading did present it early on) but I just didn't make the connection between that concept and the conditional operator -- I guess we can't all be as quick or smart as you apparently are. When you don't have the time to take a course with a teacher, it's really nice to have a resource like this group to bounce the occasional question off of and get it immediately resolved. Sorry to have wasted your time with my "easy" question. Then again, judging by the amount of spam clogging up this group, I'd say that right there's your *real* problem...
From: Stevo on 20 Oct 2009 02:08 Thomas 'PointedEars' Lahn wrote: > dsmithy wrote: > >> I came across this line of code in a book. It appears to use the >> alternative if syntax, but it doesn't quite make sense to me: >> >> res += (i * j) % 8 ? " " : "*"; > > That is _not_ "alternative if syntax". See below. > PointedEars Yes it is. There's nothing at all wrong with referring to a ternary statement as "alternative if syntax". It gets across in very simple terms what you can do with it. If a regular if looks like this: if ( x==1 ) y=2; else y=0; and as a ternary statement it looks like this: y = x==1 ? 2 : 0; That is clearly an alternative if syntax. Stop trying to confuse people.
From: Stevo on 20 Oct 2009 02:11 dsmithy wrote: > On Oct 19, 9:37 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> > wrote: >> dsmithy wrote: >>> I came across this line of code in a book. It appears to use the >>> alternative if syntax, but it doesn't quite make sense to me: >>> res += (i * j) % 8 ? " " : "*"; >> That is _not_ "alternative if syntax". See below. >> >>> where res is a string variable, and i and j are number variables (i >>> increments from 1 to 7 and j increments from 1 to 15). >>> What's confusing me is that I thought the expression preceding the ? >>> had to be a conditional (i.e., true or false) statement in order for >>> the computer to choose between " " and "*". >> You were mistaken. >> >>> But (i * j) % 8 isn't a conditional. It just evaluates to a number. >> Exactly. >> >>> So how is the choice made between " " and "*"? >> ECMAScript implementations until including that of Edition 3 (so all that >> are relevant client-side to date) use loose typing. Operators and methods >> implicitly convert operands and arguments to values of defined types for >> internal processing. With the Conditional Operator, the >> /ConditionalExpression/ is evaluated and converted to Boolean. If converted >> to `true', the result is " ", otherwise "*": >> >> ,-[ECMAScript Language Specification, Edition 3 Final] >> >> PointedEars > > Well you're a testy one, aren't you. I've been "RTFM" as you so > crudely put it and trying to understand (in truth I feel like I've > figured out quite a bit on my own without bugging the likes of you). I > read about type conversions (the book I'm reading did present it early > on) but I just didn't make the connection between that concept and the > conditional operator -- I guess we can't all be as quick or smart as > you apparently are. When you don't have the time to take a course with > a teacher, it's really nice to have a resource like this group to > bounce the occasional question off of and get it immediately resolved. > Sorry to have wasted your time with my "easy" question. Then again, > judging by the amount of spam clogging up this group, I'd say that > right there's your *real* problem... It doesn't take long for people to figure out how unfriendly PointedEars is. It's weird isn't it, if he only knew how to communicate in a friendly manner he might well become a hero around here. Instead though, despite his obvious knowledge, he makes enemies here every day.
From: The Natural Philosopher on 20 Oct 2009 02:30
Stevo wrote: > dsmithy wrote: >> On Oct 19, 9:37 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> >> wrote: >>> dsmithy wrote: >>>> I came across this line of code in a book. It appears to use the >>>> alternative if syntax, but it doesn't quite make sense to me: >>>> res += (i * j) % 8 ? " " : "*"; >>> That is _not_ "alternative if syntax". See below. >>> >>>> where res is a string variable, and i and j are number variables (i >>>> increments from 1 to 7 and j increments from 1 to 15). >>>> What's confusing me is that I thought the expression preceding the ? >>>> had to be a conditional (i.e., true or false) statement in order for >>>> the computer to choose between " " and "*". >>> You were mistaken. >>> >>>> But (i * j) % 8 isn't a conditional. It just evaluates to a number. >>> Exactly. >>> >>>> So how is the choice made between " " and "*"? >>> ECMAScript implementations until including that of Edition 3 (so all >>> that >>> are relevant client-side to date) use loose typing. Operators and >>> methods >>> implicitly convert operands and arguments to values of defined types for >>> internal processing. With the Conditional Operator, the >>> /ConditionalExpression/ is evaluated and converted to Boolean. If >>> converted >>> to `true', the result is " ", otherwise "*": >>> >>> ,-[ECMAScript Language Specification, Edition 3 Final] >>> >>> PointedEars >> >> Well you're a testy one, aren't you. I've been "RTFM" as you so >> crudely put it and trying to understand (in truth I feel like I've >> figured out quite a bit on my own without bugging the likes of you). I >> read about type conversions (the book I'm reading did present it early >> on) but I just didn't make the connection between that concept and the >> conditional operator -- I guess we can't all be as quick or smart as >> you apparently are. When you don't have the time to take a course with >> a teacher, it's really nice to have a resource like this group to >> bounce the occasional question off of and get it immediately resolved. >> Sorry to have wasted your time with my "easy" question. Then again, >> judging by the amount of spam clogging up this group, I'd say that >> right there's your *real* problem... > > It doesn't take long for people to figure out how unfriendly PointedEars > is. It's weird isn't it, if he only knew how to communicate in a > friendly manner he might well become a hero around here. Instead though, > despite his obvious knowledge, he makes enemies here every day. I always listen to what Pointed Ears has to say. Its seldom wrong. Good information usually has a price. If its being belittled and humbled, sometimes its still worth it. Personally I think the ternary operator is reasonably expressed as an alternative if statement. In terms of logical program flow anyway. If there are detailed differences, I am interested to hear them. Pointed Ears differs from most other pejorative posters, in that his interest is in pedantic correctness. Not winning arguments. He seems to carry a sniper rifle, and aim to hit the bullseye, not a 12 gauge and blast anyone who disagrees with him ,regardless. If you don't like his style, use the killfile. |