Swift provides a variety of process control structures, including a while loop that can perform tasks multiple times , and select if, guard, and switch statements that perform different code branches based on specific criteria. There are also break and continue statements that control the process to jump to other code .
Swift also adds a for-in loop to make it easier to iterate over the group (array), Dictionary (dictionary), interval (range),string ( string) and other sequence types.
Swift's switch
statements are more powerful than the C language. In the C language, if a case is accidentally omitted, it break
runs through the next case,swift without writing break
, so this cross-cutting situation does not occur. The case can also match more type patterns.
1.for-in Cycle
- Swift uses the for-in loop to iterate through all the elements in a collection, such as the interval represented by a number, the elements in the array, and the characters in the string.
for inch 1... 5 { print ("\ (index) times 5 is \ (Index * 5)"); // If you do not need to know the value of each item within the interval sequence, you can use an underscore (_) to override the variable name to ignore access to the value for inch 1... 5 { print ("Hello");}
Traversal of arrays and dictionaries
2.While Cycle
The while Loop runs a series of statements until the condition becomes false. This type of loop is suitable for use in cases where the number of iterations before the first iteration is unknown. Swift provides two form of while loop:
- while loop, each time the loop starts, the calculation condition is met;
- repeat-while Loop, which calculates whether the condition meets at the end of the loop.
00; while start < end{ print (start); 1 0; repeat{ print (start); 1 ;} while start < end
3.conditional Statements
- It is often useful to execute specific code based on specific criteria, such as when the value entered is too large or too small, a message is displayed to the user, and so on.
- Swift provides two types of conditional statements: Theif statement and the switch statement. In general, use an if statement when the condition is simple and the likelihood is rare . The Switch statement is more suitable for situations where the conditions are more complex, more likely and require pattern matching (pattern-matching).
//IFLet Temperatureinfahrenheit = -;ifTemperatureinfahrenheit <= +{print ("It ' s very cold. Consider wearing a scarf.");}Else ifTemperatureinfahrenheit >= the{print ("It ' s really warm. Don ' t forget to wear sunscreen.");}Else{print ("It's not so cold. Wear a T-shirt.");}//Switch Base usageLet Somecharacter:character ="e";Switchsomecharacter{ Case "a","e","I","o","u": Print ("\ (somecharacter) is a vowel"); Case "b","C","D","F","g","h","J","k","L","m", "N","P","Q","R","s","T","v","W","x","y","Z": Print ("\ (somecharacter) is a consonant");default: Print ("\ (somecharacter) is not a vowel or a consonant");}//Description://1. You do not need to explicitly use the break statement in the case branch//2. A case can also contain multiple patterns, separating them with commas;//3. Each case branch must contain at least one statement. //switch interval matchingLet Approximatecount = +;Switchapproximatecount{ Case 0: Print ("No"); Case 1.. <5: Print ("a few"); Case 5.. < A: Print ("several"); Case A.. < -: Print ("dozens"); Case -.. < +: Print ("hundreds");default: Print ("many");}//Switch tuple matchingLet Somepoint = (1,1);Switchsomepoint{ Case(0,0): Print ("(0, 0) is at the Origin"); Case(Let X,0)://Value BindingPrint"(\ (somepoint.0), 0) is on the x-axis, x value of \ (x)"); Case(0, _): Print ("(0, \ (somepoint.1)) is on the y-axis"); Case(-2...2, -2...2): Print ("(\ (somepoint.0), \ (Somepoint.1)) is inside the box");default: Print ("(\ (somepoint.0), \ (Somepoint.1)) is outside of the box");}//Description: Use an underscore (_) to match all possible values
4. Control transfer Statements
swift There are five kinds of control transfer statements:
- Continue: The Continue statement tells a loop body to stop the loop iteration at once and start the next iteration of the loop again.
- Break: The break statement immediately ends the execution of the entire control flow.
- Fallthrough: Thefallthrough keyword does not check the next match condition that will fall into the executed case. Fallthrough simply allows code execution to continue to connect to The execution code in the next case, which is the same as the switch statement attribute in the C language standard .
- Return
- Throw
5. Control Flow