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?

  • $\begingroup$ I asked a similar question cseducators.stackexchange.com/questions/6699/… $\endgroup$ Dec 28, 2021 at 7:55
  • $\begingroup$ Do you know Processing? I do think Octave is too difficult /too math oriented for 12/13 kids. In Prcessing, basic ideas and notions of programming can be visualized by animated geometry examples. It was deveolped in a school of art with the idea of being a didactical tool too. (scrivo dall'Italia anche io :-) ). $\endgroup$
    – mario
    Dec 28, 2021 at 15:06
  • $\begingroup$ How is it possible, in Italy or anywhere else, that standard examples aren't available to all teachers? $\endgroup$ Jan 8, 2022 at 23:55

5 Answers 5


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.

  • $\begingroup$ Disagree on waiting to introduce for loops, since they're very useful for things like iterating over arrays. For instance, in python, "for var in var_list:" $\endgroup$
    – nick012000
    Dec 17, 2021 at 21:37
  • 3
    $\begingroup$ @nick012000 I see that -- in fact, I've even taken that approach for many years. I eventually switched to doing while first because I've found over the years that it helps my students avoid cognitive traps and understand better what a loop is doing. And usefulness hardly matters when we're only talking about 2 to 3 periods before we've covered both anyway -- we certainly cover for loops! I just have to make sure it all gets into their head in a sensible way. $\endgroup$
    – Ben I.
    Dec 17, 2021 at 22:19
  • 1
    $\begingroup$ Remark 1. Your examples are largely of "temporal loops": each iteration has to follow the previous, for instance because it corresponds to a subsequent event. However, in for-loops the loop index is often extraneous: you really want to perform an action , or test a predicate, over all elements in a collection. Thus, the python for i in whatever or C++ for ( auto e : collection ) are more natural than indexed loops, let alone while loops. So I would distinguish between two types of loops: strict sequences, and "for all" loops. Neither should be reduced to the other. $\endgroup$ Dec 19, 2021 at 21:04
  • $\begingroup$ Remark 2. If a for loop is syntactic sugar around a while loop, and you teach the latter first, shouldn't you argue that a while loop is syntactic sugar around a if / goto combination and teach that first? Or maybe I'm just kidding. I do think that showing some mechanism later because it can be reduced is the wrong approach. I try to teach the highest abstraction first. $\endgroup$ Dec 19, 2021 at 21:07
  • $\begingroup$ @VictorEijkhout I tend to point out early on that all of programming is made up, but that the CPU is more of a 'given', because we have to construct a physical thing to run a nonphysical process. That is the leap which makes programming different from everything else. The interface between transistors and a program is the 5 operations a CPU can do, and it is well worthwhile to pierce the bubble of obscurity about computers right at the start. Then there is some basis for the abstraction. Variables are unlike anything else in the universe. It is a wonder to behold. $\endgroup$
    – Scott Rowe
    Jan 9, 2022 at 22:52

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.

I struggle in finding exercises and examples which could be suitable for them.

See: Runestone Academy. Also see CSAwesome. CSAwesome is used by hundreds of teachers in the US.


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.


  • $\begingroup$ I'm not based in the UK but I love teachcomputing.org. It's such a tremendous resource! $\endgroup$
    – Ben I.
    Dec 17, 2021 at 18:36

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.