Variables vs Constants: What You Need to Know

Variables vs Constants: What You Need to Know

What are Variables?

In programming, a variable is a value that can change, depending on conditions or on information passed to the program.

Variables are mutable.

What are Constants?

In programming, a constant is a value that cannot be changed, depending on conditions or information passed to the program.

Constants are immutable

Naming Variables and Constants

Constant and variable names can contain almost any character, including Unicode characters.

Constant and variable names can’t contain whitespace characters or mathematical symbols, Nor can they begin with a number, although numbers may be included elsewhere within the name.

Declaring and Initializing a Variable

var keyword declares a variable in swift.

// Declaring a Variable
var username

// Initializing a Variable
username = "Prahlad"
// Or we can declare and initialize in a single line
var username = "Prahlad"

Variables can be Reassigned

// First Declaration and Initialization
var username = "Prahlad"

// Reassigning to new value
username = "Prahlad Inala"

Declaring and Initializing a Constants

let keyword declares a Constants in swift.

// Declaring and Initializing a Constants
let accountId = 1234567

Constants cannot be Reassigned

// First Declaration and Initialization
let accountId = 1234567 

// Reassigning to new value
accountId = 12345

// ERROR: Cannot assign to value: 'accountId' is a 'let' contact. Change 'let' to 'var' to make it mutable

Data Types in Swift

S.NoData TypeCalled byExampleDescription
1.String: String"Prahlad Inala"represents textual data
2.Character: Character'P'a 16-bit Unicode character
3.Int: Int7an integer number
4.Boolean: BooltrueAny of two values: true or false
5.Float: Float13.04represents 32-bit floating-point number
6.Double: Double15.7007007012345represents 64-bit floating-point number

String Data Type

Sometimes we need to ask the user only to enter text (String), for this condition we can define datatype while declaring a variable.

var username: String = "Prahlad Inala"

username = 07

// ERROR: Cannot assign the value of type 'Int' to type 'String'

**Declare Multiple Variables of String: **

var name, email, address: String

// Not these name, email, and address variables are allowed to be Initiated with a String only.

Int Data Type

Sometimes we need to ask the user only to enter numbers (Int), for this condition we can define datatype while declaring a variable.

var numberOfLoginsInADay: Int = 34

numberOfLoginsInADay = "Prahlad Inala"

// ERROR: Cannot assign the value of type 'String' to type 'Int'

Int has a more precise Declaration:

  1. Int32

  2. Int64

  3. Int8

  4. Int16

**Declare Multiple Variables of Integer: **

var accountID, phonenumber, zipcode: Int

// Not these name, email, and address variables are allowed to be Initiated with a String only.

Character Data Type

var grade: Character = 'P'

grade = "Prahlad Inala"

// ERROR: Cannot assign the value of type 'String' to type 'Character'

Double Data Type

var numbers: Double = 15.7007007012345

Float Data Type

var pie: Float = 3.143

Boolean Data Type

var isUserAdmin: Bool= true

Comments in Swift

Use comments to include non-executable text in your code, as a note or reminder to yourself. Comments are ignored by the Swift compiler when your code is compiled.

Single Line Comments

Single-line comments begin with two forward slashes (//):

// This is a comment.

Multi Line Comments

Multiline comments start with a forward-slash followed by an asterisk (/) and end with an asterisk followed by a forward-slash (/):

/* This is also a comment
but is written over multiple lines. */

Follow for more

Linkedin: https://www.linkedin.com/in/prahladinala/
Github: https://github.com/prahladinala/
Instagram: https://instagram.com/prahlad.inala/
Twitter: https://twitter.com/prahladinala
Figma Community: https://www.figma.com/@prahladinala
Dribbble: https://dribbble.com/prahladinala
Behance: https://www.behance.net/prahladinala
Personal Portfolio: https://prahladinala.in
ToolMate: https://toolmate.co.in

Thank you!

Did you find this article valuable?

Support Prahlad Inala by becoming a sponsor. Any amount is appreciated!