introduction to programming - ibm€¦ · fastest path to where you want to go ... ⚫introduction...

53
Introduction to Programming Swift Playgrounds 1

Upload: others

Post on 20-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Introduction to Programming

Swift Playgrounds

1

Page 2: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 3: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 4: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Instructions to reset a lesson

4

(1) Select lesson

(2) Select …

(3) Select Reset Page

Page 5: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Introduction⚫ What is programming or coding?

5

Page 6: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Introduction⚫ Coding

⚫ Developers

⚫ A command

⚫ Sequence

6

Page 7: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 8: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Swift Playgrounds⚫ A Swift Playground is an app on iPad that allows a user to

write programs with Swift code.

8

Page 9: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Alternatives to Swift⚫ Scratch -- https://scratch.mit.edu/

⚫ Grasshopper -- https://grasshopper.codes/

⚫ CodeMonkey -- https://www.codemonkey.com/

⚫ GDevelop -- https://editor.gdevelop-app.com/

9

Page 10: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Quick Overview of Swift Playgrounds

10

Code

Commands

Pull up the Keyboard

Page 11: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Quick Overview of Swift Playgrounds

11

Code

Commands

Keyboard

Page 12: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 13: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 1: Issuing Commands

13

Page 14: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 1/6: Solution

14

Page 15: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Think Like a Detective⚫ Bug

⚫ Debugging

15

Page 16: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 17: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Functions⚫ Can you name some daily routines that you do that have

several steps?

17Brush teeth

Make sandwich

Page 18: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 19: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 20: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 2/6: Functions

20

Page 21: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 2: Solution

21

Page 22: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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()

Page 23: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 24: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 3/6: Loops

24

Page 25: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 3: Solution

25

Page 26: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Conditional Code⚫ A condition is something you test that results in true or

false

⚫ If raining then ……

⚫ If trafficLighIsGreen then …..

⚫ If handsAreDirty then ….

26

Page 27: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 28: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 29: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 30: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 31: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 4/6: While Loops

31

Page 32: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 4/6: Solution

32

Page 33: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 34: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 35: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 5/6: Algorithms

35

Page 36: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 5/6: Solution

36

Page 37: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 38: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 39: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 6/6: Solution

39

Page 40: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Wrap up⚫ Introduction to

programming⚫ Concept Definition⚫ Issuing commands in

Swift Playground⚫ Writing functions

⚫ For Loops⚫ Condition code⚫ While Loops⚫ Algorithms⚫ Variables

40

Page 41: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Things to try at home⚫ Arrays⚫ Build 3-d worlds

41

Page 42: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 43: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 44: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Arrays⚫ You can access and modify an element in the array by

using an index

44

Page 45: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 46: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 7: Arrays

46

Page 47: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 7: Solution

47

Page 48: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Final Project: World Creation⚫ We will create a world using the coding concepts that we

just learned in the class.

48

Page 49: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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

Page 50: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 8: World Creation

50

Page 51: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Exercise 8: One solution

51

Page 52: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

Backup

52

Page 53: Introduction to Programming - IBM€¦ · fastest path to where you want to go ... ⚫Introduction to programming ⚫Concept Definition ⚫Issuing commands in Swift Playground ⚫Writing

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