DEV Community

Lou Franco
Lou Franco

Posted on • Updated on • Originally published at app-o-mat.com

The Swift Programming Language Companion: Basic Operators

This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple.

Read each article after you have read the corresponding chapter in the book. This article is a companion to Basic Operators.

Set up a reading environment

If you are jumping around these articles, make sure you read the Introduction to see my recommendation for setting up a reading environment.

Exercises for Basic Operators

At this point, you should have read Basic Operators in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.

In your Playground write code to do the following

  1. Declare three Ints named x, y, and z and give them initial values
  2. Declare another Int that is the sum of x, y, and z
  3. Declare two Doubles named a and b
  4. Declare a Double named c that is a divided by b
  5. Declare an Int named mod that is the remainder of x divided by y
  6. Declare a Bool that is true is x is less than y
  7. Declare a Bool that is true is x is less than y and y is greater than z
  8. Declare a Bool that is true is x is less than y or y is greater than z
  9. Declare a String set to "I have x cats" where x is x and "cats" is properly pluralized (use + to concatenate strings and ? : to check x)
  10. Declare an optional integer (Int?) named xNil and set it to nil. Then declare an Int named xOr5 where you use the nil coalescing operator (??) to set it to either xNil or 5. See what happens when you assign an actual integer to xNil.
  11. Print the numbers from 1 to 5 using a Range (...).
  12. Print the numbers from 0 to [1, 2, 3].count using a Range (..<).
  13. Print the values of [1, 2, 3, 4, 5] starting from the 2nd value (2) using a one-sided range.

Next: Strings and Characters

The next article will provide exercises for the Strings and Characters chapter.

Top comments (0)