Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

Swift - Constants, Variables & Print function

In Swift, constants and variables associate a name with a value of a particular type. The value of a constant cannot be changed after it's set, whereas a variable can be set to a different value in the future.

Let's dive into constants, variables, and the print function in Swift:

1. Constants:

Constants are declared using the let keyword. Once set, the value of a constant cannot be changed.

let pi = 3.14159

2. Variables:

Variables are declared using the var keyword.

var age = 25
age = 26  // This is valid because age is a variable

3. Type Annotations:

You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store:

var userName: String
userName = "Alice"

4. Multiple Assignments:

You can declare multiple constants or multiple variables on a single line, separated by commas:

let x = 10, y = 20, z = 30
var red, green, blue: Double

5. Print Function:

The print(_:separator:terminator:) function is a global function in Swift that writes one or more values to the standard output.

  • Basic Usage:

    print("Hello, World!")
    
  • Printing Variables and Constants:

    var greeting = "Hello"
    let name = "Alice"
    print("\(greeting), \(name)!")
    

    This uses string interpolation (\()) to insert constants or variables into a string.

  • Using separator and terminator:

    print("Red", "Green", "Blue", separator: " - ", terminator: "!")
    

    This will output: Red - Green - Blue!

Conclusion:

Swift offers a straightforward way to define constants and variables using the let and var keywords respectively. The language's static typing, combined with its type inference capabilities, makes it both powerful and user-friendly. The print function, meanwhile, is versatile and provides options to customize the output format, making debugging and logging easier.

  1. How to declare constants in Swift:

    • Description: Constants in Swift are declared using the let keyword. Once a value is assigned to a constant, it cannot be changed.

    • Code:

      let pi = 3.14159
      print(pi)
      
  2. Defining variables in Swift programming:

    • Description: Variables in Swift are declared using the var keyword. Unlike constants, variables can have their values changed after they are initially set.

    • Code:

      var age = 25
      age = 26 // Valid, as it's a variable
      print(age)
      
  3. Print statements with variables in Swift:

    • Description: Print statements in Swift can include variables for displaying their values.

    • Code:

      let name = "John"
      var score = 95
      
      print("Player: \(name), Score: \(score)")
      
  4. Concatenating strings in Swift print:

    • Description: Strings and variables can be concatenated in print statements in Swift.

    • Code:

      let firstName = "Alice"
      let lastName = "Smith"
      
      print("Full Name: " + firstName + " " + lastName)
      
  5. Formatting output with print in Swift:

    • Description: The print function in Swift allows formatting output using various options.

    • Code:

      let price = 29.99
      
      // Formatting with precision
      print(String(format: "Price: %.2f", price))
      
  6. Swift multiline print statements:

    • Description: Multiline print statements in Swift can be achieved by using line breaks (\n).

    • Code:

      let message = """
                     Hello,
                     This is a multiline
                     message in Swift.
                     """
      print(message)
      
  7. Using constants and variables in Swift examples:

    • Description: Constants and variables can be used in various combinations to create dynamic and flexible code.

    • Code:

      let maxAttempts = 3
      var currentAttempts = 0
      
      while currentAttempts < maxAttempts {
          print("Attempt \(currentAttempts + 1)")
          currentAttempts += 1
      }
      
  8. Swift type annotations for variables:

    • Description: Swift allows specifying the type of a variable using type annotations.

    • Code:

      var message: String = "Hello, Swift!"
      
      // Type annotation for an integer
      var count: Int = 10