Operators and Expressions (i)

Source: Internet
Author: User
Tags bitwise modulus ranges sin square root
Main.swift//Operator and expression (i)////Created by Goddog on 15/6/23. Copyright (c) 2015 Goddog.
All rights reserved.  Import Foundation//mark:-Assignment operator//with = as assignment operator var str = "Swift" var pi:double = 3.14 var Visited:bool = true Var str1:string = str//swift does not support continuous assignment var a:int//var b = a = Ten//mark:-Arithmetic operator//Add, subtract, multiply, divide//pay attention to the remainder operation: the operator is not an integer on both sides of the required operators, and supports the Floating-point var a1 = 12.1 var B1 = 4.2 var mod = a1% B1 println (mod) println ("negative modulus negative: \ (-12.3-2.4)")//Return negative number println ("Negative number modulo positive: \  (-21.2% 3) ")//Return negative println (" positive modulus negative: \ (24.4%-7.2) ")//Return positive println (" positive modulo 0.0:\ (4% 0.0) ")//Return Nan println (" negative modulus 0.0:\ (-2% 0.0)//Return nan println ("0 pairs of positive numbers modulo: \ (0 2)")//Return 0.0 println ("0 to 0.0 modulo \ (0% 0.0)")//Return nan//self-add and decrement: 1. Put the + + on the left side, first add one to the operand, and then put it in the expression In//2. Put + + on the right, first put the operand in the expression, and then add the operand to a var a2 = 5 var D2 = 1 var b2 = a2++ + 1 var c2 = ++d2 + 1 println ("A2 is: \ (a 2), B2 is: \ (b2), C2 is: \ (c2) ") var a3 = 4.2 println (" A of 5 Parties: \ (POW (a3,5)) ") println (" A's square root: \ (sqrt (A3)) ") println (" The random number between 0~10 " : \ (arc4random ()%) ") PRIntln (The Sin function value of "1.2: \ (sin (1.2))")//mark:-overflow operator//swift does not allow integer variables or constants to be assigned a value that exceeds the range of its number var f:int16 = 32767//f = f + 1//Super //&+, &-, &*, &/, &% f = f &+ 1//value overflow f = f &-100//value underflow let G = ten let k = g &/0/ /Get 0 Let q = g &% 0//Get 0//mark:-bitwise operator//& bitwise AND, | bitwise OR, ^ bitwise XOR, ~ bitwise negation, << left shift operator, >> right shift operator//mark:-range operation //1. The closed range operator a...b is used to define a range from a~b (including A, b boundary) for all values, a cannot be greater than b var ranges = 2...6 for range in ranges {println (range)}//2. Half Open Range Operator: A.. < is used to define a range of all values from a~b (containing a, but not including B), for iterating over an array of appropriate var books = ["Swift", "Java", "OC", "PHP"] for index in 0..<books.count {pri  Ntln ("index+1 language is: \ (Books[index])"}//mark:-comparison operator//> >= < <= = = = = = =!== var K1 =  Nsmutablearray () var K2 = Nsmutablearray ()//Determines whether the contents of two objects are equal, outputs true println (k1 = = K2)//Determines whether two objects point to the same object, outputs false println (K1 = = = K2)//mark:-Logical operator//&& and, & not shorted with, | | Or, | Not short circuit OR,! Non, ^ xor//mark:-Three mesh operation var G1 = 2 var g2 = 4 var g3 = g1 > g2? "G1 Greater than G2" : "G1 is not much more than G2" println (G3)//mark:-Nil merge operator//a?? The function of B is to determine if the optional type variable A is nil, and if it is not nil, the actual value wrapped by the optional variable A is returned, otherwise the type of the default value B,b is the same as a let DefaultName = "Cao"//defines an optional type variable, and is not an empty var name1: String?
= "Liu Bei" println (name1?? defaultname)//define an optional type variable, and an empty var name2:string? println (name2? defaultname)//actually a?? b equals a! = nil? A!
 : b println ("Hello, world!")

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.