Swift Variables in Real-Life

Swift Variables in Real-Life

Use variables to track values like counters, flags, and user input.


Example: Counter

Use a variable to track a counter and print it as it changes.

Example

var count = 0
count += 1
print("Count: \(count)")


Example: Greeting

Store a value in a variable and use interpolation to print it.

Example

var name = "Kai"
print("Hello, \(name)!")
name = "Elisabeth"
print("Hello, \(name)!")

This example updates a variable and prints the new greeting.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *