Difference between revisions of "W1036 Data Types"

From Coder Merlin
(Replaced content with "{{MovedToMoodle|CS-151, Unit III, Chapter 1}}")
Tag: Replaced
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Whenever you save data to a variable, the computer needs to know what exactly the variable is storing (i.e. data types). This is important because a computer's memory is just a lot of ones and zeros; without knowing the type of data a variable holds then the computer has no way of knowing if '01000001' is representing the letter 'A' in ASCII (a character encoding) or the integer 65. Fortunately, higher-level programming languages abstract much of the behind-the-scenes memory stuff, so all you need to worry about as a programmer is using the appropriate data type for a variable.
{{MovedToMoodle|CS-151, Unit III, Chapter 1}}
 
Data types will also come in handy when creating and using functions, but we'll get to that later.
 
== String ==
A string is a ''string'' of characters. In fact, in the programming language C, there isn't a built-in type for strings; you instead have to use an actual array of characters. In Swift, you can manually set a variable to the type string by using the type name <syntaxhighlight lang="swift" inline>String</syntaxhighlight>. Swift can also automatically guess the type in most circumstances.
 
<syntaxhighlight lang="swift">
var myStr : String = "This is a string" // myStr is of type String
var anotherStr = "This is another string" // anotherStr is automatically assigned the type of String
</syntaxhighlight>
 
== Numbers ==
Although Swift has quite a few different types for numbers, the main focus will be on integers (which store whole numbers) and the '''Double''' type, which is used to store decimals.
 
=== Integers ===
Integers, denoted by the type name '''Int''', are used to store both positive and negative integers. Any math operations between two integers will not automatically result in any decimal places, and instead remove everything that would have been after the decimal place. For example, dividing the integer 10 by 3 would return 3 instead of 3.3.
 
<syntaxhighlight lang="swift">
var myNumber : Int = 150 // myNumber is of type Int
var anotherNumber = 112 // anotherNumber is also of type Int
var divResult = 10 / 3 // divResult is an Int with a value of 3
</syntaxhighlight>
 
=== Doubles ===
Doubles are the type we'll use to store decimals in Swift.
 
It is worth noting that there's also a '''Float''' type that can store decimals with less precision. The advantage is that this uses less memory, but that's usually not a concern on modern computers, which usually have gigabytes of RAM (a Double takes up 64 bits, or 8 bytes; in other words, a Double uses 8e-7 percent of a single gigabyte).
 
<syntaxhighlight lang="swift">
var myDouble : Double = 150 // myNumber is of type Double
var anotherNumber = 120.0 // anotherNumber is also of type Double
var divResult = 10.0 / 3 // divResult is a Double with a value of 3.3333333333333335
</syntaxhighlight>
 
Notice that by adding a decimal point, Swift will automatically assume a variable is a Double. When doing calculations, as long as any of the numbers involved have a decimal place, the result will also have decimal places.
 
==== Precision ====
To put it simply, Doubles aren't exact. For most use cases, this will be fine, but you may run into trouble if you need a large degree of precision. For example:
 
<syntaxhighlight lang="swift">
var divResult = 3.33 // divResult is a Double with a value of 3.3300000000000001
</syntaxhighlight>
 
== Boolean ==
The last data type you'll need to know about for the next few exercises is '''Bool''', short for boolean. This data type holds either '''true''' or '''false''', '''1''' or '''0''', or '''on''' or '''off''' depending on who you ask.
 
<syntaxhighlight lang="swift">
let myBool = true
let myOtherBool = false
</syntaxhighlight>

Latest revision as of 20:17, 8 February 2024

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
HintIcon.png
Moved to Moodle logo.svg
CS-151, Unit III, Chapter 1