Mini-exercises

  1. Create a variable called range and set it equal to a range starting at 1 and ending with 10 inclusive. Write a for loop which iterates over this range and

prints the square of each number.

  1. Write a for loop which iterates over the same range as in the exercise above and prints the square root of each number. Hint: you will need to type convert your loop variable.
  2. Above, you saw a for loop which iterated over only the even rows like so:

var sum = 0

for row in 0..<8 { if row % 2 == 0 {

continue

}

for column in 0..<8 { sum += row * column

}

}

Change this to use a where clause on the first for loop to skip even rows instead of using continue. Check that the sum is 448 as in the initial example.

results matching ""

    No results matching ""