This is a blog about me, Seinfeld, okonomiyaki, japanese toilet seats, and other things of interest

Tuesday, July 25, 2006

for bla bla else bla bla

Ever written something like this?:

found = false
for i in list
if i == wanted
found = true
do_something()
break

if not found
do_something_else()

In Python there is a nice construction that makes this much easier - the for else statement. The same code in Python can be written as:

for i in list:
if i == wanted:
do_something()
break
else:
do_something_else()

The else-statement is executed if the for-loop is terminated by exhaustion of the list. Pretty neat. Is this available in any other language?

2 Comments:

Blogger RoboGeek said...

Mmmm...

I think what you want is an archaic language known as BaySick. It was very popular among the early so-called High-level Language Programming-in-my-bedroom officianados back in the mid-eighties.

The story of BaySick's rise and fall is a tortuous and tragic one, but the denoument is widely agreed to be centred around the now infamous "GOTO" construct. For all its glory, failings and leaps into the deep-blue of uninitialised memory, you could do this:

I=0
LOOP:
PRINT "DO-SOMETHING"
IF FOUND GOTO OBLIVION
I=I+1
IF I>10 GOTO OVER:
GOTO LOOP

OVER:
PRINT "MORE PROGRAM"
GOTO END

OBLIVION:
PRINT "NOT SO SHABBY HUH"

END:

12:15 PM

 
Anonymous Anonymous said...

The wonderful world of ruby:

if list.include? i
do_something
else
do_something_else
end


kinda cheating, but cool.

2:07 AM

 

Post a Comment

<< Home