0
0
Swiftprogramming~10 mins

Int, Double, Float number types in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Int, Double, Float number types
Start
Declare Int variable
Declare Double variable
Declare Float variable
Use variables in calculations
Print results
End
This flow shows declaring Int, Double, and Float variables, using them, and printing their values.
Execution Sample
Swift
let age: Int = 30
let price: Double = 19.99
let temperature: Float = 36.6
print(age)
print(price)
print(temperature)
This code declares three variables of different number types and prints their values.
Execution Table
StepActionVariableValueOutput
1Declare Int variable 'age'age30
2Declare Double variable 'price'price19.99
3Declare Float variable 'temperature'temperature36.6
4Print 'age'age3030
5Print 'price'price19.9919.99
6Print 'temperature'temperature36.636.6
💡 All variables declared and printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
ageundefined30303030
priceundefinedundefined19.9919.9919.99
temperatureundefinedundefinedundefined36.636.6
Key Moments - 2 Insights
Why do we use Int for 'age' but Double for 'price'?
Because 'age' is a whole number without decimals, Int is perfect. 'price' can have decimals, so Double is used. See steps 1 and 2 in execution_table.
Why is 'temperature' declared as Float and not Double?
Float uses less memory and is enough for 'temperature' precision here. Double is more precise but uses more space. See step 3 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'price' after step 2?
A19.99
B30
C36.6
Dundefined
💡 Hint
Check the 'price' value in row with step 2 in execution_table.
At which step is the 'temperature' variable declared?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the action 'Declare Float variable' in execution_table.
If we change 'age' to 30.5, what will happen?
AIt will convert to Double silently.
BIt will store 30.5 as Int automatically.
CIt will cause a type error because Int cannot hold decimals.
DIt will store 30.5 as Float.
💡 Hint
Recall that Int only holds whole numbers, see step 1 in execution_table.
Concept Snapshot
Int, Double, Float in Swift:
- Int: whole numbers, no decimals
- Double: decimal numbers, high precision
- Float: decimal numbers, less precision, uses less memory
Use Int for counts, Double for money, Float for less precise decimals
Declare with let/var and type annotation
Full Transcript
This visual execution shows how Swift handles three number types: Int, Double, and Float. First, an Int variable 'age' is declared with value 30, suitable for whole numbers. Next, a Double variable 'price' is declared with 19.99, allowing decimals with high precision. Then, a Float variable 'temperature' is declared with 36.6, which uses less memory but less precision. Each variable is printed, showing their stored values. Key points include choosing Int for whole numbers, Double for precise decimals, and Float for less precise decimals. Trying to assign a decimal to Int causes an error. This helps beginners understand number types and their uses in Swift.