ProgramGuru

Data types and safe conversion

Data types and safe conversion

A school report combines names, whole-number marks and averages. String represents text, Int whole numbers, Double floating-point numbers and Bool true or false. Types define which operations are valid, so a number written inside quotes is not automatically usable in arithmetic.

What you’ll learn

  • Convert integer totals before fractional division.
  • Handle a failed conversion from text without inventing a mark.

Data types and safe conversion in the example

  1. Typed inputs

    A school report combines names, whole-number marks and averages.

    Typed inputs
  2. Explicit conversion

    Swift does not silently combine Int and Double in every expression.

    Explicit conversion
  3. Numeric result

    The annotation names total’s type explicitly.

    Numeric result
The annotation names total’s type explicitly. Converting 155 to Double allows floating-point division by 2.0, giving 77.5. Parsing "80" succeeds, and the nil-coalescing operator displays that value rather than the fallback -1.

How data types and safe conversion works

Swift does not silently combine Int and Double in every expression. Convert deliberately with Double(value) when the meaning supports it. Converting arbitrary text with Int(text) can fail, so it returns an optional: either a parsed integer or no value. This is different from forcing every invalid input to zero.

Work through data types and safe conversion

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

let total: Int = 155
let average = Double(total) / 2.0
let parsed = Int("80")
print(average)
print(parsed ?? -1)

Expected output

77.5
80

Follow the execution

The annotation names total’s type explicitly. Converting 155 to Double allows floating-point division by 2.0, giving 77.5. Parsing "80" succeeds, and the nil-coalescing operator displays that value rather than the fallback -1.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Data types and safe conversion

Your task: What would the second output become if the text were eighty?

Swift · complete program

let total: Int = 155
let average = Double(total) / 2.0
let parsed = Int("80")
print(average)
print(parsed ?? -1)

Worked answer

Parsing would fail and the fallback would print -1. The fallback is a demonstration policy, not a valid student score; a real form should report the invalid input rather than silently treating it as a mark.

Make conversion failure visible

Integer division drops the fractional part for these positive integer operands. Floating-point values approximate many decimal fractions, so exact equality can be unsuitable for computed measurements. Validate meaning and range as well as whether conversion succeeds.

Check your decision

What would the second output become if the text were eighty?
Choose one answer. You can try again.

Answer and explanation

Parsing would fail and the fallback would print -1.

Parsing would fail and the fallback would print -1. The fallback is a demonstration policy, not a valid student score; a real form should report the invalid input rather than silently treating it as a mark.

Make conversion failure visible

Parsing would fail and the fallback would print -1. The fallback is a demonstration policy, not a valid student score; a real form should report the invalid input rather than silently treating it as a mark. Integer division drops the fractional part for these positive integer operands.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Arithmetic, comparisons and Boolean logic →