Swift basic operator

Source: Internet
Author: User
Tags arithmetic operators


01-Assignment and arithmetic operators


// assignment operator
let (regular task, overtime task) = (4, 2)

// arithmetic operator
1 + 1
7-3
3 * 4
10.0 / 6
02-Excess and increase and decrease
// remainder operator
10% 6
10% -6
-10% 6

// take the remainder of the floating-point number
1024% 3.5

// Increment and decrement
var Like = 0

// Increment first and then return, this method is recommended
++ Like

// return first and then increment
Like ++

// string concatenation
"Puppy" + "??"
03- negative and combination assignment
// take negative
let three = 3
let minus three = -three

// combined assignment statement
var number = 1

// equivalent to number = number + 3
number + = 3
04-Comparison and ternary operators
// comparison operator
1 == 1
2! = 1
2> 1
1 <2
1> = 1
2 <= 1

let welcome message = "Welcome to learn Swift"
if (welcome message == "Welcome to learn Swift") {
    println ("You are very welcome")
} else {
    println ("Ha ha")
}

// Ternary operator
let wholesale price = 50
let member = false
let basic markup = 20
let member markup = 10
var price = wholesale price + (member? member markup: basic markup)

// Use if else to implement the above function
if (member) {
    Sale price = wholesale price + basic markup
} else {
    Sale price = wholesale price + member's markup
}
5-closed interval and half interval
// closed interval
for year in (1949 ... 2015) {
    println ("\ (year) is the \ (year-1949 + 1) year")
}

// open range
var star list = ["Andy Lau", "Li Jetie", "Guo Fucheng", "Zhang Xueyou"]
var number of stars = list of stars. count

for sort order in 0 .. <number of stars {
    println ("The \ (ranking order + 1) star is \ (star list [order]]")
}
6-logical operators
// Logical Operators
let enter = true

// logical NOT
if! please enter {
    println ("can enter")
} else {
    println ("Deny enter")
}

// logical AND and logical OR
let password is correct = false
let retina correct = false
let master key = true

if (password correct && retina correct || master key) {
    println ("can enter")
} else {
    println ("Deny enter")
}
07-Selection operator
// select and operator
let bottom line brand = "Xiaomi"

// optional variables
var Ideal Brand: String?

Ideal Brand = "iPhone 6"
var Actual Start = (Ideal Brand ?? Bottom Line Brand)
 

Swift basic operators

Related Article

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.