Playground-noun:a place where people can playimport uikit//------------------------------------------------------- -----------------------//1. for//traditional for loop mode also supports var num = 0for in Swift (var i = 0; i < i++) {num + = i}num//--------------------------------- ---------------------------------------------//2. For-in is used to traverse a range (range), sequence (sequence), set (collection), series (progression)//All elements execute a series of statements//1> closed interval loops, from 1 cycles to 10num = 0for I in 1...10 {num + = i}num//2> open interval loop, from 1 cycles to 9num = 0for i in 1..<10 {num + = i}num//3> If you do not need to know the value of each item in the interval, you can use an underscore (_ Instead of the variable name, omit access to the interval value num = 0for _ in 1...10 {num++}numnum = 0for I in 0...5 {to J in 0...5 {num = i + j}} num//------------------------------------------------------------------------------//3. Loop-generated array var array = [String] () for I in 0..<10 {array.append ("itcast \ (i)")}array//----------------------------------- -------------------------------------------//4. Loop Traversal dictionary var agedict:dictionary<string, int> = ["Mary ":", "Rose": 20]var names = [String] () var ages = [Int] () for (key, value) in Agedict {names.append (key) Ages.ap Pend (value)}namesagesvar keys = [string] () var values = [string] () var dict1 = ["Name": "Mike", "Age": "+"];for (name, age) In Dict1 {keys.append (name) values.append (age)}keysvalues//------------------------------------------------------ ------------------------//5. 1 of the functions that use labels to exit loops//Use labels are: You can explicitly specify which loop to exit, and the following example exits the loop with the name "Mainloop" var result = 1;mainloop:for I in 1...5 {for j in 1...5 {result + = J if (i = = 3 && j = 3) {Break Mainloop}}} Result
Swift Basic usage-for loop traversal, iterate through the dictionary, loop through the generated array