Hot answers tagged

58 votes

Teaching the humble for loop

Show then how the loop can be unfolded into a while loop: int i = 0 // Step 1 while (i< 100) { // Step 2 // Do something i++ // Step 3 } Explain that ...
thesecretmaster's user avatar
  • 4,795
34 votes
Accepted

Teaching the humble for loop

I know I'm coming late to this party but this is a really good question and I think it deserves a really good treatment. The original for loop, the one with the ...
candied_orange's user avatar
28 votes

Teaching the tradeoffs of the foreach loop

I think the advantages are huge, overwhelming. They avoid off by one errors - the bane of the novice programmer needing to allocate an explicit iterator - avoiding adding names to the namespace. ...
Buffy's user avatar
  • 36k
26 votes

Teaching the humble for loop

I teach for loops using the following pseudocode, which we then translate into actual code: ...
Peter's user avatar
  • 9,082
17 votes

Order to teach looping constructs in a non ideal language

I prefer to teach while loops as part of my unit on if/else statements since the syntax and ...
Bryan R's user avatar
  • 508
10 votes

Teaching the tradeoffs of the foreach loop

I think foreach loops are great, and are almost always preferred over for loops. While for loops are fundamental material and definitely should be covered, they're sort of infrequently used in ...
Michael0x2a's user avatar
  • 4,005
9 votes

Teaching the tradeoffs of the foreach loop

There is much discussion here about how a traditional for seems more powerful than a foreach, but a traditional ...
VisualMelon's user avatar
9 votes

Teaching the humble for loop

It's about setting boundary conditions first. Provide them with a more concrete example, like filling a glass of milk. You kind of need a glass or container (let me know the next time you pour milk ...
auden's user avatar
  • 4,684
8 votes

Teaching the humble for loop

tl;dr– A for loop is just a while loop that has extra slots for a pre-loop statement and a post-loop-run statement. Since they'...
Nat's user avatar
  • 897
8 votes

Teaching the humble for loop

Theoretically, I teach the while loop first. Others have already explained how and why, but I'll go ahead with an example: ...
Joel Rees's user avatar
  • 291
8 votes
Accepted

How to teach which loop should be used

Use the for when you know how many times to execute it, such as counting, or iterating over an array. Use while or ...
Gypsy Spellweaver's user avatar
8 votes

Order to teach looping constructs in a non ideal language

All of the IF and Loop structures are based on two fundamental operations: Test and Branch. The only difference between an IF-Else and a Loop of any kind is whether you branch downward or back to the ...
user4226's user avatar
7 votes

How to teach which loop should be used

I teach them while loops, for loops, and for each loops; in that order. What I've found is that while loops tend to make the most sense for students. The idea of while I'm hungry, eat pizza is ...
Ryan Nutt's user avatar
  • 3,029
7 votes

Teaching the humble for loop

I think the way to teach it is not by talking about order of operation, but by teaching the overall concept of a looping. The for header contains the steps that are ...
Barmar's user avatar
  • 341
7 votes

Single-dimensional array and simple looping activities of significance

The Dutch National Flag problem is linear in running time. Essentially sort an array with only 3 distinct values each of which may appear 0 or more times. (not length 3). You are allowed only one pass ...
Buffy's user avatar
  • 36k
7 votes

Single-dimensional array and simple looping activities of significance

A variant on the ENIGMA machine encryption works well in a single loop, and is sufficiently complex to give students a real challenge. The core idea of the ENIGMA machine for this assignment is that (...
Ben I.'s user avatar
  • 32.9k
6 votes

Example for For vs While Loop (in Java)

If I'm interpreting your question correctly you'd like real life examples that translate to code. Maybe something like the following. You want to drive or ride your bike to the ice-cream shop a few ...
Buffy's user avatar
  • 36k
6 votes

Teaching the humble for loop

I used to have a role where I supported college students in CS1. I developed the following exercises to address this exact issue: Counting code executions Text version: How many times do each of the ...
nova's user avatar
  • 1,975
6 votes

Using physical activity to explain the nested for loop

Your pupils are already be doing nested loops. ...
ctrl-alt-delor's user avatar
5 votes

How to teach which loop should be used

I find that a useful way of teaching the different types of loops is simply giving examples and asking student to express them more concisely. For example, I'd give my students a loops of the format: ...
thesecretmaster's user avatar
  • 4,795
5 votes
Accepted

Order to teach looping constructs in a non ideal language

While your ideal order makes sense, you really should try to plan the course based on the limitations of the language that you're working with. In this case, instead of thinking about the different ...
thesecretmaster's user avatar
  • 4,795
4 votes

Teaching the humble for loop

Maybe the for (int i=0; i<10; i++) { ... } loops seems funny when you compare it to the "interval" loop of other languages, like Pascal's ...
Michel Billaud's user avatar
4 votes

Teaching the tradeoffs of the foreach loop

To do something like the following with foreach... ...
Buffy's user avatar
  • 36k
4 votes

How to teach which loop should be used

I introduce loops by showing them loops in the physical world. For example I have a student take 7 steps - counting loop. An other student walks to a specified point - while/until loop. We talk about ...
Alfred Thompson's user avatar
4 votes

Teaching the humble for loop

I am not an educator, but I hope my answer will be helpful. In B.A.S.I.C, which I learned as a child, there are "for-to-step" loops and their argument order is the same as C style languages. I have ...
TecBrat's user avatar
  • 141
4 votes

Using physical activity to explain the nested for loop

Well, you may want to do this outside. You may want to have a lot of time, and for some, you might want to have medical personnel available. To really do it as an active learning exercise, you need ...
Buffy's user avatar
  • 36k
4 votes

Order to teach looping constructs in a non ideal language

Perhaps you are creating a problem where none exists. You seem to be assuming that looping needs to be covered all at once and all together, rather than distributed over some some longer range ...
Buffy's user avatar
  • 36k
4 votes

What are good, practical labs and activities for loops?

Note that the following are intended for the instructor, not always directly for the student. If used well they require some analysis, not just coding, from the student. Use the fact that the sum of ...
Buffy's user avatar
  • 36k
4 votes

What are good, practical labs and activities for loops?

I'm adding in a second answer to look at a few heftier assignments, worthy of a lab. These were created by a truly fantastic colleague of mine. The game Nim is excellent for a loop with no arrays. ...
Ben I.'s user avatar
  • 32.9k
3 votes

Teaching the humble for loop

Another way of looking at this is to explore what is missing or lost at various stages: In Java For each: ...
Mr Bradley's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible