From: John Joyce Date: 2007-03-15T14:41:53+09:00 Subject: Re: Newbie: if / elseif So it doesn't work like the C variety? if ( condition ) statement // maybe done else if ( condition ) statement // or maybe done else statement // done if the 1st and 2nd are not done Honestly, my biggest difficulties so far are that I think of code somewhere between C and PHP (I like to call it a nicer C) On Mar 15, 2007, at 12:20 PM, Chad Perrin wrote: > On Thu, Mar 15, 2007 at 12:12:17PM +0900, Augie De Blieck Jr. wrote: >>> if $val =~ /this/i >>> puts "this" >>> elseif $val =~ /that/i >>> puts "that" >>> elseif $val =~ /the other/i >>> puts "the other" >>> end >> >> Just remember that even when you spell it right, it's going to drop >> out of the whole construct just as soon as it fulfills a RegEx. >> After >> it prints "this," it stops looking at any of the other "elsif" >> statements. >> >> That short circuiting is a feature. =) > > Indeed -- that's the whole point of using elsif rather than just a > list > of separate if statements. The if/elsif/else construct is used for > mutually exclusive flow control, where only one of several is to be > executed. Using a series of if statements considers each in a vacuum, > in order, while if/elsif/else considers them all as part of a greater > whole. > > -- > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] > "Real ugliness is not harsh-looking syntax, but having to > build programs out of the wrong concepts." - Paul Graham >