Created
September 6, 2014 00:26
-
-
Save ronaldsmartin/f8e16470d0151bf6090f to your computer and use it in GitHub Desktop.
Teaching programming to Daniela using Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Learning Swift with Daniela #1 | |
/* | |
* Lesson Plan | |
* =========== | |
1. Variables & Constants | |
2. Types | |
3. Operators | |
4. Condtionals & Branching | |
*/ | |
// Print a String to the console | |
println("Hello, world!") | |
// A String is a string (or sequence) of characters (letters) denoted by quotation marks | |
// This is a constant. It cannot be changed. | |
// It is useful for referring to a value multiple times. | |
let constantHello = "Hello" // "String" | |
println(constantHello) | |
println(constantHello) | |
// This is a variable. | |
// It is like a constant, but it can be altered. | |
var changeableHello = "Hello is changable" | |
println(changeableHello) | |
changeableHello = "Hello, Daniela!" | |
println(changeableHello) | |
// You can declare multiple variables/constants on one line by separating them with commas. | |
var pi = 3.14, tau = 2.28 | |
// Swift's *type inference* lets it figure out what type of data is written | |
var x = 5 // int (Integer) | |
// Type Annotation! | |
// This lets us declare a variable or constant's type explicitly. | |
// In Java: String myString = "Uh..." | |
// Type annotation in Swift | |
var myString: String = "Uh..." | |
var myInteger: Int = 5 | |
var myFloat: Float = 5.1 | |
var myBoolean: Bool = true | |
// Basic Types: | |
// String -> sequence of Characters | |
// Character -> letter | |
// Int -> whole number | |
// Float -> floating-point number (decimal) | |
// Double -> float with higher precision | |
// Boolean -> true or false | |
let sum = 1 + 1 | |
let decimalSum = 1.0 + 1.0 | |
// "camelCase" makes identifies more legible | |
// writeABunchOfWords <- Easier to read | |
// writeabunchofwords | |
let daniela = "Daniela" | |
let ronald = "Ronald" | |
let smushedName = daniela + ronald | |
// Binary operators take two arguments | |
println(daniela + ronald) | |
println(2 * 3) | |
4 / 2 | |
1 - 1 | |
2 % 2 | |
// Unary (one argument) operator | |
// negative sign (-) is the unary operator | |
var negativeOne = -1 | |
var positiveOne = -negativeOne | |
let isDoneWithService = true | |
// Ternary operator takes three arguments | |
// Format: | |
// condition ? valueIfConditionIsTrue : valueIfConditionIsFalse | |
var danielaStatus = isDoneWithService ? "I'm done!" : "I'm not done yet." | |
// The above has the same meaning as: | |
if isDoneWithService { | |
danielaStatus = "I'm done" | |
} else { | |
danielaStatus = "I'm not done yet." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment