| View previous topic :: View next topic |
| Author |
Message |
kadamat
Joined: 01 Jun 2006 Posts: 88
|
LOL. *whistling sound of bomb dropping* can anyone tell me how to handle a situation where one might want to have multiple options or cases in a logical statement>? If else type stuff.
I have more than two cases though |
| |
|
|
|
|
Koplaf
Joined: 28 May 2006 Posts: 130
|
| Use the boolean AND/OR statements |
| |
|
|
ovigo22
Joined: 15 May 2006 Posts: 130
|
If (condition) { action } else if (condition) { action } else { action }
Ahh .. stuff like that .. |
| |
|
|
Mupolan
Joined: 30 May 2006 Posts: 101
|
| Or switch |
| |
|
|
ovigo22
Joined: 15 May 2006 Posts: 130
|
| If ( this || orthis ) { action } else if ( this && andthis ) { action } |
| |
|
|
Roombor
Joined: 02 Jun 2006 Posts: 111
|
| Sounds perfect for switch |
| |
|
|
AmeliaR
Joined: 31 May 2006 Posts: 142
|
| kadamat: er... switch/case ? |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| So, could it be if (condition) { action } else if (condition) { action } else { action } -------------and then an infinite number of continued else { action } else { action } else { action } ? |
| |
|
|
ovigo22
Joined: 15 May 2006 Posts: 130
|
Yeah .. switch if the LHS of the comparison is always the same
kadamat, no, only one will ever get called |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| So switch is better for this? |
| |
|
|
ovigo22
Joined: 15 May 2006 Posts: 130
|
| You can have as many 'else if' as you like |
| |
|
|
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| Cool... but it seems like extra keystrokes for a single situation |
| |
|
|
ovigo22
Joined: 15 May 2006 Posts: 130
|
Not necesarily .. switch is good for: if (X =1 ) { action } else if (X = 2) { action } else if (X = 3) {action} else {action} where the LHS of the condition is always the same
(think about multiple 'else' clasuses .. when would any but the first get executed? |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| Interesting |
| |
|
|
AmeliaR
Joined: 31 May 2006 Posts: 142
|
| ovigo22, heh, well not to get offtrack, but for that I think a for loop would be better :-P |
| |
|
|
ovigo22
Joined: 15 May 2006 Posts: 130
|
| Where? |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| Well, basically, I have a dropdown menu with four options. I need to set up a statement to handle all four. which would be the coolest/most elegant way to approach this? |
| |
|
|
ovigo22
Joined: 15 May 2006 Posts: 130
|
kadamat,switch would be as the LHS will always be the selectedIndex ..
(object full of functions == perl) |
| |
|
|
Gladis
Joined: 24 May 2006 Posts: 108
|
| Perl must die |
| |
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| Thanks guys. So switch it is! |
| |
|
|
Klimapol
Joined: 27 May 2006 Posts: 134
|
ovigo22, t's also python.
Object full of functions is tasty. |
| |
|
|
|
|
kadamat
Joined: 01 Jun 2006 Posts: 88
|
So, it would be used like: selection = document.frmCheckMe.giveaway.value;
Then switch (selection)
?
Then case "name of item selected" :
Then the stuff I want to happen
Then the next case and so on? |
| |
|
|
Gladis
Joined: 24 May 2006 Posts: 108
| |
kadamat
Joined: 01 Jun 2006 Posts: 88
|
| Ah. ha! Thanks. |
| |
|
|
Klimapol
Joined: 27 May 2006 Posts: 134
|
Gladis, Are the javascript tutors or Java tutors.
? |
| |
|
|
| Page 1 of 2 |
Goto page 1, 2 Next |
|