Repository navigation
Expand file tree
/
Copy pathStatements.html
More file actions
1016 lines (973 loc) · 64.8 KB
/
Copy pathStatements.html
File metadata and controls
1016 lines (973 loc) · 64.8 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Statements</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter nav-visited-chapter">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name">Language Guide
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="The Basics.html#TP40014097-CH5-XID_399" data-id="TP40014097-CH5-XID_399" class="">The Basics</a>
</li>
<li class="nav-chapter">
<a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70" class="">Basic Operators</a>
</li>
<li class="nav-chapter">
<a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368" class="">Strings and Characters</a>
</li>
<li class="nav-chapter">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133" class="">Collection Types</a>
</li>
<li class="nav-chapter">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153" class="">Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204" class="">Functions</a>
</li>
<li class="nav-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117" class="">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185" class="">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94" class="">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323" class="">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300" class="">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393" class="">Subscripts</a>
</li>
<li class="nav-chapter">
<a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251" class="">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266" class="">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182" class="">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50" class="">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312" class="">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443" class="">Type Casting</a>
</li>
<li class="nav-chapter">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309" class="">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191" class="">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345" class="">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234" class="">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28" class="">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name nav-part-active open-part">Language Reference
<ul class="nav-chapters" style="height: 299px;">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" class="">About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794" class="">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988" class="">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655" class="">Expressions</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="#" data-id="TP40014097-CH33-XID_913" class="nav-chapter-active">Statements</a>
</li>
<li class="nav-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460">Attributes</a>
</li>
<li class="nav-chapter">
<a href="Patterns.html#TP40014097-CH36-XID_878" data-id="TP40014097-CH36-XID_878">Patterns</a>
</li>
<li class="nav-chapter">
<a href="Generic Parameters and Arguments.html#TP40014097-CH37-XID_774" data-id="TP40014097-CH37-XID_774">Generic Parameters and Arguments</a>
</li>
<li class="nav-chapter">
<a href="Summary of the Grammar.html#TP40014097-CH38-XID_1030" data-id="TP40014097-CH38-XID_1030">Summary of the Grammar</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="chapter">
<a name="TP40014097-CH33"></a><a name="TP40014097-CH33-XID_913"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Statements</h2>
<section id="mini_toc" class="hideInXcode slide-out" role="navigation">
<div id="mini_toc_button">
<p>On This Page</p>
</div>
<ul class="list-bullet">
<li class="item">
<p class="para">
<a href="#TP40014097-CH33-XID_922">
Loop Statements
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH33-XID_943">
Branch Statements
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH33-XID_965">
Labeled Statement
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH33-XID_970">
Control Transfer Statements
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
In Swift, there are two kinds of statements: simple statements and control flow statements. Simple statements are the most common and consist of either an expression or a declaration. Control flow statements are used to control the flow of execution in a program. There are three types of control flow statements in Swift: loop statements, branch statements, and control transfer statements.
</p>
<p class="para">
Loop statements allow a block of code to be executed repeatedly, branch statements allow a certain block of code to be executed only when certain conditions are met, and control transfer statements provide a way to alter the order in which code is executed. Each type of control flow statement is described in detail below.
</p>
<p class="para">
A semicolon (<code class="code-voice">;</code>) can optionally appear after any statement and is used to separate multiple statements if they appear on the same line.
</p>
<div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/statement"></a>
<span class="syntax-def-name">
statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><span class="optional"><code class="literal">;</code><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_916"></a>
<span class="syntax-def-name">
statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/declaration" data-id="//apple_ref/swift/grammar/declaration">declaration</a></span><span class="optional"><code class="literal">;</code><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_917"></a>
<span class="syntax-def-name">
statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/loop-statement" data-id="//apple_ref/swift/grammar/loop-statement">loop-statement</a></span><span class="optional"><code class="literal">;</code><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_918"></a>
<span class="syntax-def-name">
statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/branch-statement" data-id="//apple_ref/swift/grammar/branch-statement">branch-statement</a></span><span class="optional"><code class="literal">;</code><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_919"></a>
<span class="syntax-def-name">
statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/labeled-statement" data-id="//apple_ref/swift/grammar/labeled-statement">labeled-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_920"></a>
<span class="syntax-def-name">
statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/control-transfer-statement" data-id="//apple_ref/swift/grammar/control-transfer-statement">control-transfer-statement</a></span><span class="optional"><code class="literal">;</code><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/statements"></a>
<span class="syntax-def-name">
statements
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/statement" data-id="//apple_ref/swift/grammar/statement">statement</a></span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/statements" data-id="//apple_ref/swift/grammar/statements">statements</a></span><sub class="subscript">opt</sub></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_922"></a>
<h3 class="section-name" tabindex="0">Loop Statements</h3>
<p class="para">
Loop statements allow a block of code to be executed repeatedly, depending on the conditions specified in the loop. Swift has four loop statements: a <code class="code-voice">for</code> statement, a <code class="code-voice">for</code>-<code class="code-voice">in</code> statement, a <code class="code-voice">while</code> statement, and a <code class="code-voice">do</code>-<code class="code-voice">while</code> statement.
</p><p class="para">
Control flow in a loop statement can be changed by a <code class="code-voice">break</code> statement and a <code class="code-voice">continue</code> statement and is discussed in <span class="x-name"><a href="#TP40014097-CH33-XID_976" data-id="TP40014097-CH33-XID_976">Break Statement</a></span> and <span class="x-name"><a href="#TP40014097-CH33-XID_979" data-id="TP40014097-CH33-XID_979">Continue Statement</a></span> below.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a loop statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/loop-statement"></a>
<span class="syntax-def-name">
loop-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/for-statement" data-id="//apple_ref/swift/grammar/for-statement">for-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_925"></a>
<span class="syntax-def-name">
loop-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/for-in-statement" data-id="//apple_ref/swift/grammar/for-in-statement">for-in-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_926"></a>
<span class="syntax-def-name">
loop-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/while-statement" data-id="//apple_ref/swift/grammar/while-statement">while-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_927"></a>
<span class="syntax-def-name">
loop-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/do-while-statement" data-id="//apple_ref/swift/grammar/do-while-statement">do-while-statement</a></span>
</p>
</div>
</div>
<section class="section">
<a name="TP40014097-CH33-XID_928"></a>
<h3 class="section-name" tabindex="0">For Statement</h3>
<p class="para">
A <code class="code-voice">for</code> statement allows a block of code to be executed repeatedly while incrementing a counter, as long as a condition remains true.
</p><p class="para">
A <code class="code-voice">for</code> statement has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">for</span> <em class="variable-text">initialization</em>; <em class="variable-text">condition</em>; <em class="variable-text">increment</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
The semicolons between the <em>initialization</em>, <em>condition</em>, and <em>increment</em> are required. The braces around the <em>statements</em> in the body of the loop are also required.
</p><p class="para">
A <code class="code-voice">for</code> statement is executed as follows:
</p><ol class="list-number">
<li class="item"><p class="para">
The <em>initialization</em> is evaluated only once. It is typically used to declare and initialize any variables that are needed for the remainder of the loop.
</p>
</li><li class="item"><p class="para">
The <em>condition</em> expression is evaluated.
</p>
<p class="para">
If <code class="code-voice">true</code>, the program executes the <em>statements</em>, and execution continues to step 3. If <code class="code-voice">false</code>, the program does not execute the <em>statements</em> or the <em>increment</em> expression, and the program is finished executing the <code class="code-voice">for</code> statement.
</p>
</li><li class="item"><p class="para">
The <em>increment</em> expression is evaluated, and execution returns to step 2.
</p>
</li>
</ol><p class="para">
Variables defined within the <em>initialization</em> are valid only within the scope of the <code class="code-voice">for</code> statement itself.
</p><p class="para">
The value of the <em>condition</em> expression must have a type that conforms to the <code class="code-voice">LogicValue</code> protocol.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a for statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/for-statement"></a>
<span class="syntax-def-name">
for-statement
</span>
<span class="arrow">
→
</span><code class="literal">for</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/for-init" data-id="//apple_ref/swift/grammar/for-init">for-init</a></span><sub class="subscript">opt</sub></span><code class="literal">;</code><span class="optional"><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><sub class="subscript">opt</sub></span><code class="literal">;</code><span class="optional"><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/code-block" data-id="//apple_ref/swift/grammar/code-block">code-block</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_931"></a>
<span class="syntax-def-name">
for-statement
</span>
<span class="arrow">
→
</span><code class="literal">for</code><code class="literal">(</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/for-init" data-id="//apple_ref/swift/grammar/for-init">for-init</a></span><sub class="subscript">opt</sub></span><code class="literal">;</code><span class="optional"><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><sub class="subscript">opt</sub></span><code class="literal">;</code><span class="optional"><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><sub class="subscript">opt</sub></span><code class="literal">)</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/code-block" data-id="//apple_ref/swift/grammar/code-block">code-block</a></span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/for-init"></a>
<span class="syntax-def-name">
for-init
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/variable-declaration" data-id="//apple_ref/swift/grammar/variable-declaration">variable-declaration</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression-list" data-id="//apple_ref/swift/grammar/expression-list">expression-list</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_933"></a>
<h3 class="section-name" tabindex="0">For-In Statement</h3>
<p class="para">
A <code class="code-voice">for</code>-<code class="code-voice">in</code> statement allows a block of code to be executed once for each item in a collection (or any type) that conforms to the <code class="code-voice">Sequence</code> protocol.
</p><p class="para">
A <code class="code-voice">for</code>-<code class="code-voice">in</code> statement has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">for</span> <em class="variable-text">item</em> <span class="kt">in</span> <em class="variable-text">collection</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
The <code class="code-voice">generate</code> method is called on the <em>collection</em> expression to obtain a value of a generator type—that is, a type that conforms to the <code class="code-voice">Generator</code> protocol. The program begins executing a loop by calling the <code class="code-voice">next</code> method on the stream. If the value returned is not <code class="code-voice">None</code>, it is assigned to the <em>item</em> pattern, the program executes the <em>statements</em>, and then continues execution at the beginning of the loop. Otherwise, the program does not perform assignment or execute the <em>statements</em>, and it is finished executing the <code class="code-voice">for</code>-<code class="code-voice">in</code> statement.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a for-in statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/for-in-statement"></a>
<span class="syntax-def-name">
for-in-statement
</span>
<span class="arrow">
→
</span><code class="literal">for</code><span class="syntactic-cat"><a href="Patterns.html#//apple_ref/swift/grammar/pattern" data-id="//apple_ref/swift/grammar/pattern">pattern</a></span><code class="literal">in</code><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/code-block" data-id="//apple_ref/swift/grammar/code-block">code-block</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_936"></a>
<h3 class="section-name" tabindex="0">While Statement</h3>
<p class="para">
A <code class="code-voice">while</code> statement allows a block of code to be executed repeatedly, as long as a condition remains true.
</p><p class="para">
A <code class="code-voice">while</code> statement has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">while</span> <em class="variable-text">condition</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
A <code class="code-voice">while</code> statement is executed as follows:
</p><ol class="list-number">
<li class="item"><p class="para">
The <em>condition</em> is evaluated.
</p>
<p class="para">
If <code class="code-voice">true</code>, execution continues to step 2. If <code class="code-voice">false</code>, the program is finished executing the <code class="code-voice">while</code> statement.
</p>
</li><li class="item"><p class="para">
The program executes the <em>statements</em>, and execution returns to step 1.
</p>
</li>
</ol><p class="para">
Because the value of the <em>condition</em> is evaluated before the <em>statements</em> are executed, the <em>statements</em> in a <code class="code-voice">while</code> statement can be executed zero or more times.
</p><p class="para">
The value of the <em>condition</em> must have a type that conforms to the <code class="code-voice">LogicValue</code> protocol. The condition can also be an optional binding declaration, as discussed in <span class="x-name"><a href="The Basics.html#TP40014097-CH5-XID_432" data-id="TP40014097-CH5-XID_432">Optional Binding</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a while statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/while-statement"></a>
<span class="syntax-def-name">
while-statement
</span>
<span class="arrow">
→
</span><code class="literal">while</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/while-condition" data-id="//apple_ref/swift/grammar/while-condition">while-condition</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/code-block" data-id="//apple_ref/swift/grammar/code-block">code-block</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/while-condition"></a>
<span class="syntax-def-name">
while-condition
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="Declarations.html#//apple_ref/swift/grammar/declaration" data-id="//apple_ref/swift/grammar/declaration">declaration</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_940"></a>
<h3 class="section-name" tabindex="0">Do-While Statement</h3>
<p class="para">
A <code class="code-voice">do</code>-<code class="code-voice">while</code> statement allows a block of code to be executed one or more times, as long as a condition remains true.
</p><p class="para">
A <code class="code-voice">do</code>-<code class="code-voice">while</code> statement has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">do</span> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">} <span class="kt">while</span> <em class="variable-text">condition</em></pre></li>
</ul>
</div><p class="para">
A <code class="code-voice">do</code>-<code class="code-voice">while</code> statement is executed as follows:
</p><ol class="list-number">
<li class="item"><p class="para">
The program executes the <em>statements</em>, and execution continues to step 2.
</p>
</li><li class="item"><p class="para">
The <em>condition</em> is evaluated.
</p>
<p class="para">
If <code class="code-voice">true</code>, execution returns to step 1. If <code class="code-voice">false</code>, the program is finished executing the <code class="code-voice">do</code>-<code class="code-voice">while</code> statement.
</p>
</li>
</ol><p class="para">
Because the value of the <em>condition</em> is evaluated after the <em>statements</em> are executed, the <em>statements</em> in a <code class="code-voice">do</code>-<code class="code-voice">while</code> statement are executed at least once.
</p><p class="para">
The value of the <em>condition</em> must have a type that conforms to the <code class="code-voice">LogicValue</code> protocol. The condition can also be an optional binding declaration, as discussed in <span class="x-name"><a href="The Basics.html#TP40014097-CH5-XID_432" data-id="TP40014097-CH5-XID_432">Optional Binding</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a do-while statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/do-while-statement"></a>
<span class="syntax-def-name">
do-while-statement
</span>
<span class="arrow">
→
</span><code class="literal">do</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/code-block" data-id="//apple_ref/swift/grammar/code-block">code-block</a></span><code class="literal">while</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/while-condition" data-id="//apple_ref/swift/grammar/while-condition">while-condition</a></span>
</p>
</div>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_943"></a>
<h3 class="section-name" tabindex="0">Branch Statements</h3>
<p class="para">
Branch statements allow the program to execute certain parts of code depending on the value of one or more conditions. The values of the conditions specified in a branch statement control how the program branches and, therefore, what block of code is executed. Swift has two branch statements: an <code class="code-voice">if</code> statement and a <code class="code-voice">switch</code> statement.
</p><p class="para">
Control flow in a <code class="code-voice">switch</code> statement can be changed by a <code class="code-voice">break</code> statement and is discussed in <span class="x-name"><a href="#TP40014097-CH33-XID_976" data-id="TP40014097-CH33-XID_976">Break Statement</a></span> below.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a branch statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/branch-statement"></a>
<span class="syntax-def-name">
branch-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/if-statement" data-id="//apple_ref/swift/grammar/if-statement">if-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_946"></a>
<span class="syntax-def-name">
branch-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/switch-statement" data-id="//apple_ref/swift/grammar/switch-statement">switch-statement</a></span>
</p>
</div>
</div>
<section class="section">
<a name="TP40014097-CH33-XID_947"></a>
<h3 class="section-name" tabindex="0">If Statement</h3>
<p class="para">
An <code class="code-voice">if</code> statement is used for executing code based on the evaluation of one or more conditions.
</p><p class="para">
There are two basic forms of an <code class="code-voice">if</code> statement. In each form, the opening and closing braces are required.
</p><p class="para">
The first form allows code to be executed only when a condition is true and has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">if</span> <em class="variable-text">condition</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
The second form of an <code class="code-voice">if</code> statement provides an additional <em>else clause</em> (introduced by the <code class="code-voice">else</code> keyword) and is used for executing one part of code when the condition is true and another part code when the same condition is false. When a single else clause is present, an <code class="code-voice">if</code> statement has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">if</span> <em class="variable-text">condition</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements to execute if condition is true</em></pre></li><li><pre class="code-voice">} <span class="kt">else</span> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements to execute if condition is false</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
The else clause of an <code class="code-voice">if</code> statement can contain another <code class="code-voice">if</code> statement to test more than one condition. An <code class="code-voice">if</code> statement chained together in this way has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">if</span> <em class="variable-text">condition 1</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements to execute if condition 1 is true</em></pre></li><li><pre class="code-voice">} <span class="kt">else</span> <span class="kt">if</span> <em class="variable-text">condition 2</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements to execute if condition 2 is true</em></pre></li><li><pre class="code-voice">} <span class="kt">else</span> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements to execute if both conditions are false</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
The value of any condition in an <code class="code-voice">if</code> statement must have a type that conforms to the <code class="code-voice">LogicValue</code> protocol. The condition can also be an optional binding declaration, as discussed in <span class="x-name"><a href="The Basics.html#TP40014097-CH5-XID_432" data-id="TP40014097-CH5-XID_432">Optional Binding</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an if statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/if-statement"></a>
<span class="syntax-def-name">
if-statement
</span>
<span class="arrow">
→
</span><code class="literal">if</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/if-condition" data-id="//apple_ref/swift/grammar/if-condition">if-condition</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/code-block" data-id="//apple_ref/swift/grammar/code-block">code-block</a></span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/else-clause" data-id="//apple_ref/swift/grammar/else-clause">else-clause</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/if-condition"></a>
<span class="syntax-def-name">
if-condition
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="Declarations.html#//apple_ref/swift/grammar/declaration" data-id="//apple_ref/swift/grammar/declaration">declaration</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/else-clause"></a>
<span class="syntax-def-name">
else-clause
</span>
<span class="arrow">
→
</span><span class="alternative">
<code class="literal">else</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/code-block" data-id="//apple_ref/swift/grammar/code-block">code-block</a></span>
</span><span class="alternative">
<code class="literal">else</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/if-statement" data-id="//apple_ref/swift/grammar/if-statement">if-statement</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_952"></a>
<h3 class="section-name" tabindex="0">Switch Statement</h3>
<p class="para">
A <code class="code-voice">switch</code> statement allows certain blocks of code to be executed depending on the value of a control expression.
</p><p class="para">
A switch statement has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">switch</span> <em class="variable-text">control expression</em> {</pre></li><li><pre class="code-voice"><span class="kt">case</span> <em class="variable-text">pattern 1</em>:</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice"><span class="kt">case</span> <em class="variable-text">pattern 2</em> <span class="kt">where</span> <em class="variable-text">condition</em>:</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice"><span class="kt">case</span> <em class="variable-text">pattern 3</em> <span class="kt">where</span> <em class="variable-text">condition</em>,</pre></li><li><pre class="code-voice"><em class="variable-text">pattern 4</em> <span class="kt">where</span> <em class="variable-text">condition</em>:</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice"><span class="kt">default</span>:</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
The <em>control expression</em> of the <code class="code-voice">switch</code> statement is evaluated and then compared with the patterns specified in each case. If a match is found, the program executes the <em>statements</em> listed within the scope of that case. The scope of each case can’t be empty. As a result, you must include at least one statement following the colon (<code class="code-voice">:</code>) of each case label. Use a single <code class="code-voice">break</code> statement if you don’t intend to execute any code in the body of a matched case.
</p><p class="para">
The values of expressions your code can branch on is very flexible. For instance, in addition to the values of scalar types, such as integers and characters, your code can branch on the values of any type, including floating-point numbers, strings, tuples, instances of custom classes, and optionals. The value of the <em>control expression</em> can even be matched to the value of a case in an enumeration and checked for inclusion in a specified range of values. For examples of how to use these various types of values in <code class="code-voice">switch</code> statements, see <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_163" data-id="TP40014097-CH9-XID_163">Switch</a></span> in the <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153">Control Flow</a></span> chapter.
</p><p class="para">
A <code class="code-voice">switch</code> case can optionally contain a guard expression after each pattern. A <em>guard expression</em> is introduced by the keyword <code class="code-voice">where</code> followed by an expression, and is used to provide an additional condition before a pattern in a case is considered matched to the <em>control expression</em>. If a guard expression is present, the <em>statements</em> within the relevant case are executed only if the value of the <em>control expression</em> matches one of the patterns of the case and the guard expression evaluates to <code class="code-voice">true</code>. For instance, a <em>control expression</em> matches the case in the example below only if it is a tuple that contains two elements of the same value, such as <code class="code-voice">(1, 1)</code>.
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">case</span> <span class="kt">let</span> (<span class="vc">x</span>, <span class="vc">y</span>) <span class="kt">where</span> <span class="vc">x</span> == <span class="vc">y</span>:</code></li>
</ul>
</div>
</section><p class="para">
As the above example shows, patterns in a case can also bind constants using the keyword <code class="code-voice">let</code> (they can also bind variables using the keyword <code class="code-voice">var</code>). These constants (or variables) can then be referenced in a corresponding guard expression and throughout the rest of the code within the scope of the case. That said, if the case contains multiple patterns that match the control expression, none of those patterns can contain constant or variable bindings.
</p><p class="para">
A <code class="code-voice">switch</code> statement can also include a default case, introduced by the keyword <code class="code-voice">default</code>. The code within a default case is executed only if no other cases match the control expression. A <code class="code-voice">switch</code> statement can include only one default case, which must appear at the end of the <code class="code-voice">switch</code> statement.
</p><p class="para">
Although the actual execution order of pattern-matching operations, and in particular the evaluation order of patterns in cases, is unspecified, pattern matching in a <code class="code-voice">switch</code> statement behaves as if the evaluation is performed in source order—that is, the order in which they appear in source code. As a result, if multiple cases contain patterns that evaluate to the same value, and thus can match the value of the control expression, the program executes only the code within the first matching case in source order.
</p>
<section class="section">
<a name="TP40014097-CH33-XID_953"></a>
<h3 class="section-name" tabindex="0">Switch Statements Must Be Exhaustive</h3>
<p class="para">
In Swift, every possible value of the control expression’s type must match the value of at least one pattern of a case. When this simply isn’t feasible (for instance, when the control expression’s type is <code class="code-voice">Int</code>), you can include a default case to satisfy the requirement.
</p>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_954"></a>
<h3 class="section-name" tabindex="0">Execution Does Not Fall Through Cases Implicitly</h3>
<p class="para">
After the code within a matched case has finished executing, the program exits from the <code class="code-voice">switch</code> statement. Program execution does not continue or “fall through” to the next case or default case. That said, if you want execution to continue from one case to the next, explicitly include a <code class="code-voice">fallthrough</code> statement, which simply consists of the keyword <code class="code-voice">fallthrough</code>, in the case from which you want execution to continue. For more information about the <code class="code-voice">fallthrough</code> statement, see <span class="x-name"><a href=" #TP40014097-CH33-XID_982" data-id="TP40014097-CH33-XID_982">Fallthrough Statement</a></span> below.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a switch statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/switch-statement"></a>
<span class="syntax-def-name">
switch-statement
</span>
<span class="arrow">
→
</span><code class="literal">switch</code><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><code class="literal">{</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/switch-cases" data-id="//apple_ref/swift/grammar/switch-cases">switch-cases</a></span><sub class="subscript">opt</sub></span><code class="literal">}</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/switch-cases"></a>
<span class="syntax-def-name">
switch-cases
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/switch-case" data-id="//apple_ref/swift/grammar/switch-case">switch-case</a></span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/switch-cases" data-id="//apple_ref/swift/grammar/switch-cases">switch-cases</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/switch-case"></a>
<span class="syntax-def-name">
switch-case
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/case-label" data-id="//apple_ref/swift/grammar/case-label">case-label</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/statements" data-id="//apple_ref/swift/grammar/statements">statements</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/default-label" data-id="//apple_ref/swift/grammar/default-label">default-label</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/statements" data-id="//apple_ref/swift/grammar/statements">statements</a></span>
</span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_959"></a>
<span class="syntax-def-name">
switch-case
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/case-label" data-id="//apple_ref/swift/grammar/case-label">case-label</a></span><code class="literal">;</code>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/default-label" data-id="//apple_ref/swift/grammar/default-label">default-label</a></span><code class="literal">;</code>
</span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/case-label"></a>
<span class="syntax-def-name">
case-label
</span>
<span class="arrow">
→
</span><code class="literal">case</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/case-item-list" data-id="//apple_ref/swift/grammar/case-item-list">case-item-list</a></span><code class="literal">:</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/case-item-list"></a>
<span class="syntax-def-name">
case-item-list
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="Patterns.html#//apple_ref/swift/grammar/pattern" data-id="//apple_ref/swift/grammar/pattern">pattern</a></span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/guard-clause" data-id="//apple_ref/swift/grammar/guard-clause">guard-clause</a></span><sub class="subscript">opt</sub></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="Patterns.html#//apple_ref/swift/grammar/pattern" data-id="//apple_ref/swift/grammar/pattern">pattern</a></span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/guard-clause" data-id="//apple_ref/swift/grammar/guard-clause">guard-clause</a></span><sub class="subscript">opt</sub></span><code class="literal">,</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/case-item-list" data-id="//apple_ref/swift/grammar/case-item-list">case-item-list</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/default-label"></a>
<span class="syntax-def-name">
default-label
</span>
<span class="arrow">
→
</span><code class="literal">default</code><code class="literal">:</code>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/guard-clause"></a>
<span class="syntax-def-name">
guard-clause
</span>
<span class="arrow">
→
</span><code class="literal">where</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/guard-expression" data-id="//apple_ref/swift/grammar/guard-expression">guard-expression</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/guard-expression"></a>
<span class="syntax-def-name">
guard-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span>
</p>
</div>
</div>
</section>
</section>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_965"></a>
<h3 class="section-name" tabindex="0">Labeled Statement</h3>
<p class="para">
You can prefix a loop statement or a <code class="code-voice">switch</code> statement with a <em>statement label</em>, which consists of the name of the label followed immediately by a colon (:). Use statement labels with <code class="code-voice">break</code> and <code class="code-voice">continue</code> statements to be explicit about how you want to change control flow in a loop statement or a <code class="code-voice">switch</code> statement, as discussed in <span class="x-name"><a href="#TP40014097-CH33-XID_976" data-id="TP40014097-CH33-XID_976">Break Statement</a></span> and <span class="x-name"><a href="#TP40014097-CH33-XID_979" data-id="TP40014097-CH33-XID_979">Continue Statement</a></span> below.
</p><p class="para">
The scope of a labeled statement is the entire statement following the statement label. You can nest labeled statements, but the name of each statement label must be unique.
</p><p class="para">
For more information and to see examples of how to use statement labels, see <span class="x-name"><a href="Control Flow.html #TP40014097-CH9-XID_180" data-id="TP40014097-CH9-XID_180">Labeled Statements</a></span> in the <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153">Control Flow</a></span> chapter.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a labeled statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/labeled-statement"></a>
<span class="syntax-def-name">
labeled-statement
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/statement-label" data-id="//apple_ref/swift/grammar/statement-label">statement-label</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/loop-statement" data-id="//apple_ref/swift/grammar/loop-statement">loop-statement</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/statement-label" data-id="//apple_ref/swift/grammar/statement-label">statement-label</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/switch-statement" data-id="//apple_ref/swift/grammar/switch-statement">switch-statement</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/statement-label"></a>
<span class="syntax-def-name">
statement-label
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/label-name" data-id="//apple_ref/swift/grammar/label-name">label-name</a></span><code class="literal">:</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/label-name"></a>
<span class="syntax-def-name">
label-name
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Lexical Structure.html#//apple_ref/swift/grammar/identifier" data-id="//apple_ref/swift/grammar/identifier">identifier</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_970"></a>
<h3 class="section-name" tabindex="0">Control Transfer Statements</h3>
<p class="para">
Control transfer statements can change the order in which code in your program is executed by unconditionally transferring program control from one piece of code to another. Swift has four control transfer statements: a <code class="code-voice">break</code> statement, a <code class="code-voice">continue</code> statement, a <code class="code-voice">fallthrough</code> statement, and a <code class="code-voice">return</code> statement.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a control transfer statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/control-transfer-statement"></a>
<span class="syntax-def-name">
control-transfer-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/break-statement" data-id="//apple_ref/swift/grammar/break-statement">break-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_973"></a>
<span class="syntax-def-name">
control-transfer-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/continue-statement" data-id="//apple_ref/swift/grammar/continue-statement">continue-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_974"></a>
<span class="syntax-def-name">
control-transfer-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Control Flow.html#//apple_ref/swift/grammar/fallthrough-statement" data-id="//apple_ref/swift/grammar/fallthrough-statement">fallthrough-statement</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH33-XID_975"></a>
<span class="syntax-def-name">
control-transfer-statement
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/return-statement" data-id="//apple_ref/swift/grammar/return-statement">return-statement</a></span>
</p>
</div>
</div>
<section class="section">
<a name="TP40014097-CH33-XID_976"></a>
<h3 class="section-name" tabindex="0">Break Statement</h3>
<p class="para">
A <code class="code-voice">break</code> statement ends program execution of a loop or a <code class="code-voice">switch</code> statement. A <code class="code-voice">break</code> statement can consist of only the keyword <code class="code-voice">break</code>, or it can consist of the keyword <code class="code-voice">break</code> followed by the name of a statement label, as shown below.
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">break</span></pre></li><li><pre class="code-voice"><span class="kt">break</span> <em class="variable-text">label name</em></pre></li>
</ul>
</div><p class="para">
When a <code class="code-voice">break</code> statement is followed by the name of a statement label, it ends program execution of the loop or <code class="code-voice">switch</code> statement named by that label.
</p><p class="para">
When a <code class="code-voice">break</code> statement is not followed by the name of a statement label, it ends program execution of the <code class="code-voice">switch</code> statement or the innermost enclosing loop statement in which it occurs.
</p><p class="para">
In both cases, program control is then transferred to the first line of code following the enclosing loop or <code class="code-voice">switch</code> statement, if any.
</p><p class="para">
For examples of how to use a <code class="code-voice">break</code> statement, see <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_174" data-id="TP40014097-CH9-XID_174">Break</a></span> and <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_180" data-id="TP40014097-CH9-XID_180">Labeled Statements</a></span> in the <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153">Control Flow</a></span> chapter.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a break statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/break-statement"></a>
<span class="syntax-def-name">
break-statement
</span>
<span class="arrow">
→
</span><code class="literal">break</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/label-name" data-id="//apple_ref/swift/grammar/label-name">label-name</a></span><sub class="subscript">opt</sub></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_979"></a>
<h3 class="section-name" tabindex="0">Continue Statement</h3>
<p class="para">
A <code class="code-voice">continue</code> statement ends program execution of the current iteration of a loop statement but does not stop execution of the loop statement. A <code class="code-voice">continue</code> statement can consist of only the keyword <code class="code-voice">continue</code>, or it can consist of the keyword <code class="code-voice">continue</code> followed by the name of a statement label, as shown below.
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">continue</span></pre></li><li><pre class="code-voice"><span class="kt">continue</span> <em class="variable-text">label name</em></pre></li>
</ul>
</div><p class="para">
When a <code class="code-voice">continue</code> statement is followed by the name of a statement label, it ends program execution of the current iteration of the loop statement named by that label.
</p><p class="para">
When a <code class="code-voice">continue</code> statement is not followed by the name of a statement label, it ends program execution of the current iteration of the innermost enclosing loop statement in which it occurs.
</p><p class="para">
In both cases, program control is then transferred to the condition of the enclosing loop statement.
</p><p class="para">
In a <code class="code-voice">for</code> statement, the increment expression is still evaluated after the <code class="code-voice">continue</code> statement is executed, because the increment expression is evaluated after the execution of the loop’s body.
</p><p class="para">
For examples of how to use a <code class="code-voice">continue</code> statement, see <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_172" data-id="TP40014097-CH9-XID_172">Continue</a></span> and <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_180" data-id="TP40014097-CH9-XID_180">Labeled Statements</a></span> in the <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153">Control Flow</a></span> chapter.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a continue statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/continue-statement"></a>
<span class="syntax-def-name">
continue-statement
</span>
<span class="arrow">
→
</span><code class="literal">continue</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/label-name" data-id="//apple_ref/swift/grammar/label-name">label-name</a></span><sub class="subscript">opt</sub></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_982"></a>
<h3 class="section-name" tabindex="0">Fallthrough Statement</h3>
<p class="para">
A <code class="code-voice">fallthrough</code> statement consists of the <code class="code-voice">fallthrough</code> keyword and occurs only in a case block of a <code class="code-voice">switch</code> statement. A <code class="code-voice">fallthrough</code> statement causes program execution to continue from one case in a <code class="code-voice">switch</code> statement to the next case. Program execution continues to the next case even if the patterns of the case label do not match the value of the <code class="code-voice">switch</code> statement’s control expression.
</p><p class="para">
A <code class="code-voice">fallthrough</code> statement can appear anywhere inside a <code class="code-voice">switch</code> statement, not just as the last statement of a case block, but it can’t be used in the final case block. It also cannot transfer control into a case block whose pattern contains value binding patterns.
</p><p class="para">
For an example of how to use a <code class="code-voice">fallthrough</code> statement in a <code class="code-voice">switch</code> statement, see <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_171" data-id="TP40014097-CH9-XID_171">Control Transfer Statements</a></span> in the <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153">Control Flow</a></span> chapter.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a fallthrough statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/fallthrough-statement"></a>
<span class="syntax-def-name">
fallthrough-statement
</span>
<span class="arrow">
→
</span><code class="literal">fallthrough</code>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH33-XID_985"></a>
<h3 class="section-name" tabindex="0">Return Statement</h3>
<p class="para">
A <code class="code-voice">return</code> statement occurs only in the body of a function or method definition and causes program execution to return to the calling function or method. Program execution continues at the point immediately following the function or method call.
</p><p class="para">
A <code class="code-voice">return</code> statement can consist of only the keyword <code class="code-voice">return</code>, or it can consist of the keyword <code class="code-voice">return</code> followed by an expression, as shown below.
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">return</span></pre></li><li><pre class="code-voice"><span class="kt">return</span> <em class="variable-text">expression</em></pre></li>
</ul>
</div><p class="para">
When a <code class="code-voice">return</code> statement is followed by an expression, the value of the expression is returned to the calling function or method. If the value of the expression does not match the value of the return type declared in the function or method declaration, the expression’s value is converted to the return type before it is returned to the calling function or method.
</p><p class="para">
When a <code class="code-voice">return</code> statement is not followed by an expression, it can be used only to return from a function or method that does not return a value (that is, when the return type of the function or method is <code class="code-voice">Void</code> or <code class="code-voice">()</code>).
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a return statement
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/return-statement"></a>
<span class="syntax-def-name">
return-statement
</span>
<span class="arrow">
→
</span><code class="literal">return</code><span class="optional"><span class="syntactic-cat"><a href="Expressions.html#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><sub class="subscript">opt</sub></span>
</p>
</div>
</div>
</section>
</section>