In this series of posts, I’m going to explain much of Apple’s new Swift programming language. It assumes you have no earlier programming knowledge or need to brush up on a few concepts to get things working.
Since the Publication of the Book The C Programming Language by Kernigan and Ritchie many decades ago, everyone starts a tutorial with the program Hello World, which prints the phrase Hello World to the console. In C, that took a little work, and so was valuable. But
println("Hello World") is so boring I thought I’d start with something a bit bigger and dissect it:
/* ------------------- Our first program /*Created 2014 June 06*/ by Steven J. Lipton ---------------------- */ //constant declarations let pi = 3.1415926 let z = 5.0 let area = pi * z * z //formula as constant //printing println("We like Pizza! ") print("The area of a pizza ") print("Can be calculated by the formula ") print("pi * z * z = a\n") println(" A \(z * 2) inch Pizza has an area of \(area) square inches")
If we were to run this, it would produce the following output on the console:
We like Pizza! The area of a pizza Can be calculated by the formula pi * z * z = a A 10.0 inch Pizza has an area of 78.539815 square inches
Let’s look at this piece by piece and see what is happening.
Comments are code that the computer ignores. It is a way to document code within the code. When we need to tell people who might read this code what it does, we document with comments. However there is another use of comments which Swift does remarkably well.
Lines 1-6 is a multiple line comment. Multiple line comments start with the characters
/* and end with the characters
*/ They can be several lines long, as in this case. Many programming languages like C and Java have this feature. Longer comments and identifying information use multiple line comments. Swift takes multiple line comments one step further with a feature many developers will love: nested comments. In many computer languages line 3 would cause an error, but Swift will not complain about this. Developers use multiple line comments to comment out parts of code they would otherwise delete. Commenting keeps the code handy if the deletion is a mistake. Commented out code also documents there was a change to the code. Nested comments permit to comment out already commented code.
Line 7 is a singe line comment. The character sequence
// marks the beginning of a comment. The end of the current line marks the end of the comment. Like line 7 and 12, single line comments are an entire line of code. Single line comments can appear at the end a line of code, such as line 10.
Frequently, we need a value for something, like the size of a pizza. We can say we have a 10-inch diameter pizza. That would have a slice size of 5.0 inches, the radius of the pizza. We might make calculations with this number several times in our application, but it does not change. We might however tweak the application at some point to change to a 8-inch radius. Instead of changing every 5 to a 8, it is easier to change the value in one place and use a symbol for radius. We call such a symbol a constant. In our code lines 8 and 9, we declare a constant using the word
let, followed by the word that will represent the constant, then an equals sign, then a value. In our example line 8, we declare a constant
pi, which will stand for our value of pi, used for computing things with circles. We also declare
z as a constant in line 9 which is the radius of the pizza. In lines 11 and 22, we use
z to compute values. Instead of changing the values, we can just change
z in line 9.
Output to the Console
While Swift is still in beta, we cannot send our results to a user interface. The
print(). The most common type is
println like line 13 above telling the world
println("We like Pizza! ") . The
println() function will take the part in between quotes, called a string and print it to the console. After
println prints to the console, it moves to print the next string on the beginning of the next line, referred to as a carriage-return or a newline.
print() function in line 14 through 16 does exactly the same thing as
println, but without the newline. The next
println prints directly after the last thing printed. The output for each of these is on the same line, not the next line like
println in line 13. Line 16 is slightly different. At the end of the string is the characters
\n Sequences starting with the backslash character
\are escape sequences. Escape sequences allow printing of things we otherwise could not print in a string. One of the most important of the escape sequences is
\n, which is the newline character. It tells
print(" \n") and
println(" ") are identical in function. As we get more sophisticated we will find situations where
\n becomes very handy to use instead of
Line 17 has a very important use of the escape sequence. Strings do not do calculations or accept numbers: they are just sequences of characters. What if, like line 17, we want to find the diameter and area of a pizza and print it out? Swift has string interpolation, which inserts numbers into a string. Place the number in parentheses after the backslash and swift will make it part of the string. In line 17, we add the constant
area and the calculation
z*2 to the string, which prints out with the correct numbers.
Constants cannot change once we set them with the
let keyword. What if we want to change the value after we set it the first time? For that we need a variable, and that will be our topic next time.