Swift: Comments, Integers, Floating Point Numbers

Comments

Comments are used to make code more readable and understandable. They are ignored by the compiler, but they can be very helpful for other programmers who are trying to read your code.

To add a comment in Swift, use the // symbol. Any text that follows the // symbol on the same line will be ignored by the compiler.

For example, the following code shows how to add a comment to a Swift program:

// This is a comment.
print("Hello, world!")

Semicolons

Semicolons are used to separate statements in Swift. They are not required, but it is good practice to use them.

For example, the following code shows how to use semicolons to separate statements:

let hello ="Hello, world!"; print(hello)
// Prints Hello, world!

Integers

Integers are numbers that do not have a fractional part. They can be positive, negative, or zero.

Swift provides two types of integers: signed integers and unsigned integers. Signed integers can be positive, negative, or zero. Unsigned integers can only be positive or zero.

Integer Bounds

Every integer type in Swift has a range of values that it can represent. This range is called the integer bounds.

The integer bounds for the different integer types in Swift are as follows:
  • Int: -2147483648 to 2147483647
  • UInt: 0 to 4294967295
If you try to assign a value to an integer variable that is outside of the integer bounds, the compiler will generate an error.

Int

The Int type is the default integer type in Swift. It is a signed integer that can represent values from -2147483648 to 2147483647.

Example:
let number: Int = 10

UInt

The UInt type is an unsigned integer that can represent values from 0 to 4294967295.

Example:
let number: UInt = 10

Floating-Point Numbers

Floating-point numbers are numbers that can have a fractional part. They can be very large or very small.

Swift provides two types of floating-point numbers: doubles and floats. Doubles are more precise than floats, but they are also slower and take up more memory.

Example:
let doubleNumber: Double = 3.14159
let floatNumber: Float = 3.14159


Previous: Swift and its Introduction Next: Type Safety and Conversions

Comments

Popular posts from this blog

Introduction and get started with Swift

Swift: Providing a Fallback Value, Force Unwrapping, Implicitly Unwrapped Optionals