I teach in a secondary, first grade, school in Italy. The kids are 13 years old, on last year before second grade. I am teaching them the very basic of programming. Last year we did some flowcharts algorithms, this year we started code programming in Octave. I've teached them how to make simple algorithms with if/else and now I'm going to go for the for and while loops, but I struggle in finding exercises and examples which could be suitable for them, apart from cumulative sums/differences. Consider that they are very young and have just basic math knowledge. They don't even know what a matrix is, but I can overcome that by explaining at least a monodimensional one. But really I can use some help on exercise with for and while. Any help?
I'm not familiar with Octave, but here are some examples we use for while loops:
- Collect a user's input until it is the correct type (such as asking the user for a number)
- Find a running sum of numbers until a user inputs 0
- A piggy bank that collects coins (represented by single-letter user inputs) that provides a total balance only after the user inputs "exit"
- Play tic-tac-toe until the game is won or has reached a draw. (Note that this game is small enough that you can do it with nine variables, which gives you flexibility based on what you've covered so far with your students)
- Play the game of Nim until a player has won, then begun a new game. (Loop in loop!)
A few caveats for you: don't introduce loops inside loops at first, and when you do, use methods to break it up. There is plenty to master in single loops.
I also wouldn't introduce for loops and while loops at the same time. Start with while, since for loops just jam all of the loop governance onto a single, weird line. Let them work through loop logic in the more intuitive while structure first.
When you do eventually get to for loops, you can show its equivalence to
while, and then do things like take a number range and return its sum. Point out that the for loop is syntactic sugar just to show a loop's structure all at once, and that we tend to use it only in the simplest cases, such as when we know exactly how many iterations we want in advance.
The collatz conjecture is a great example of a while loop:
- start with any number
- if it's even, divide by 2
- if it's odd, times three and add 1
- stop if you ever reach 1. (what happens if you continue?)
The conjecture that you'll always reach 1 is just about the easiest to explain, hardest to prove, statement in math. As in: still open.
For students to explore:
- What's the longest route to 1 you can find?
- What's the biggest increase from the starting point?
- Et cetera.
Look at the scratch resources at NCCE.
Scratch is a graphical block based language designed for kids to learn programming.
The National Centre for Computing Education, have produced various teaching resources. As an experienced programmer, and new teacher, I used these as is the first time, then started adapting them.
When I was teaching myself programming at age 14 I used nested For loops to make shapes on a text screen. For example, input a width and height and make a rectangle. Then modify the code to make it hollow. Then draw triangles, then diamonds.
Eventually I had a program that continuously chose shapes and sizes at random (kids love random stuff) which scrolled up the screen indefinitely. It was easy to get started and had enough challenge to be interesting.