Key Concepts

Review core concepts you need to learn to master this subject

Variables

var score = 0

A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data.

Variables are denoted by a name.

Constants

var score = 0

Constants refer to fixed values that a program may not alter during its execution. One can be declared by using the let keyword.

Arithmetic Operators

var score = 0

Swift supports arithmetic operators for:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % remainder

Types

var score = 0

Type annotation can be used during declaration.

The basic data types are:

  • Int: integer numbers
  • Double: floating-point numbers
  • String: a sequence of characters
  • Bool: truth values

String Interpolation

var score = 0

String interpolation can be used to construct a String from a mix of variables, constants, and others by including their values inside a string literal.

Compound Assignment Operators

var score = 0

Compound assignment operators provide a shorthand method for updating the value of a variable:

  • += add and assign the sum
  • -= subtract and assign the difference
  • *= multiply and assign the product
  • /= divide and assign the quotient
  • %= divide and assign the remainder
Variables
Lesson 1 of 1
  1. 1
    The “Hello, world!” program simply writes to the screen. It does not read anything, calculate anything, or allow for user input. That’s no fun! Real programs tend to produce results based on some …
  2. 2
    Suppose we are building a game, and we want to keep track of the player’s name, the player’s score that goes from 0 to 10, the player’s level, etc. We need some variables! Before we can use a var…
  3. 3
    Computers are incredible at doing calculations. Now that we have declared variables, let’s use them with arithmetic operators to calculate things! Here are some arithmetic operators: - + additi…
  4. 4
    Sometimes the value of our variables need to be adjusted. We know that we can update our variables using arithmetic operators like this: var age = 99 age = age + 1 print(age) // Prints 100 We a…
  5. 5
    We can provide a type annotation when we declare a variable to be clear about the kind of value that the variable can store. Some of the basic data types: - Int: integer numbers - Double: float…
  6. 6
    Often times, we find ourselves needing to insert values into a string. In these cases, we can use string interpolation. In Swift, to insert a value into a string using string interpolation, we …
  7. 7
    We’ve manipulated variables in the last few exercises, but what about variables with values we don’t want to change? That’s the job for constants. Whereas variables can be reassigned during thei…
  8. 8
    Now that we’ve learned about the basics of variables and arithmetic operators, let’s write a program! The mad scientist Kelvin has mastered predicting the weather in his mountain-side meteorology …
  9. 9
    You made it to the end of the lesson! High five. 🙌 Here is a review of the lesson: - A variable represents a particular piece of your computer’s memory that has been set aside for you to use to s…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo