Язык программирования R/Управляющие структуры: различия между версиями

Содержимое удалено Содержимое добавлено
Циклы "for"
Циклы с предусловием ("while")
Строка 47:
 
=== Циклы с предусловием ("while") ===
Синтаксис цикла while вполне стандартен:
When it is not possible to use the for statement, you can also use break or while by specifying a breaking rules. One should be careful with this kind of loops since if the breaking rules is misspecified the loop will never end.
# Ключевое слово ''while''.
# Условие выполнимости в скобках.
# Список функций для итерированного выполнения в фигурных скобках.
<pre>
> g <- 0
> while (g < 1){
+ g <- rnorm(1)
+ cat(g,"\n")
+ }
-0.08111594
0.1732847
-0.2428368
0.3359238
-0.2080000
0.05458533
0.2627001
1.009195
</pre>
When it is not possible to use the for statement, you can also use break or while by specifying a breaking rules. One should be careful with this kind of loops since if the breaking rules is misspecified the loop will never end.
In the two examples below the standard normal distribution is drawn in as long as the value is lower than 1. The cat() function is used to display the present value on screen.
<pre>