0% found this document useful (0 votes)
246 views8 pages

Exercise 1

The document contains code examples demonstrating various Swift concepts like printing, constants, variables, types, and type inference. It prints statements about what was learned, song lyrics using print, and explains why a constant cannot be changed but a variable can. It shows different variable types (int, double, string, boolean) and that values must match types or it won't compile. It demonstrates type inference and requiring an explicit type if the inferred type is ambiguous.

Uploaded by

Daniel Gil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
246 views8 pages

Exercise 1

The document contains code examples demonstrating various Swift concepts like printing, constants, variables, types, and type inference. It prints statements about what was learned, song lyrics using print, and explains why a constant cannot be changed but a variable can. It shows different variable types (int, double, string, boolean) and that values must match types or it won't compile. It demonstrates type inference and requiring an explicit type if the inferred type is ambiguous.

Uploaded by

Daniel Gil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

/*:

## Exercise 1 - Use Playgrounds


The code below prints a few short statements about what you have
learned in this lesson. Open the console area and view the code's
output.
*/
print("I have learned the following:")
print("What features make Swift a modern and safe language")
print("How to use the Swift REPL in Terminal")
print("How to use playgrounds to make writing Swift fun and simple")
/*:
Now print your own phrases to the console. Pick one of your favorite
songs. Use your knowledge of the `print` function to display the song
title and artist.
*/
fun main()
{
println("Hello, world!!!");
print("ACDC Highway to hell")
print("Toby keith, In america")
print("Owl city, fireflies")
}
/*:
Use multiple `print` functions to write out some of the lyrics to the
song.
*/
println("Hello, world!!!");
println("ACDC Highway to hell");
println("Living easy, living free")
println("Season ticket on a one-way ride")
println("Asking nothing, leave me be")
println("Taking everything in my stride")
println("Don't need reason, don't need rhyme")
/*:
## Exercise 2 - Constants
Declare a constant called `friends` to represent the number of friends
you have on social media. Give it a value between 50 and 1000. Print
out
the value by referencing your constant.
*/
fun main()
{
val friends = 132;
println(friends);
}

/*:
Now assume you go through and remove friends that aren't active on
social media. Attempt to update your `friends` constant to a lower
number than it currently is. Observe what happens and then move to the
next step.
*/

/*:
Does the above code compile? Why not? Print your explanation to the
console using the `print` function. Go back and delete your line of
code
that updates the `friends` constant to a lower number so that the
playground will compile properly.
*/

/*:
## Exercise 3 - Variables
Declare a variable `schooling` and set it to the number of years of
school that you have completed. Print `schooling` to the console.
*/

/*:
Now imagine you just completed an additional year of school, and
update
the `schooling` variable accordingly. Print `schooling` to the
console.
*/
/*:
Does the above code compile? Why is this different than trying to
update a constant? Print your explanation to the console using the
`print` function.
*/

/*:
## Exercise 4 - Types and Type Safety
Declare two variables, one called `firstDecimal` and one called
`secondDecimal`. Both should have decimal values. Look at both of
their types by holding Option and clicking on the variable name.
*/

/*:
Declare a variable called `trueOrFalse` and give it a boolean value.
Try to assign it to `firstDecimal` like so: `firstDecimal =
trueOrFalse`. Does it compile? Print a statement to the console
explaining why not, and remove the line of code that will not compile.
*/
/*:
Declare a variable and give it a string value. Then try to assign it
to
`firstDecimal`. Does it compile? Print a statement to the console
explaining why not, and remove the line of code that will not compile.
*/

/*:
Finally, declare a variable with a whole number value. Then try to
assign it to `firstDecimal`. Why won't this compile even though both
variables are numbers? Print a statement to the console explaining why
not, and remove the line of code that will not compile.
*/
/*:
## Exercise 5 - Type Inference and Required Values
Declare a variable called `name` of type `String`, but do not give it
a
value. Print `name` to the console. Does the code compile? Remove any
code that will not compile.
*/

/*:
Now assign a value to `name`, and print it to the console.
*/
/*:
Declare a variable called `distanceTraveled` and set it to 0. Do not
give it an explicit type.
*/

/*:
Now assign a value of 54.3 to `distanceTraveled`. Does the code
compile? Go back and set an explicit type on `distanceTraveled` so the
code will compile.
*/

You might also like