Problem overview
The starter code declares two constants, width and height, holding 23 and 17. The task is to print the area of the rectangle they describe. The area of a rectangle is its width times its height, so the program must print the product of the two values.
The exercise is a first use of arithmetic on named values: instead of typing numbers into the expression, you use the constants that already exist.
Approach
Swift's multiplication operator is *. Because width and height are both whole numbers, Swift infers the type Int for each, and width * height is an Int as well. You don't need an extra variable for the area: print accepts any expression, evaluates it, and prints the result.
Step by step
- Leave the two constants alone. They are declared with
let, which is right: a rectangle's sides don't change while the program runs. - Write the expression
width * heighton the next line. The operands are the names of the constants, not their values typed again. - Wrap it in
print. The call writes the value of the expression to the console, followed by a line break.
The solution in Swift
let width = 23
let height = 17
print(width * height)
The program prints:
391
Swift details worth knowing here:
- Top-level code. A Swift file can contain statements outside any function, and they run from the first line down. No
mainis needed. - No semicolons. A line break ends a statement.
- Both sides must have the same type.
width * heightworks because both areInt. Multiplying anIntby aDoubledoesn't compile in Swift; one side needs an explicit conversion such asDouble(width). Rectangles with decimal sides needDoubleconstants for that reason. - Whitespace around operators. Write
width * heightwith spaces on both sides, or on neither. Swift treats an operator with space on only one side differently.
Decimal sides
If the sides can have decimals, declare them as Double and the same expression works. With let w2 = 4.5 and let h2 = 2.0, print(w2 * h2) prints 9.0. A Double always prints with a decimal point, even when the value is whole.
Mixing the two kinds is an error. With width as the Int 23 and h2 as a Double, width * h2 fails with binary operator '*' cannot be applied to operands of type 'Int' and 'Double'. Converting one side fixes it: Double(width) * h2 prints 46.0.
Storing the result
You can also keep the area in a constant, which helps when you use it more than once. With let area = width * height, the call print("The area is", area) prints The area is 391. For this exercise the extra constant isn't needed, and the task wants only the number.
Edge cases and common mistakes
- Adding instead of multiplying.
width + heightprints 40, which is the sum of the sides, not the area. - Typing the numbers.
print(23 * 17)prints the same result for this starter code, but it ignores the constants. If the values change, the answer breaks. - Printing a label as well.
print("Area: ", width * height)prints extra text. The task asks for the number alone. - Forgetting
print. A barewidth * heightat the top level is computed and thrown away, and Swift warns that the result is unused. Nothing appears in the console.
Key takeaways
*multiplies, and the area of a rectangle iswidth * height.printcan take an expression directly, so no intermediate variable is needed.- Swift does not mix
IntandDoublein one operation without a conversion.
