Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//: [For-in](@previous)

let flag = true

while (true) {
print("Hello")
if flag { break }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Styling mark: I don't like single line control flow statements, but it's up to you.

print("World")
}

print("There")


//: [Continue](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//: [Break](@previous)

var i = 0
while (i < 10) {
i += 1
print("Hello")
if i == 5 { continue }
print("World")
}

//: [forEach](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//: [Repeat-While](@previous)

//: offset - index of element
let arr = [7, 10, 1, 5, 2]

for i in arr.enumerated() {
print(i.offset, i.element)
}


//: [For-in](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//: [Enumerated](@previous)

let arr = [7, 10, 1, 5, 2]
for i in arr {
print(i)
}

//: [Break](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//: stride(from:to:by:) iterating over collection. C-style loop has been removed
for i in stride(from: 0, to: 10, by: 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it would be better to show how to use decimal loop bounds and increment. E. g. for i in stride(from: 1.3, to: 8.5, by: 1.7) { … }. Because current example may be easily replaced with its preferred variant: for i in 0..<10 { … }.

print(i)
}



//: [While](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//: [Continue](@previous)

let a = [7, 10, 1, 5, 2]

a.forEach { x in
print (x)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I supposed that space before (x) is excessive.

}

a.forEach {
print($0)
}

[7,10,1,5].forEach {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would advise to add spaces between elements.

print($0)
}


//: [Map](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//: [ForEach](@previous)

let a = [7, 10, 1, 5, 2]

a.map {
$0 * 2
}

a.map { x in
x * 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//: [While](@previous)

//: Note: Do-While has been removed. Repeat-While instead.
var i = 0
repeat {
i += 1
print(i)
} while(i < 10)


//: [Enumerated](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//: [For](@previous)
var a = 0
while (a < 10) {
a += 1
print(a)
}
//: [Repeat-While](@next)



14 changes: 14 additions & 0 deletions Swift/Iteration.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='macos' display-mode='raw'>
<pages>
<page name='for'/>
<page name='while'/>
<page name='repeat-while'/>
<page name='enumerated'/>
<page name='for-in'/>
<page name='Break'/>
<page name='Continue'/>
<page name='forEach'/>
<page name='map'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.