There are three groups of control statements in R: conditional statements, loop statements, and jump statements.
Conditional statements:
if
—tests whether a given condition is true and provides operations to perform if it’s so.if-else
—tests whether a given condition is true, provides operations to perform if it’s so and another set of operations to perform in the opposite case.if... else if... else
—tests a series of conditions one by one, provides operations to perform for each condition if it’s true, and a fallback set of operations to perform if none of those conditions is true.switch
—evaluates an expression against the items of a list and returns a value from the list based on the results of this evaluation.
Loop statements:
for
—in for loops, iterates over a sequence.while
—in while loops, checks if a predefined logical condition (or several logical conditions) is met at the current iteration.repeat
—in repeat loops, continues performing the same set of operations until a predefined break condition (or several break conditions) is met.
Jump statements:
next
—skips a particular iteration of a loop and jumps to the next one if a certain condition is met.break
—stops and exits the loop at a particular iteration if a certain condition is met.return
—exits a function and returns the result.
Leave a Reply