ControlStructures.pdf

(87 KB) Pobierz
Introduction to the R Language
Control Structures
Roger Peng, Associate Professor
Johns Hopkins Bloomberg School of Public Health
Control Structures
Control structures in R allow you to control the flow of execution of the program, depending on
runtime conditions. Common structures are
·
if
,
else
: testing a condition
·
for
: execute a loop a fixed number of times
·
while
: execute a loop
while
a condition is true
·
repeat
: execute an infinite loop
·
break
: break the execution of a loop
·
next
: skip an interation of a loop
·
return
: exit a function
Most control structures are not used in interactive sessions, but rather when writing functions or
longer expresisons.
2/14
Control Structures: if
if(<condition>)
{
## do something
}
else
{
## do something else
}
if(<condition1>)
{
## do something
}
else if(<condition2>)
{
## do something different
}
else
{
## do something different
}
3/14
if
This is a valid if/else structure.
if(x
>
3)
{
y <-
10
}
else
{
y <-
0
}
So is this one.
y <-
if(x
>
3)
{
10
}
else
{
0
}
4/14
if
Of course, the else clause is not necessary.
if(<condition1>)
{
}
if(<condition2>)
{
}
5/14
Zgłoś jeśli naruszono regulamin