We’re going to practice using arrays. One of the first things we’ll do is iterate over items in an array; just like you would with items on a to do list, or a shopping list.
For this we’ll be using our for loops quite a bit and working within our repeating block.
We’ll also keep other things in our arrays, like characters, gems, and portals.
Arrays are yet another super useful tool in our coding toolbox. They have a ton of interesting functionality and features, and we’ll get to explore some of that today.
We’ve covered while loops the last few days, which are ways to repeat a certain block of code while a condition is true. We learned why this tool is handy, and when you might use it instead of a for loop.
understandsWhileLoops = true
Now we’re ready for the next tool.
Today: Arrays!
In Chapter 10, we talk about arrays.
An array is a list of items. In Swift, the items in the list will all have the same type. In our playgrounds, we might have an array of Characters or Ints (remember, these are whole numbers).
This is a heavily used tool in programming.
If you skipped ahead a little bit in the Rock, Paper, Scissors playground, you may have seen arrays in use and wondered what the square brackets (‘[‘ and ‘]’) were for, or some of the ways loops were used looked a little different than our for i in 1...n or while condition syntax.
We’ll learn all about the syntax of arrays; how to create them, change them, and use them.
We finished up logical operators yesterday with a little BuzzFeed-style quiz. Our code looked a little something like this:
show("What is your name?")
let name = ask("Name")
show("Hi " + name)
show("Answer some questions and I'll tell you what type of animal you are.")
show("Are you house-trained?")
var isHouseTrained = askForBool()
show("If I threw a ball would you chase it?")
var wouldChaseBall = askForBool()
//🦁🐱🐶🐺
if isHouseTrained && wouldChaseBall {
print("You're a 🐶!")
} else if !isHouseTrained && wouldChaseBall {
print("You're a 🐺!")
} else if isHouseTrained && !wouldChaseBall {
print("You're a 🐱!")
} else if !isHouseTrained && !wouldChaseBall {
print("You're a 🦁!")
}
Paste that into a Swift Playground on your iPad and try it out!
Today’s Session
Today we’re back with while loops.
They require a little more design thinking, and especially today’s playground, where we nest loops! This just means we’ll be running a loop inside of a loop.
Because this is a little bit of a leap, conceptually, we’re going to take our time and come up with some strategies for reading code that might help you. Not only will they make reading code someone else has written easier, they’ll make reading and explaining your own code later easier. And the tips will help you design your code better, before you even start writing a single line.
The second week we introduced loops and variables. We even started on conditional coding!
For the third week we continued with conditions, introduced types and initialization, and started functions with parameters.
Last week we dove deeper into functions with parameters and logical operators (which brought us back to conditional coding again). We dropped an extra video last week to try and help illustrate the usage of some of these concepts.
What’s up this week
This week I hope to start us off with a live debugging session. This is useful because it’s a scenario you’ll often find yourself in. You’ll have a chunk of code you need to walk through in order to hunt down a tricky bug or bad behavior.
After the debugging session we’re going to explore a great way to exercise our newfound skills with conditionals and logical operators. We’ll use The Code Hub’s version of the Answers playground, from our playgrounds feed.
We’ll quiz our friends and collect their answers in variables and then use conditional coding and logical operators to change what our answer to them is.
Later… more loops!
Later in the week we’ll continue with more ways to loop and then maybe squeak in the start of arrays: a handy way to store lists of things.
As with many of our sessions lately, yesterday’s was meaty. Logical operators are a whole new level in our programming toolbox.
We have the NOT operator, which is an exclamation mark (!) in front of a boolean variable, like !isOnGem. So if we are on a gem, that whole statement, the exclamation mark and all, will return false. Confusing? Sure, maybe a little. But we’ll get it with practice.
Then there’s the AND operator, which is two ampersands together (&&). This tests to see if both values are true. If one of them is false, the test will fail and return false. In code, it would look something like this: isOnGem && isBlocked. The only way this returns true is if we are on a gem and we’re also unable to move forward (that’s what the isBlocked tests).
Lastly, there’s the OR operator, which is two pipes next to each other (||). In this case, if one of the values is true, we’ll return true. The code looks like this: isOnGem || isBlocked. In this case, if either case is true… if we’re on a gem or we’re unable to move forward… or even both are true, we’ll return true.
Deep Breaths
The puzzles will require a bit of patience and sometimes a bit of trial and error to get a feel for combining conditions. But that’s what we’ll be practicing today.
Hopefully a few of the tricks we use to visualize the concept of logical operators will help you in today’s session!
We have covered a ton already this week, with our functions with added flair. You’ve been writing (and using) code that is super powerful because we can pass in parameters that change what the function actually does.
Hopefully you were able to build your own amazing game of Rock, Paper, Scissors. In our own version above we might have cheated a little by making an unbeatable unicorn. What did you do? Feel free to email us your creations at [email protected].
Logical Operators
Today we start on Chapter 8 of Everyone Can Code Puzzles, Logical Operators!
We’ll be talking about booleans again: values that are either true or false.
We’ll be introduced to the NOT (!) operator, which turns a boolean value into its opposite value. The AND (&&) operator, where two boolean values must be true for the condition to be true. And the OR (||) operator, which tests two booleans and if one of the booleans is true then the condition will consider itself true and execute that branch of the condition.
What this all means is that our conditions get can more sophisticated. With logical operators it allows us, especially with the && and || operators, to test multiple conditions at once.
Here in Ireland, you might check to see ifit’s raining AND if it’s not too windythen you would bring an umbrella, otherwise, you would leave the umbrella at home.
We’d write that condition a little like this, if it were code:
We’ll keep going with our final playground page in Learn to Code 2 about functions with parameters today. We started on the page yesterday, but today we’ll finish off one of the few possible solutions, and then we’ll move on to yet another new playground.
Let’s Play a Game
After that quick review we’ll move on to our new playground: Rock, Paper, Scissors.
This is an amazing playground for encouraging interaction amongst your students and creativity. The students will build their own game of rock, paper, scissors with emoji and battle it out!
It’s usually one of the more engaging sessions in a class, and hopefully that translates to home, as well.
Can you believe you’ve made it to week four? This is a gigantic effort! While it’s true that #EveryoneCanCode, it’s not easy. It requires practice and a lot of patience.
And if you feel like you’re a little behind, need to catch up, or just want to re-watch a session, you can always use the kids.code() playlist.
Recap
Last week we discussed two heavy topics: Types (and Initialization) and Functions with Parameters.
The syntax that we’ve learned so far gets a little funkier when we add these things into the mix.
Our homework over the weekend was to figure out a better way… or maybe just a different way, to write the function in the screenshot above.
Did you manage to come up with something better?
What’s Up This Week
We’re going to keep going with Functions with Parameters this week. Over the Easter weekend (which is still on here in Ireland with Easter Monday) we threw together a little addition to our Holiday Turtle Graphics playground.
In the new chapter, following our usual Christmas one, we’ve added two new egg-decorating pages. This chapter uses the more involved Turtle Graphics functions, so it shows off both instances of a type and a function with a parameter.
It’s available from The Code Hub’s playground feed, and will show up automatically in Playgrounds if you’ve already subscribed to the feed.
Later…
Later in the week we’ll move on to Logical Operators, which sounds like a particularly geeky band. They’ll really make our earlier lessons with conditional coding start to sing.