Control flow of Swift learning notes

Source: Internet
Author: User

UseIf andSwitch to make logical judgments, usingFor-in, < Span class= "Crayon-pre crayon-code" >for ,  while , and  < Span class= "Crayon-pre crayon-code" >repeat- while to do the loop. Using parentheses to enclose a condition or loop variable is no longer mandatory, but you still need to enclose the code block with curly braces

Let hscores = [ the, -, the, -, A]        varTeamscore =0         forScoreinchHscores {ifScore > -{Teamscore+=3Print ("what's here \ (Teamscore)")            }Else{Teamscore+=1Print ("What's this? \ (Teamscore)")            }        }

In a  if statement, the condition must be a Boolean expression--which means  if  hscore  { ...

var " Hello "  ==var"John Appleseed"var"Hello" ! " if let name = optionalname {    "Hello, \ (name)"} 

You can use it together.if and  let to manipulate those values that may be lost. These values are represented using the optional options. The optional value includes a value or a  nil to indicate that the value does not exist. Use a question mark behind the type of a value ( " to mark a value as optional

 var  optionalstring:string? =  " Span style= "color: #800000;" >hello   print (optionalstring  = =  nil)  var  optionalname:string? =  john Appleseed   " var  greeting =  " hello!   " if  let name = Optionalname {greeting  = 
     
       "
      hello, \ (name)  "  }

If the value of the option is  nil, the condition is  < Span class= "Crayon-pre crayon-code" >false and the code in the curly braces will be skipped. Otherwise, the value of the optional option is expanded and assigned to  let the constant declared behind, which makes the expanded value available to the code within the curly braces.

Another way to handle an optional value is to use the ?? operator to provide a default value. If an optional value is lost, the default value is used

Let nickname:string? ="John Appleseed""Hi \ (nickname?? FullName)"

The Switch selection statement supports any type of data and various types of comparison operations-it is no longer limited to integer and test equality

Let vegetable ="Red Pepper"SwitchVegetable { Case "Celery": Print ("Add Some raisins and make ants on a log.") Case "Cucumber","Watercress": Print ("That's would make a good tea sandwich.") CaseLet XwhereX.hassuffix ("Pepper"): Print ("is it a spicy \ (x)?")default: Print ("everything tastes good in soup.")}

Note Let can be used in a pattern to specify a matching value into a constant.

After executing the case that is matched in the switch statement, the program exits from the switch statement. Execution does not continue to jump to the next case, so there is absolutely no need to explicitly mark break after each case.

You can use for -in to traverse the items in the dictionary, which requires a pair of variable names to store the key-value pairs. Dictionaries use unordered collections, so the traversal of key values is also unordered

Let interestingnumbers = [    "Prime": [2,3,5,7, One, -],    "Fibonacci": [1,1,2,3,5,8],    "Square": [1,4,9, -, -],]varlargest =0 for(Kind, numbers)inchInterestingnumbers { forNumberinchNumbers {ifNumber >Largest {largest=Number}}}  Print (largest)

Use  while to repeat the code fast until the conditions change. The condition of the loop can be placed at the end, which guarantees that the loop runs at least once

var 2  while  - {    2  var2Repeat {     2  while theprint (m)

Control flow of Swift learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.