Skip to content

nested loop

2 messages · Sébastien Bihorel, Duncan Murdoch

#
Hi all,

Does anybody know whether one can nest an 'if' statement in a 'for' 
loop. According to the results of my code, the for loop is performed 
first, but I'm not sure I got something else wrong with my code. I'm 
trying to perform the if statement for each step of the for loop. Thanks 
in advance.

Best regards,
Sebastian
#
On Sat, 21 Feb 2004 23:23:45 -0600, you wrote:

            
Yes, no problem:
+ if (i %% 2 == 0) cat(i, ' is even!\n')
+ }
2  is even!
4  is even!
6  is even!
8  is even!
10  is even!

If you leave off the braces ("{ }"), my example will still work, but
multi-line examples won't:  only the first line will be repeated in
the for loop.

Duncan Murdoch