[R] nested loop
Duncan Murdoch
dmurdoch at pair.com
Sun Feb 22 13:10:16 CET 2004
On Sat, 21 Feb 2004 23:23:45 -0600, you wrote:
>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.
Yes, no problem:
> for (i in 1:10) {
+ 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
More information about the R-help
mailing list