Ternary conditional operator vs. If Statement
7 posts • Page 1 of 1
Ternary conditional operator vs. If Statement
http://blog.joshbuhler.com/?year=2005&m ... ator&page=
Well, I finally realized what's going on here! What's causing so many people so much grief.
It's the use of the ternary conditional operator expression instead of an if statement!
They're very similar in many ways, and can many times be substituted for one another. However, they are not just an alternate if statement.
Basically, the ternary conditional operator works like this in most programming languages:
Whereas an if structure to do the same thing would look like this:
Writing your if statement with as a ternary conditional saves a little bit of typing in this case.
Here's how the ternary conditional operator works:
It evaluates the condition. If the condition returns TRUE, it evaluates the isTrueAction. If the condition returns FALSE, it evaluates the isFalseAction.
The ternary conditional operator is great for writing things like:
What it is not great for is writing things like:
Because to write that with the ternary conditional operator would require two functions, like so:
But, that example above is not necessary in Algodoo. The developers have allowed us to insert line breaks in our ternary conditionals. This removes the need to have multiple functions.
This, however, doesn't solve the even larger problem of the ridiculous nesting that has to occur for multiple elseif statements.
For example:
Is almost impossible to correctly nest brackets for unless you're writing it in a programming text editor that highlights the brackets.
And here is the same code as an if/esleif statement:
As an if statement, the code is much easier to write, nest and understand. It is also much easier to skim, which is indispensable for large amounts of code.
So, overall, though the ternary conditional operator has its uses, and in many cases will serve the purpose, it is not always the right thing to use. Therefore, I propose and advocate the inclusion of REAL if statements in Algodoo.
Well, I finally realized what's going on here! What's causing so many people so much grief.
It's the use of the ternary conditional operator expression instead of an if statement!
They're very similar in many ways, and can many times be substituted for one another. However, they are not just an alternate if statement.
Basically, the ternary conditional operator works like this in most programming languages:
- Code: Select all
(condition) ? isTrueAction : isFalseAction;
Whereas an if structure to do the same thing would look like this:
- Code: Select all
if (condition) {
isTrueAction;
} else {
isFalseAction;
}
Writing your if statement with as a ternary conditional saves a little bit of typing in this case.
Here's how the ternary conditional operator works:
It evaluates the condition. If the condition returns TRUE, it evaluates the isTrueAction. If the condition returns FALSE, it evaluates the isFalseAction.
The ternary conditional operator is great for writing things like:
- Code: Select all
(x) => {x < 1} ? {"X is less than 1} : {"X is not less than 1"};
What it is not great for is writing things like:
- Code: Select all
if (foo == "monkeys") {
"Foo equals monkeys";
} esle {
"Foo does not equal monkeys";
"Setting foo to monkeys...";
foo = "monkeys";
"Foo equals monkeys";
}
Because to write that with the ternary conditional operator would require two functions, like so:
- Code: Select all
(foo) => {foo == "monkeys"} ? {Scene.My.lessthan(foo,"monkeys")} : {Scene.my.notlessthan(foo,"monkeys")};
Scene.My.lessthan = (what,less)=>{print (what + " equals " + less)};
Scene.My.notlessthan = (what,notless)=>{print (what + " does not equal " + notless);print ("Setting " + what + " to " + notless + "...");what = notless;print (what + " equals " + notless)};
But, that example above is not necessary in Algodoo. The developers have allowed us to insert line breaks in our ternary conditionals. This removes the need to have multiple functions.
This, however, doesn't solve the even larger problem of the ridiculous nesting that has to occur for multiple elseif statements.
For example:
- Code: Select all
Scene.my.checkWavelength = (Scene.my.wavelength >= 380.0) && (Scene.my.wavelength < 440.0) ? {
Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
Scene.my.green = 0.0;
Scene.my.blue = 1.0;
} : {(Scene.my.wavelength >= 440.0) && (Scene.my.wavelength < 490.0) ? {
Scene.my.red = 0.0;
Scene.my.green = (Scene.my.wavelength - 440.0)/(490.0-440.0);
Scene.my.blue = 1.0;
} : {(Scene.my.wavelength >= 490.0) && (Scene.my.wavelength < 510.0) ? {
Scene.my.red = 0.0;
Scene.my.green = 0.0;
Scene.my.blue = (Scene.my.wavelength - 510.0)/(510.0-490.0);
} : {(Scene.my.wavelength >= 510.0) && (Scene.my.wavelength < 580.0) ? {
Scene.my.red = (Scene.my.wavelength - 510.0)/(580.0-510.0);
Scene.my.green = 1.0;
Scene.my.blue = 0.0;
} : {(Scene.my.wavelength >= 580.0) && (Scene.my.wavelength < 645.0) ? {
Scene.my.red = 1.0;
Scene.my.green = -(Scene.my.wavelength - 645.0)/(645.0-580.0);
Scene.my.blue = 0.0;
} : {(Scene.my.wavelength >= 645.0) && (Scene.my.wavelength <= 780.0) ? {
Scene.my.red = 1.0;
Scene.my.green = 0.0;
Scene.my.blue = 0.0;
} : {
Scene.my.red = 0.0;
Scene.my.green = 0.0;
Scene.my.blue = 0.0;
}
}
}
}
}
}
Is almost impossible to correctly nest brackets for unless you're writing it in a programming text editor that highlights the brackets.
And here is the same code as an if/esleif statement:
- Code: Select all
if (Wavelength >= 380.0, Wavelength < 440.0) {
Red = -(Wavelength - 440.) / (440. - 380.);
Green = 0.0;
Blue = 1.0;
} elseif (Wavelength >= 440.0) and (Wavelength < 490.0) {
Red = 0.0;
Green = (Wavelength - 440.) / (490. - 440.);
Blue = 1.0;
} elseif (Wavelength >= 490.0) and (Wavelength < 510.0) {
Red = 0.0;
Green = 1.0;
Blue = -(Wavelength - 510.) / (510. - 490.);
} elseif (Wavelength >= 510.0) and (Wavelength < 580.0) {
Red = (Wavelength - 510.) / (580. - 510.);
Green = 1.0;
Blue = 0.0;
} elseif (Wavelength >= 580.0) and (Wavelength < 645.0) {
Red = 1.0;
Green = -(Wavelength - 645.) / (645. - 580.);
Blue = 0.0;
} elseif (Wavelength >= 645.0) and (Wavelength <= 780.0) {
Red = 1.0;
Green = 0.0;
Blue = 0.0;
} else {
Red = 0.0;
Green = 0.0;
Blue = 0.0;
}
As an if statement, the code is much easier to write, nest and understand. It is also much easier to skim, which is indispensable for large amounts of code.
So, overall, though the ternary conditional operator has its uses, and in many cases will serve the purpose, it is not always the right thing to use. Therefore, I propose and advocate the inclusion of REAL if statements in Algodoo.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial.
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial.
a Mammoth wrote:be boring and interesting.
I didn't know you could go up 3.1.3! Thanks Mystery person!Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
-
Dakta - Posts: 417
- Joined: Sat Sep 12, 2009 4:36 pm
Re: Ternary conditional operator vs. If Statement
Dakta wrote:
- Code: Select all
(foo) => {foo == "monkeys"} ? {Scene.My.lessthan(foo,"monkeys")} : {Scene.my.notlessthan(foo,"monkeys")};
Scene.My.lessthan = (what,less)=>{print (what + " equals " + less)};
Scene.My.notlessthan = (what,notless)=>{print (what + " does not equal " + notless);print ("Setting " + what + " to " + notless + "...");what = notless;print (what + " equals " + notless)};
But, that example above is not necessary in Algodoo. The developers have allowed us to insert line breaks in our ternary conditionals. This removes the need to have multiple functions.
For this you never needed multiple functions even without line breaks. The new line break feature is just for better looks. It doesn't change anything in the method of thyme scripting.
But ElseIf would be indeed a cool feature.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Ternary conditional operator vs. If Statement
i would LOVE a switch case command added to thyme even if its faked by the compiler for the cosmetic effect, thyme is dirrty!
- Mr_Stabby
- Posts: 155
- Joined: Wed Dec 16, 2009 12:16 am
Re: Ternary conditional operator vs. If Statement
I agree, I'd love to see real switch/case and if/elseif/else structures.
- daniels220
- Posts: 95
- Joined: Mon Aug 31, 2009 11:30 pm
Re: Ternary conditional operator vs. If Statement
/agree
And for/while/foreach loops
And for/while/foreach loops
Link: "Surely somebody hates Walter Cronkite.."
Sonic: "Probably.. But somebody hates everyone."
Sonic: "Probably.. But somebody hates everyone."
-
link0007 - Posts: 408
- Joined: Thu Jun 11, 2009 2:45 pm
Re: Ternary conditional operator vs. If Statement
Isn't there a for loop?
While would be kinda nice, and foreach could be awesome—it would make arbitrary teleportation easier, if the other problems can be solved.
- Code: Select all
for(n,(i)=>{/*Loop Body*/})
While would be kinda nice, and foreach could be awesome—it would make arbitrary teleportation easier, if the other problems can be solved.
- daniels220
- Posts: 95
- Joined: Mon Aug 31, 2009 11:30 pm
Re: Ternary conditional operator vs. If Statement
Yes.
So, right now we're going for functional elseif/else statements (because this nested conditional business is ugly and a pain in the a$$), which might require actual if statements. We're also going for for/while/foreach commands. Those last three are a must to avoid ridiculous functions to run through arrays and apply functions to items.
What do you guys think about being able to create custom Widgets with thyme? Like being able to create buttons and sliders and text fields and apply values to them that the user can access at runtime? If you could do this, it would be really awesome!
It is also another step along the lines of creating Algodoo executable files (basically a stripped down version of Algodoo with no tools that can only run a single scene distributed with it, so that it could be embedded in web pages and distributed without the user having to have a license to view your scene), which would be REALLY cool.
I think I'll pop the elseif, for, while, and foreach suggestions into the "Suggestions/Feedback" forum and see how that goes.
So, right now we're going for functional elseif/else statements (because this nested conditional business is ugly and a pain in the a$$), which might require actual if statements. We're also going for for/while/foreach commands. Those last three are a must to avoid ridiculous functions to run through arrays and apply functions to items.
What do you guys think about being able to create custom Widgets with thyme? Like being able to create buttons and sliders and text fields and apply values to them that the user can access at runtime? If you could do this, it would be really awesome!
It is also another step along the lines of creating Algodoo executable files (basically a stripped down version of Algodoo with no tools that can only run a single scene distributed with it, so that it could be embedded in web pages and distributed without the user having to have a license to view your scene), which would be REALLY cool.
I think I'll pop the elseif, for, while, and foreach suggestions into the "Suggestions/Feedback" forum and see how that goes.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial.
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial.
a Mammoth wrote:be boring and interesting.
I didn't know you could go up 3.1.3! Thanks Mystery person!Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
-
Dakta - Posts: 417
- Joined: Sat Sep 12, 2009 4:36 pm
7 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 4 guests