Upload
others
View
12
Download
0
Embed Size (px)
Citation preview
Introduction to Programming
Swift Playgrounds
1
System Requirements⚫ iPad Version: 2nd Gen or higher (1st generation iPads will not
work!)⚫ Swift Playgrounds Application from Apple App Store.
Setup⚫ Open the Swift Playgrounds application and download the
modules named “Learn to Code 1” and “Learn to Code 2”. ⚫ Upgrade them to the latest version if you see the option to.
2
Error Messages/Troubleshooting⚫ If you are stuck on any part of the lesson, you can feel free
to email [email protected] for help (mention “IBM Family Science Swift Programming” in the email subject). Any questions about the lesson are welcome. If you are stuck on a coding section, please provide a short description of the problem and attach a screenshot of your code.
3
Instructions to reset a lesson
4
(1) Select lesson
(2) Select …
(3) Select Reset Page
Introduction⚫ What is programming or coding?
5
Introduction⚫ Coding
⚫ Developers
⚫ A command
⚫ Sequence
6
Introduction⚫ Coding is telling a computer what to do
⚫ Developers write code to build their apps and games
⚫ A command is a specific action for the computer to perform
⚫ Sequence is the order in which the commands are given
7
Swift Playgrounds⚫ A Swift Playground is an app on iPad that allows a user to
write programs with Swift code.
8
Alternatives to Swift⚫ Scratch -- https://scratch.mit.edu/
⚫ Grasshopper -- https://grasshopper.codes/
⚫ CodeMonkey -- https://www.codemonkey.com/
⚫ GDevelop -- https://editor.gdevelop-app.com/
9
Quick Overview of Swift Playgrounds
10
Code
Commands
Pull up the Keyboard
Quick Overview of Swift Playgrounds
11
Code
Commands
Keyboard
Exercise 1/6: Warming up⚫ Open Learn to Code 1⚫ Issuing Commands section⚫ Goal is to collect the gem
⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
12
Exercise 1: Issuing Commands
13
Exercise 1/6: Solution
14
Think Like a Detective⚫ Bug
⚫ Debugging
15
Think Like a Detective⚫ A bug is an error your code
⚫ Debugging is the process of identifying and fixing the error
⚫ During programming, you will come across situation when your program does not work as you planned.⚫ Be patient⚫ Think about the problem
16
Functions⚫ Can you name some daily routines that you do that have
several steps?
17Brush teeth
Make sandwich
Functions⚫ A Function is a collection of commands grouped together
and given a name. The set of commands can then be run with just the name of the function whenever that set is needed⚫ MakeSandwich()
⚫ A parameter is extra information that gets passed to a function⚫ MakeSandwich(peanut butter, jelly, white bread)⚫ MakeSandwich(lettuce, tomato, wheat bread)
18
Exercise 2/6: Functions⚫ Open Learn to Code 1⚫ Collect, Toggle and Repeat⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
19
Exercise 2/6: Functions
20
Exercise 2: Solution
21
For Loop⚫ A for loop runs a block of code over and over for a set of
number of times⚫ If you need to make many sandwiches for your guests ⚫ MakeSandwich()⚫ MakeSandwich()⚫ MakeSandwich()⚫ MakeSandwich()⚫ ….⚫ MakeSandwich()
22
⚫ For number of guests in the party⚫ MakeSandwich()
Exercise 3/6: Loops⚫ Open Learn to Code 1⚫ To the Edge and Back⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
23
Exercise 3/6: Loops
24
Exercise 3: Solution
25
Conditional Code⚫ A condition is something you test that results in true or
false
⚫ If raining then ……
⚫ If trafficLighIsGreen then …..
⚫ If handsAreDirty then ….
26
Conditional Code⚫ Conditional code is a block of code that will run on if
something is true
⚫ If trafficLightIsGreen { Car drives forward across the intersection} else { Wait()}
27
Conditional Code⚫ A boolean is a value that can be either true or false⚫ A logical operator is a symbol or words like “and”, “or” and
“not” that connects two or more Booleans to make conditions decisions more specific.
⚫ AND (&&) – Results in true only if two statements are true;⚫ If (daylight) AND (not raining) then play outside
⚫ OR (||) – Results in true if one or both statements are true⚫ If (thirsty) OR (hungry) then goto McDonalds
⚫ NOT (!) – Results in true if the value is false or vice-versa⚫ If (NOT tired) then play outside
28
While Loop⚫ A while loop is a loop that runs a block of code as long as
a condition is true.
⚫ When the condition is false, the loop stops running
⚫ While (iceCreamIsSoft) { keep in the freezer}Eat Ice Cream
29
Exercise 4/6: While Loops⚫ Open Learn to Code 1⚫ The Running Code While⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
30
Exercise 4/6: While Loops
31
Exercise 4/6: Solution
32
Algorithms⚫ An algorithm is a step-by-step set of rules or instructions⚫ e.g. Navigation app uses an algorithm to figure out the
fastest path to where you want to go⚫ Compare distances and average speeds⚫ Compare current traffic conditions⚫ Select route with the shortest
⚫ Pseudocode is an information description of code or a concept that’s intended for human reading.
33
Exercise 5/6: Algorithms⚫ Open Learn to Code 1⚫ The Right-Hand Rule⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
34
Exercise 5/6: Algorithms
35
Exercise 5/6: Solution
36
Variables⚫ A variable is a named container that stores a value.
The value can change over time.
⚫ A box has both a label and contents⚫ The contents can change⚫ The box’s content can be found by
finding the box with the correct label
⚫ A variable is similar to a box
37
Exercise 6/6: Variables⚫ Open Learn to Code 2⚫ Seeking seven gems⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
38
Exercise 6/6: Solution
39
Wrap up⚫ Introduction to
programming⚫ Concept Definition⚫ Issuing commands in
Swift Playground⚫ Writing functions
⚫ For Loops⚫ Condition code⚫ While Loops⚫ Algorithms⚫ Variables
40
Things to try at home⚫ Arrays⚫ Build 3-d worlds
41
Arrays⚫ An array is a collection that stores an ordered list of items⚫ An index is the number that represents the position of an
item in an array⚫ Imagine you are making an ice cream sundae
42
Arrays⚫ Defining each ingredient as a constant is really repetitive.
Instead, use an array to store a list of items in a certain order.
43
Arrays⚫ You can access and modify an element in the array by
using an index
44
Exercise 7: Arrays⚫ Open Learn to Code 2⚫ Stacking Blocks⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
45
Exercise 7: Arrays
46
Exercise 7: Solution
47
Final Project: World Creation⚫ We will create a world using the coding concepts that we
just learned in the class.
48
Exercise 8: World Creation⚫ Open Learn to Code 2⚫ World Creation⚫ Select Tap to code⚫ Pick the appropriate command from the bottom⚫ TYPE Run My Code on the right
49
Exercise 8: World Creation
50
Exercise 8: One solution
51
Backup
52
Recommended time for each topic including exercises⚫ Introduction – 10⚫ Issuing commands in
Swift Playground - 15⚫ Think Like a detective - 5⚫ Writing functions – 15 ⚫ For Loops – 15⚫ Condition code – 10
⚫ While Loops - 15⚫ Algorithms - 15⚫ Variables – 10⚫ Wrap up - 10
TOTAL class time = 2 hr 53