Again a big dream, also can not resist you like to insist!
I ran and ran, just to catch up with the expectations of their own.
Third, operator + expression
Swift allows overloading of operators, such as "+" you can overload it
Follow-up details will be introduced
assignment operator Pass
arithmetic operators Most pass, only say special
19/4 result is 4 instead of 5//truncated rounding
Complete complex math operations with mathematical functions in <math.h> in the ANSI C standard library
Import Foundation
var b = Pow (3.2,5)//3.2 5-Time Square
var C = sqrt (3.2)//square root
0~10 random number var d = arc4random ()%10
var e = sin (1.57)//1.57 as radians
-Negative
overflow operator , which truncates the overflow data bit and makes the program more robust
&+, &-,&*,&/,&%
Signed integers, the highest bit is the sign bit, 0 is a positive number, and 1 represents a negative number.
The computer saves the integer in the form of complement, the complement of positive number is the same as the source code, the complement of negative number = Inverse code +1 (bitwise negation, the highest bit unchanged)
Displacement Operators :
The left shift n is equal to n times 2, and the right shift is divided by the N-squared of 2.
Range Operators :
A...B includes a A, B boundary. For in will use the
var range = 2...6
For NUM in range
{
Pringtln (num*5)
}
A.. <b when iterating through an array (you know, because it starts with 0)
Let books = ["OC", "Swift", "Java", "Go"]
For index in 0..<books.count
{
println (Books[index])
}
comparison operators : = = = Variables that determine the reference type, return ture when referencing the same object
logical operators :
Two will short-circuit: is the front expression judgment, the back is not judged, there is no chance of execution, direct results
&& False if False
& with but no short circuit
Three mesh operator :
var c =5, d = 5
C>d println ("C greater than D"):(C<d?println ("C less than D"):p rintln ("C equals D"))
Nil merge operator:??
A?? b Determine whether a is nil, not nil, return a value, nil, return the default B value, the requirement type must be consistent!
Example Let DefaultName = "Renhairui"
var name:string?= "Engineer"
println (name?? defaultname)//Output Engineer
var namenil:string?
println (namenil?? defaultname)//Output Renhairui
Actually a?? b equivalent to a!=nil? a!:b
Iv. Process Control
Branching structure
Loop structure
If else to deal with a small scope first
var age = 45
If Age >60
{
println ("seniors")
}
else if age>40{
println ("Middle-aged")
}else if age>20{
println ("Young people")
}
Switch does not continue to execute the next case
var score = "C"
Switch score
{
Case "A", "a":
println ("excellent")
Fallthrough through
Default
Break directly ignore
}
var score = 87
Switch score{
Case 91...100:
println ("excellent")
Case similarly
}
var somepoint = (x:1,y:-4)
Switch Sompoint
{
Case (0,0):
println ("origin")
Case (_,0):
println ("x-axis")
Case (0,_):
println ("Y-axis")
Case (0...int.max,0...int.max):
println ("First quadrant")
}
Case Value Binding
var score= 178
Switch score
{
Case 91...100:
println ("Youxiu")
Case 0..<60:
println ("")
Case Let Grade:
println ("Input score \ (grade) illegal")
}
var somepoint = (x:2,y:0)
Switch Somepoint
{
Case (0,0):
println ()
Case (Var x,0):
println ("on x axis, x value is \ (x)")
Case (0,var y):
println ("On y-axis, y value is \ (y)")
Case (x, y):
println ("Location: \ (x), \ (y)")
}
Where to add additional judging conditions to the case
Case var (x, y) where x>0 && y>0:
println ("\ (x), \ (y) in the first quadrant")
Loop Body: Code that is executed repeatedly
A For loop can specify multiple initializations at the same time, or it can be an expression that contains a logical operator
for var b= 0,s=0,p = 0; b<10 && s<4 &&p<10;p++
{
}
return can end the entire method function
break completely ends a loop, jumping out of the loop body, no matter in that loop.
continue ignores the remaining statements of this loop
Swift Learning Note 3-4