R is an expression language, meaning that its command type has only functions or expressions and returns a result from them.
The statement group is determined by the curly braces '{}', at which point the result is the result of the last statement in the group that can return a value.
conditional Statements
> if (expr_1) expr_2 else expr_3
Where the conditional expression EXPR1 must return a logical value, operator && and | | are often used in the condition section.
& | and &&, | | The difference is that,& and | are calculated in terms of element-wise,&& and | | The first element of the vector is evaluated, and the second parameter is evaluated only when required.
The vector version of the IF/ELSE structure is the function IfElse, which is in the form of IfElse (condition,a,b), and the rule that produces the function result is: If Condition[i] is true, the corresponding a[i] element The opposite is the b[i] element. According to this principle the function returns a vector consisting of the corresponding elements in a, B, the length of the vector and the longest parameter.
Looping Statements
> For (name in expr_1) expr_2
Where name is a loop variable, expr1 is a vector expression (usually a sequence of 1:20), and EXPR2 is often a group of expression statements, EXPR2 is repeated repeatedly as name takes the value of the expr1 result vector in turn.
> repeat(expr)
> while (condition) expr
The break statement can be used to interrupt any loop, possibly a non-normal interrupt. And this is the only way to stop the repeat loop.
The next statement can abort a specific loop and jump to the next one.