-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfor-and-while.html
371 lines (330 loc) · 25.2 KB
/
for-and-while.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>7 For and While | Programming for Data Analysis and Statistics with R</title>
<meta name="description" content="7 For and While | Programming for Data Analysis and Statistics with R" />
<meta name="generator" content="bookdown 0.20 and GitBook 2.6.7" />
<meta property="og:title" content="7 For and While | Programming for Data Analysis and Statistics with R" />
<meta property="og:type" content="book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="7 For and While | Programming for Data Analysis and Statistics with R" />
<meta name="author" content="Diren Senger" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="conditions-if-and-else.html"/>
<link rel="next" href="writing-own-functions.html"/>
<script src="book_assets/jquery-2.2.3/jquery.min.js"></script>
<link href="book_assets/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter" data-level="1" data-path="about.html"><a href="about.html"><i class="fa fa-check"></i><b>1</b> About</a></li>
<li class="chapter" data-level="2" data-path="installation-notes.html"><a href="installation-notes.html"><i class="fa fa-check"></i><b>2</b> Installation Notes</a></li>
<li class="chapter" data-level="3" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html"><i class="fa fa-check"></i><b>3</b> RStudio Programming Environment</a><ul>
<li class="chapter" data-level="3.1" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html#creating-a-new-project"><i class="fa fa-check"></i><b>3.1</b> Creating a new project</a></li>
<li class="chapter" data-level="3.2" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html#rstudio-screen"><i class="fa fa-check"></i><b>3.2</b> RStudio screen</a></li>
<li class="chapter" data-level="3.3" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html#r-workflow"><i class="fa fa-check"></i><b>3.3</b> R Workflow</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="r-data-types.html"><a href="r-data-types.html"><i class="fa fa-check"></i><b>4</b> R Data types</a><ul>
<li class="chapter" data-level="4.1" data-path="r-data-types.html"><a href="r-data-types.html#character"><i class="fa fa-check"></i><b>4.1</b> Character</a></li>
<li class="chapter" data-level="4.2" data-path="r-data-types.html"><a href="r-data-types.html#numeric"><i class="fa fa-check"></i><b>4.2</b> Numeric</a></li>
<li class="chapter" data-level="4.3" data-path="r-data-types.html"><a href="r-data-types.html#logical"><i class="fa fa-check"></i><b>4.3</b> Logical</a></li>
<li class="chapter" data-level="4.4" data-path="r-data-types.html"><a href="r-data-types.html#vectors"><i class="fa fa-check"></i><b>4.4</b> Vectors</a></li>
<li class="chapter" data-level="4.5" data-path="r-data-types.html"><a href="r-data-types.html#factors"><i class="fa fa-check"></i><b>4.5</b> Factors</a></li>
<li class="chapter" data-level="4.6" data-path="r-data-types.html"><a href="r-data-types.html#list-of-lists"><i class="fa fa-check"></i><b>4.6</b> List of lists</a></li>
<li class="chapter" data-level="4.7" data-path="r-data-types.html"><a href="r-data-types.html#matrices"><i class="fa fa-check"></i><b>4.7</b> Matrices</a></li>
<li class="chapter" data-level="4.8" data-path="r-data-types.html"><a href="r-data-types.html#data-frames"><i class="fa fa-check"></i><b>4.8</b> Data frames</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html"><i class="fa fa-check"></i><b>5</b> Preparing your data for analysis</a><ul>
<li class="chapter" data-level="5.1" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#reading-the-data"><i class="fa fa-check"></i><b>5.1</b> Reading the data</a></li>
<li class="chapter" data-level="5.2" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#transforming-entire-columns"><i class="fa fa-check"></i><b>5.2</b> Transforming entire columns</a></li>
<li class="chapter" data-level="5.3" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#subsetting-data"><i class="fa fa-check"></i><b>5.3</b> Subsetting data</a></li>
<li class="chapter" data-level="5.4" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#useful-functions"><i class="fa fa-check"></i><b>5.4</b> Useful functions</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="conditions-if-and-else.html"><a href="conditions-if-and-else.html"><i class="fa fa-check"></i><b>6</b> Conditions - If and Else</a><ul>
<li class="chapter" data-level="6.1" data-path="conditions-if-and-else.html"><a href="conditions-if-and-else.html#if"><i class="fa fa-check"></i><b>6.1</b> If</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="for-and-while.html"><a href="for-and-while.html"><i class="fa fa-check"></i><b>7</b> For and While</a><ul>
<li class="chapter" data-level="7.1" data-path="for-and-while.html"><a href="for-and-while.html#for"><i class="fa fa-check"></i><b>7.1</b> For</a></li>
<li class="chapter" data-level="7.2" data-path="for-and-while.html"><a href="for-and-while.html#while"><i class="fa fa-check"></i><b>7.2</b> While</a></li>
<li class="chapter" data-level="7.3" data-path="for-and-while.html"><a href="for-and-while.html#do-while"><i class="fa fa-check"></i><b>7.3</b> Do-while</a></li>
<li class="chapter" data-level="7.4" data-path="for-and-while.html"><a href="for-and-while.html#for-vs.while"><i class="fa fa-check"></i><b>7.4</b> For vs. While</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="writing-own-functions.html"><a href="writing-own-functions.html"><i class="fa fa-check"></i><b>8</b> Writing own functions</a></li>
<li class="chapter" data-level="9" data-path="debugging.html"><a href="debugging.html"><i class="fa fa-check"></i><b>9</b> Debugging</a></li>
<li class="chapter" data-level="10" data-path="piping.html"><a href="piping.html"><i class="fa fa-check"></i><b>10</b> Piping</a></li>
<li class="chapter" data-level="11" data-path="plotting.html"><a href="plotting.html"><i class="fa fa-check"></i><b>11</b> Plotting</a><ul>
<li class="chapter" data-level="11.1" data-path="plotting.html"><a href="plotting.html#standard-library"><i class="fa fa-check"></i><b>11.1</b> Standard library</a><ul>
<li class="chapter" data-level="11.1.1" data-path="plotting.html"><a href="plotting.html#points-or-lines-type"><i class="fa fa-check"></i><b>11.1.1</b> Points or Lines? (type)</a></li>
<li class="chapter" data-level="11.1.2" data-path="plotting.html"><a href="plotting.html#line-type-lty"><i class="fa fa-check"></i><b>11.1.2</b> Line type (lty)</a></li>
</ul></li>
<li class="chapter" data-level="11.2" data-path="plotting.html"><a href="plotting.html#ggplot"><i class="fa fa-check"></i><b>11.2</b> ggplot</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="r-markdown.html"><a href="r-markdown.html"><i class="fa fa-check"></i><b>12</b> R-Markdown</a><ul>
<li class="chapter" data-level="12.1" data-path="r-markdown.html"><a href="r-markdown.html#output-formats"><i class="fa fa-check"></i><b>12.1</b> Output formats</a></li>
<li class="chapter" data-level="12.2" data-path="r-markdown.html"><a href="r-markdown.html#document-structure"><i class="fa fa-check"></i><b>12.2</b> Document structure</a></li>
<li class="chapter" data-level="12.3" data-path="r-markdown.html"><a href="r-markdown.html#code-blocks"><i class="fa fa-check"></i><b>12.3</b> Code blocks</a></li>
<li class="chapter" data-level="12.4" data-path="r-markdown.html"><a href="r-markdown.html#lists"><i class="fa fa-check"></i><b>12.4</b> Lists</a></li>
<li class="chapter" data-level="12.5" data-path="r-markdown.html"><a href="r-markdown.html#insert-an-image"><i class="fa fa-check"></i><b>12.5</b> Insert an image</a></li>
<li class="chapter" data-level="12.6" data-path="r-markdown.html"><a href="r-markdown.html#further-documentation"><i class="fa fa-check"></i><b>12.6</b> Further documentation</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html"><i class="fa fa-check"></i><b>13</b> Short statistical breaks</a><ul>
<li class="chapter" data-level="13.1" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#dinosours"><i class="fa fa-check"></i><b>13.1</b> Dinosours</a></li>
<li class="chapter" data-level="13.2" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#stars"><i class="fa fa-check"></i><b>13.2</b> Stars</a></li>
<li class="chapter" data-level="13.3" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#correlation"><i class="fa fa-check"></i><b>13.3</b> Correlation</a></li>
<li class="chapter" data-level="13.4" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#beginners-book"><i class="fa fa-check"></i><b>13.4</b> Beginners Book</a></li>
</ul></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Programming for Data Analysis and Statistics with R</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="for-and-while" class="section level1">
<h1><span class="header-section-number">7</span> For and While</h1>
<div id="for" class="section level2">
<h2><span class="header-section-number">7.1</span> For</h2>
<div class="sourceCode" id="cb100"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb100-1" data-line-number="1">ages <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">31</span>, <span class="dv">29</span>, <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb100-2" data-line-number="2">total.age <-<span class="st"> </span><span class="dv">0</span></a>
<a class="sourceLine" id="cb100-3" data-line-number="3"><span class="cf">for</span>(i <span class="cf">in</span> ages){</a>
<a class="sourceLine" id="cb100-4" data-line-number="4"> total.age <-<span class="st"> </span>total.age <span class="op">+</span><span class="st"> </span>i</a>
<a class="sourceLine" id="cb100-5" data-line-number="5">}</a>
<a class="sourceLine" id="cb100-6" data-line-number="6">total.age</a></code></pre></div>
<pre><code>## [1] 65</code></pre>
<div class="sourceCode" id="cb102"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb102-1" data-line-number="1">myfamily <-<span class="st"> </span><span class="kw">list</span>(<span class="dt">Paula=</span><span class="dv">31</span>, <span class="dt">Ali=</span><span class="dv">29</span>, <span class="dt">Emma=</span><span class="dv">5</span>)</a>
<a class="sourceLine" id="cb102-2" data-line-number="2">age <-<span class="st"> </span><span class="dv">0</span></a>
<a class="sourceLine" id="cb102-3" data-line-number="3"><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="kw">length</span>(myfamily)){</a>
<a class="sourceLine" id="cb102-4" data-line-number="4"> age <-<span class="st"> </span>age <span class="op">+</span><span class="st"> </span>myfamily[[i]]</a>
<a class="sourceLine" id="cb102-5" data-line-number="5">}</a>
<a class="sourceLine" id="cb102-6" data-line-number="6">age</a></code></pre></div>
<pre><code>## [1] 65</code></pre>
<p>apply it to columns:</p>
<div class="sourceCode" id="cb104"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb104-1" data-line-number="1"><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="kw">ncol</span>(df)){</a>
<a class="sourceLine" id="cb104-2" data-line-number="2"> name <-<span class="st"> </span><span class="kw">names</span>(df)[i]</a>
<a class="sourceLine" id="cb104-3" data-line-number="3"> colclass <-<span class="st"> </span><span class="kw">class</span>(df[,i])</a>
<a class="sourceLine" id="cb104-4" data-line-number="4"> <span class="kw">print</span>(<span class="kw">paste</span>(name, <span class="st">":"</span>, colclass))</a>
<a class="sourceLine" id="cb104-5" data-line-number="5">}</a></code></pre></div>
<pre><code>## [1] "time : POSIXct" "time : POSIXt"
## [1] "hive : integer"
## [1] "h : numeric"
## [1] "t_i_1 : numeric"
## [1] "t_i_2 : numeric"
## [1] "t_i_3 : numeric"
## [1] "t_i_4 : numeric"
## [1] "t_i_5 : numeric"
## [1] "t_o : numeric"
## [1] "weight_kg : numeric"
## [1] "weight_g : numeric"
## [1] "highTemp : logical"</code></pre>
<p>transform long code …</p>
<div class="sourceCode" id="cb106"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb106-1" data-line-number="1">df <-<span class="st"> </span><span class="kw">read.csv</span>(<span class="st">"beedata.csv"</span>)</a>
<a class="sourceLine" id="cb106-2" data-line-number="2"><span class="kw">print</span>(<span class="st">"mean of h:"</span>)</a>
<a class="sourceLine" id="cb106-3" data-line-number="3"><span class="kw">mean</span>(df<span class="op">$</span>h)</a>
<a class="sourceLine" id="cb106-4" data-line-number="4"><span class="kw">print</span>(<span class="st">"mean of t_i_1:"</span>)</a>
<a class="sourceLine" id="cb106-5" data-line-number="5"><span class="kw">mean</span>(df<span class="op">$</span>t_i_<span class="dv">1</span>)</a>
<a class="sourceLine" id="cb106-6" data-line-number="6"><span class="kw">print</span>(<span class="st">"mean of weight_kg:"</span>)</a>
<a class="sourceLine" id="cb106-7" data-line-number="7"><span class="kw">mean</span>(df<span class="op">$</span>weight_kg)</a>
<a class="sourceLine" id="cb106-8" data-line-number="8"><span class="kw">print</span>(<span class="st">"missing h values:"</span>)</a>
<a class="sourceLine" id="cb106-9" data-line-number="9"><span class="kw">sum</span>(<span class="kw">is.na</span>(df<span class="op">$</span>h))</a>
<a class="sourceLine" id="cb106-10" data-line-number="10"><span class="kw">print</span>(<span class="st">"missing t_i_1 values:"</span>)</a>
<a class="sourceLine" id="cb106-11" data-line-number="11"><span class="kw">sum</span>(<span class="kw">is.na</span>(df<span class="op">$</span>h))</a>
<a class="sourceLine" id="cb106-12" data-line-number="12"><span class="kw">print</span>(<span class="st">"missing weight_kg values:"</span>)</a>
<a class="sourceLine" id="cb106-13" data-line-number="13"><span class="kw">sum</span>(<span class="kw">is.na</span>(df<span class="op">$</span>h))</a></code></pre></div>
<p>… into shorter code</p>
<div class="sourceCode" id="cb107"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb107-1" data-line-number="1">to.analyse <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"h"</span>, <span class="st">"t_i_1"</span>, <span class="st">"weight_kg"</span>)</a>
<a class="sourceLine" id="cb107-2" data-line-number="2"><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="kw">length</span>(to.analyse)){</a>
<a class="sourceLine" id="cb107-3" data-line-number="3"> <span class="kw">print</span>(<span class="kw">paste</span>(<span class="st">"mean of"</span>, to.analyse[i], <span class="st">":"</span>))</a>
<a class="sourceLine" id="cb107-4" data-line-number="4"> <span class="kw">print</span>(<span class="kw">mean</span>(to.analyse[i]))</a>
<a class="sourceLine" id="cb107-5" data-line-number="5"> <span class="kw">print</span>(<span class="kw">paste</span>(<span class="st">"missing"</span>, to.analyse[i], <span class="st">"values:"</span>))</a>
<a class="sourceLine" id="cb107-6" data-line-number="6"> <span class="kw">print</span>(<span class="kw">sum</span>(<span class="kw">is.na</span>(df[,to.analyse[i]])))</a>
<a class="sourceLine" id="cb107-7" data-line-number="7">}</a></code></pre></div>
<p><img src="images/for.png" style="width:75.0%" /></p>
</div>
<div id="while" class="section level2">
<h2><span class="header-section-number">7.2</span> While</h2>
<div class="sourceCode" id="cb108"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb108-1" data-line-number="1">a <-<span class="st"> </span><span class="dv">2</span></a>
<a class="sourceLine" id="cb108-2" data-line-number="2"><span class="cf">while</span> (a<span class="op"><</span><span class="dv">1000</span>){</a>
<a class="sourceLine" id="cb108-3" data-line-number="3"> a <-<span class="st"> </span>a<span class="op">^</span><span class="dv">2</span></a>
<a class="sourceLine" id="cb108-4" data-line-number="4"> <span class="kw">print</span>(a)</a>
<a class="sourceLine" id="cb108-5" data-line-number="5">}</a></code></pre></div>
<pre><code>## [1] 4
## [1] 16
## [1] 256
## [1] 65536</code></pre>
<p><img src="images/while.png" style="width:75.0%" /></p>
</div>
<div id="do-while" class="section level2">
<h2><span class="header-section-number">7.3</span> Do-while</h2>
<div class="sourceCode" id="cb110"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb110-1" data-line-number="1">a <-<span class="st"> </span><span class="dv">10</span></a>
<a class="sourceLine" id="cb110-2" data-line-number="2"><span class="cf">repeat</span>{</a>
<a class="sourceLine" id="cb110-3" data-line-number="3"> a <-<span class="st"> </span>a <span class="op">-</span><span class="st"> </span><span class="kw">sqrt</span>(a)</a>
<a class="sourceLine" id="cb110-4" data-line-number="4"> <span class="kw">print</span>(a)</a>
<a class="sourceLine" id="cb110-5" data-line-number="5"> <span class="cf">if</span>(<span class="kw">sqrt</span>(a) <span class="op">></span><span class="st"> </span>a){</a>
<a class="sourceLine" id="cb110-6" data-line-number="6"> <span class="cf">break</span></a>
<a class="sourceLine" id="cb110-7" data-line-number="7"> }</a>
<a class="sourceLine" id="cb110-8" data-line-number="8">}</a></code></pre></div>
<pre><code>## [1] 6.837722
## [1] 4.222818
## [1] 2.167869
## [1] 0.6955003</code></pre>
<p>bad example for Do-while:</p>
<div class="sourceCode" id="cb112"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb112-1" data-line-number="1">a <-<span class="st"> </span><span class="fl">0.5</span></a>
<a class="sourceLine" id="cb112-2" data-line-number="2"><span class="cf">repeat</span>{</a>
<a class="sourceLine" id="cb112-3" data-line-number="3"> a <-<span class="st"> </span>a <span class="op">-</span><span class="st"> </span><span class="kw">sqrt</span>(a)</a>
<a class="sourceLine" id="cb112-4" data-line-number="4"> <span class="kw">print</span>(a)</a>
<a class="sourceLine" id="cb112-5" data-line-number="5"> <span class="cf">if</span>(<span class="kw">sqrt</span>(a) <span class="op">></span><span class="st"> </span>a){</a>
<a class="sourceLine" id="cb112-6" data-line-number="6"> <span class="cf">break</span></a>
<a class="sourceLine" id="cb112-7" data-line-number="7"> }</a>
<a class="sourceLine" id="cb112-8" data-line-number="8">}</a></code></pre></div>
<pre><code>## [1] -0.2071068</code></pre>
<pre><code>## Warning in sqrt(a): NaNs produced</code></pre>
<pre><code>## Error in if (sqrt(a) > a) {: missing value where TRUE/FALSE needed</code></pre>
<p><img src="images/do-while.png" style="width:75.0%" /></p>
</div>
<div id="for-vs.while" class="section level2">
<h2><span class="header-section-number">7.4</span> For vs. While</h2>
<ul>
<li>if you do not know the number of iterations needed, use while</li>
<li>else, always use for!</li>
<li>it is easier to create a while-loop that never stops, than creating a for-loop that never stops.</li>
</ul>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="conditions-if-and-else.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="writing-own-functions.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="book_assets/gitbook-2.6.7/js/app.min.js"></script>
<script src="book_assets/gitbook-2.6.7/js/lunr.js"></script>
<script src="book_assets/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="book_assets/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "subsection"
},
"search": false
});
});
</script>
</body>
</html>