Related tools
while(condition) action
: performs action while condition is TRUE.repeat(action)
: repeats action forever (i.e. until it encounters break).Note that
for
can be rewritten aswhile
and while can be rewritten asrepeat
(this goes in one direction only!); however:
Good practice is to use the least-flexible solution to a problem, so you should use
for
wherever possible. BUT you shouldn’t even use for loops for data analysis tasks asmap()
andapply()
already provide less flexible solutions to most problems. (More in Chapter 9.)
x_option <- function(x) {
switch(x,
a = "option 1",
b = "option 2",
c = "option 3"#,
#stop("Invalid `x` value")
)
}