Detailed explanation of constant and variable _swift in Swift programming

Source: Internet
Author: User
Tags character set lowercase


Constant
A constant refers to a fixed value that a program cannot change during its execution. Constants can be any basic data type such as Integer constants, floating-point constants, word constants, or strings. Can also be an enumeration constant.



These constants are treated like regular variables, except that their values cannot be modified after they are defined.



Declaring constants
When using constants, you must declare them by using the keyword let declaration as follows:

 code as follows:


Let Constantname = <initial value>

Here is a simple example of how to declare a constant in Swift:
code as follows:

Import Cocoa


Let Consta = 44
println (Consta)


When we run the above program in playground, we get the following results.







 code as follows:


44







Type annotation
When declaring a constant, you can provide a type annotation to specify which constant value type to store. The syntax is as follows:



var constantname:<data type> = <optional initial value>
Here is a simple example of how to use annotations to declare a constant in Swift. It is important to note here that it is mandatory to provide an initial value when creating a constant:


 code as follows:

Import Cocoa


Let Consta = 42
println (Consta)

Let constb:float = 3.14159

println (CONSTB)


When we run the above program in playground, we get the following results.




3.1415901184082


Named constants
The name of a constant can consist of letters, numbers, and underscores. It must be marked with letters or underscores. Uppercase and lowercase letters are different, because Swift is a case-sensitive programming language.



You can use simple or Unicode characters to name variables. The following are valid examples:


 code as follows:

Import Cocoa


Let _const = "Hello, swift!"
println (_const)

Let variable = "Hello World"
println (variable)



When we run the above program in playground, here we print a variable name in Chinese: "Variable", we get the following results.




Hello, swift!
. Hello World


Printing constants
You can use the PRINTLN function to print the current value of a constant or variable. You can insert a variable value by wrapping the name in parentheses and using a backslash in front of the left parenthesis: The following are valid examples:


 code as follows:

Import Cocoa


Let Consta = "Valuea"
Let Constb = 1101.00

println ("Value of" (Consta) is more than \ (CONSTB) millions ")


When we run the above program in playground, we get the following results.




Value of Valuea is more than 1101.0 millions


Variable
variable gives us a program that can manipulate the stored name. Each variable in Swift has a specific type, which determines the size of the variable storage; The range of values stored within the memory, and the action group can be applied to variables.



The following are the basic types of Swift variables


    • Int or UInt-this is used for integers. More specifically, you can use Int32,int64 to define a 32-or 64-bit signed integer that is used as a variable for UInt32 or UInt64 to define a 32 or 64-bit unsigned integer. For example, and-23.
    • Float-this is used to represent a 32-bit floating-point number, which is typically used to use smaller decimal points. For example: 3.14159, 0.1, and-273.158.
    • Double-This is used to represent a 64-bit floating-point number for very large floating-point values. For example: 3.14159, 0.1, and-273.158.
    • Bool-This represents a Boolean value, True or false.
    • String-This is an ordered character set combination. For example, "Hello, world!"
    • Character-This is a single character string. For example, "C"
    • Optional-This represents a variable that can hold a value or have no value.


Swift also allows you to define other types of variables, and we will later learn about types like Optional, Array, dictionaries, structures, and Classes.



The following sections describe how to declare and use a variety of different types of variables in Swift programming.



Variable declaration
A variable declaration means telling the compiler how many variable storage space to create. Before you use variables, you must declare them using the VAR keyword, as follows:


 code as follows:

var variableName = <initial value>

Here is a simple example of how to declare a variable in Swift:
code as follows:

Import Cocoa


var VarA = 42
println (VarA)


When we use playground to run the above program, we get the following results.




42


Type annotation
When declaring a variable, you can provide a type annotation to make it clear that the value of that variable can be stored. Here's the syntax:


 code as follows:

var variablename:<data type> = <optional initial value>

Here is a simple example of how Swift uses annotations to declare a variable. Note here that if you do not use a type annotation, it becomes the type of the initial value of the coercion variable, otherwise you declare the type of the variable using the type annotation.
 code as follows:

Import Cocoa


var VarA = 42
println (VarA)

var varb:float

Varb = 3.14159
println (Varb)





When we use playground to run the above program, we get the following results.


3.1415901184082





Named variables
A variable name can consist of letters, numbers, and underscores. It must be marked with letters or underscores. Variables of uppercase and lowercase letters are different, because Swift is a case-sensitive programming language.



You can use simple or Unicode characters to name variables. The following are valid examples:


 code as follows:

Import Cocoa


var _var = "Hello, swift!"
println (_var)

var variable name = "Hello World"
println (variable name)


When we use playground to run the above program, we get the following results.
Hello, swift!.
Hello World
Print variables
Use the PRINTLN function to print the current value of a constant or variable. This is a valid example by encapsulating the name in parentheses and escaping the interpolation of a variable value with a backslash before the opening parenthesis:
 code as follows:

Import Cocoa


var VarA = "Valuea"
var Varb = 1008.00

println ("Value of" (VarA) is more than \ (Varb) millions ")


When we use playground to run the above program, we get the following results.




Value of Valuea is more than 1008.0 millions




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.