Iteration

This page is under construction. Please come back later.
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
var max:Int = 5
if CommandLine.arguments.count == 2 {
max = Int(CommandLine.arguments[1]) ?? 5
}
for _:Int in 0..<max {
print("Hello, world!")
}
exit(EXIT_SUCCESS)
}
main()
Output
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
#!/usr/bin/env swift;
import Foundation
func main() -> Void {
for i:Int in stride(from: 10, through: 1, by: -1) {
print(String(i))
}
print("Blastoff!")
exit(EXIT_SUCCESS)
}
main()
Output
10
9
8
7
6
5
4
3
2
1
Blastoff!
#!/usr/bin/env swift;
import Foundation
func main() -> Void {
print("Even numbers up to 100...")
for i:Int in stride(from: 2, through: 100, by: 2) {
print(String(i))
}
exit(EXIT_SUCCESS)
}
main()
Output
Even numbers up to 100...
2
4
6
8
10
12
14
16
18
...
82
84
86
88
90
92
94
96
98
100
#!/usr/bin/env swift;
import Foundation
func main() -> Void {
print("Floating point numbers")
for d:Double in stride(from: 1.0, to: 2.01, by: 0.1) {
print(String(d))
}
exit(EXIT_SUCCESS)
}
main()
Output
Floating point numbers
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9000000000000001
2.0
#!/usr/bin/env swift;
import Foundation
func main() -> Void {
print("Powers of 2 up to 256!")
var i:Int = 1
while i <= 256 {
print(String(i))
i *= 2
}
exit(EXIT_SUCCESS)
}
main()
Output
Powers of 2 up to 256!
1
2
4
8
16
32
64
128
256
#!/usr/bin/env swift;
import Foundation
func main() -> Void {
var s:String = "Hello, world! 🥰🇺🇸"
for c in s {
print(c)
}
exit(EXIT_SUCCESS)
}
main()
Output
Iteration6.swift:5:6: warning: variable 's' was never mutated; consider changing to 'let' constant
3 |
4 | func main() -> Void {
5 | var s:String = "Hello, world! 🥰🇺🇸"
| `- warning: variable 's' was never mutated; consider changing to 'let' constant
6 | for c in s {
7 | print(c)
H
e
l
l
o
,
w
o
r
l
d
!
🥰
🇺🇸
Iterators
swiftQuestions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Amortization Table
- Amortization Table (Revisited)
- Blackboard
- Blackboard (Revisited)
- Character Types
- Compute π (Monte Carlo)
- Floating Point Error
- ISBN-10 Check
- License Plate Generator
- Multiplication Table
- Number Guessing Game
- Punchcard Message
- Random Floats
- Random Integers
- Supermarket Sale
- Supermarket Sale (Revisited)
- Temperature Table
- Unicode Test
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]
Pure Programmer


