Computer Science Canada Can you have multiple statements executed in 'shortened' conditionals? |
Author: | richcash [ Fri Apr 06, 2007 2:25 pm ] | ||||
Post subject: | Can you have multiple statements executed in 'shortened' conditionals? | ||||
I'm talking about something like this :
But can I have multiple statements executed when the condition is true?
The above doesn't work (semicolons). Also, how would I get an else if in there? |
Author: | PaulButler [ Fri Apr 06, 2007 4:04 pm ] | ||||
Post subject: | RE:Can you have multiple statements executed in \'shortened\' conditionals? | ||||
This is called the conditional operator or ternary operator. I am not sure about the first question, but for the second question you can nest the operator. This pseudo-code else-if:
Could be done with nested ternary operators like this:
Keep in mind that if statements in ruby can return a value, unlike C or Java, so if you are just using the ternary operator so you can return a value, you don't have to. |
Author: | rdrake [ Fri Apr 06, 2007 4:26 pm ] | ||||
Post subject: | Re: Can you have multiple statements executed in 'shortened' conditionals? | ||||
richcash @ Fri Apr 06, 2007 2:25 pm wrote: Also, how would I get an else if in there? You'd have to use an if statement.
If you really wanted things to get ugly, you could do the following as well.
PaulButler @ Fri Apr 06, 2007 4:04 pm wrote: Keep in mind that if statements in ruby can return a value, unlike C or Java, so if you are just using the ternary operator so you can return a value, you don't have to. Statements that return a value, eh? You mean like an expression? |
Author: | PaulButler [ Fri Apr 06, 2007 5:35 pm ] |
Post subject: | RE:Can you have multiple statements executed in \'shortened\' conditionals? |
They return a value, but they still act like control structures. I think that makes them different from expressions, but I could be wrong. I realize now that the last line of my post is a bit ambiguous, how's this: Quote: Keep in mind that if-statements in ruby can return a value, unlike C or Java, so if you are just using the ternary operator so you can return a value, you don't have to. |
Author: | richcash [ Fri Apr 06, 2007 7:09 pm ] | ||
Post subject: | Re: Can you have multiple statements executed in 'shortened' conditionals? | ||
Thanks for the replies. Yeah, I've learned if statements and I am aware that they return values, I was using the ternary operator because it looks nicer in place of shorter if statements. I never thought of nested ternary operators, thanks! I thought of a way to have multiple statements executed. Since statements are expressions in Ruby :
Is that acceptable, or would it confuse readers? I think it looks better than sticking an elsif in the middle of a line. |
Author: | wtd [ Fri Apr 06, 2007 11:26 pm ] |
Post subject: | RE:Can you have multiple statements executed in \'shortened\' conditionals? |
Conditionals in Ruby are expressions. |
Author: | wtd [ Sat Apr 07, 2007 12:26 am ] | ||
Post subject: | Re: Can you have multiple statements executed in 'shortened' conditionals? | ||
richcash @ Sat Apr 07, 2007 8:09 am wrote: Thanks for the replies.
Yeah, I've learned if statements and I am aware that they return values, I was using the ternary operator because it looks nicer in place of shorter if statements. I never thought of nested ternary operators, thanks! I thought of a way to have multiple statements executed. Since statements are expressions in Ruby :
Is that acceptable, or would it confuse readers? I think it looks better than sticking an elsif in the middle of a line. Short-circuiting. Just use if, elsif and else. Sprinkle in some "then". |
Author: | PaulButler [ Sat Apr 07, 2007 9:01 am ] | ||
Post subject: | Re: Can you have multiple statements executed in 'shortened' conditionals? | ||
wtd @ Sat Apr 07, 2007 12:26 am wrote: richcash @ Sat Apr 07, 2007 8:09 am wrote:
Short-circuiting. Yeah, that's the problem. Unless you know that statement_a will return a true value, you can't be sure that statement_b will be executed. |
Author: | wtd [ Sat Apr 07, 2007 11:09 am ] |
Post subject: | RE:Can you have multiple statements executed in \'shortened\' conditionals? |
Alternatively you could use begin...end blocks. |
Author: | richcash [ Sat Apr 07, 2007 5:53 pm ] | ||||
Post subject: | Re: Can you have multiple statements executed in 'shortened' conditionals? | ||||
PaulButler wrote: wtd @ Sat Apr 07, 2007 12:26 am wrote: richcash @ Sat Apr 07, 2007 8:09 am wrote:
Short-circuiting. Yeah, that's the problem. Unless you know that statement_a will return a true value, you can't be sure that statement_b will be executed. Oh yeah, I forgot about statement_a returning false. You see statement_a in my case will not return a boolean value, it is an assignment of a string or integer. That's why it works for me. Thanks to both of you for pointing out that it won't work if your statements return boolean values. [edit] wtd wrote: Alternatively you could use begin...end blocks. Ah, yes. That's what I was looking for in the first place.
Cool! [/edit] |