Pure Programmer
Blue Matrix


Cluster Map

Iteration

L1

This page is under construction. Please come back later.

Iteration1.swift
#!/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!
Iteration2.swift
#!/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!
Iteration3.swift
#!/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
Iteration4.swift
#!/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
Iteration5.swift
#!/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
Iteration6.swift
#!/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

swift

Questions

Projects

More ★'s indicate higher difficulty level.

References