tags:

views:

490

answers:

11

some junior high school students asked me why do we need to have loop in a program, and they just won't accept that it is needed... i already told them if we need to find the average and standard deviation of 100 numbers, we can put the numbers in an array by a loop, and then we can find the average, standard deviation, or print the numbers out however we want to (across the screen or vertically). or even find the medium or model afterward, but they just won't accept it... can somebody suggest a better way to explain it?

+8  A: 

Let them actually program and see how well would they go without loops.

Either that, or they're hardcore FP fans.

alamar
+1 for FP. I was thinking list recursion too. :)
abababa22
Or they're accustomed to languages like GNU R, APL or J :)
Joey
And there is no such thing as recursion at the cpu level. Its all loops, jumps and repeats.
Unknown
lol, FP fans .
dr. evil
what is FP fans?
動靜能量
Guys (gals?) who do, or brag about doing, a lot of Functional Programming.
alamar
+5  A: 

Technically they're right, you don't need a loop, you could do all these things using recursion.

Why don't you ask them how they would do it?

Greg Beech
Make that several million numbers and see how that recursion works out. Talk about "stack overflow"...
duffymo
+2  A: 

How about a more real-world example... Say you're in the grocery store, and want to unload everything out of your shopping cart - and you need to express that in code. If you have a loop (a while in this example), you can say:

while(the cart is not empty) put an item on the conveyor

as opposed to the situation where you didn't have a loop, where you'd have to list each item in the cart one by one and manually add the code to move them from the cart to the conveyor.

The reality is that code is just a more formalized way of looking at the world around us, and we use loops a whole lot of the time. ;)

Mark
+2  A: 

Put the numbers on some cards. Ask the students to calculate the average without looking at every single card. They won't be able to do this (at least, not accurately!), which leads into the next step.

Then ask them to write down the series of steps which would describe to a complete novice, how to calculate the average of the stack of cards.

Their description will contain an iterative loop (and give them bonus points if their loop is recursive!)

Paul Dixon
+1  A: 

Make them do things like initialize 100x100 array, and calculate average of a 10x10 integer array. They CAN use recursion. But that's just looping with a stack.

Hugo
+1  A: 

Consider anything that requires repetition. You can tell your students that if they want to, they can write a different program for each size of the input. Then, ask them what happens if the input size is an unknown and will be supplied by the user at a later time (1000 years from now).

Now, as people here have already said, you only need recursion to make things work, you don't strictly need loops. However, in a model of limited resources, some recursive algorithms do not scale for all input sizes, due to having to store state before each recursion step, either in the function stack or in the function call arguments.

ASk
This is not correct. __Every__ loop using constant space can be written recursively in a way that compilers with tail call optimization will use the same amount of space.
Unknown
A: 

Mention that their OS runs a loop.

amr
+1  A: 

The students must have heard about functional programming. You It's very IN these days, ya know ;) Next thing you know and they will shout "Why do we need assignment (=) in programming?"

All jokes aside, I believe they have no clue what functional programming is and the fact that they "won't accept" loops means they have no clue what "programming" is either. But I guess it's somewhat hard to grasp an an early age. Loops come so very natural to me, I can't see how can anyone write programs without them.

The one thing that really helps is REPETITION. Yes, REPETITION would help in your case. Just tell them it's a fact, again and again, and they are bound to learn it sooner or later. As soon as they write their first program and successfully use a loop structure, they will remember what it's good for.

Peter Perháč
A: 

You also forget higher order functions, so you do not need (explicit) recursion and iteration at all.

avg = [sum] [len] bi / (in pseudo-factor: take the element on the stack and calculate the sum of it. Remember this sum, and take the same element you took from the stack and calculate the length of it. now push both results (first the sum, then the length) on the stack and divide the second-top-element of the stack by the topmost element of the stack), or avg x = foldr (+) x / length x in haskell. Neither recursion not iteration visible.

Of course, eventually there is recursion and iteration hidden, but insisting on this is basically the same as saying: we need to use gotos, because everything is just gotos. ;)

Tetha
A: 

If they are so hardcore to not accept that a loop is needed, then sturd them with some "theoretical" stuff: first, show the equivalence between looping and recursion:

while (!stack.isEmpty) { 
    ... 
}

or just say that CPU doesn't have recursion, for recursion => loop and

fun(state) { 
  assign-state
  if (condition) {
      yourLoopCode();
      return fun(modifiedState);
  }
  return old-state
}

for loop => recursion; then you can show that a language where there is no looping and no way to do recursion like the aforementioned (primitive functions), you can't do what you can with a loop... like calculating the Ackermann function. If you don't want to be so bad, you can take as example something from numerical analysis, like the computation of the eigenvalue, where you have to loop until the convergence condition is needed... and you can't know in advance how many steps you have to do.

akappa
+2  A: 

Make them print all odd numbers between 1 and 5000, then get a popcorn and watch them suffer.

If they can't do this and still refuses that loop is not required I'm sorry but you got bunch of chronically challenged students (assuming they are not actually special). If they can't understand a reasonable and provable argument then they've got other problems which you might not find the answer in SO.

dr. evil
You can get some extra-suffer from them requesting to print all odd numbers between 1 and rand() ;)
akappa