Some questions people had in class #25

 

In a situation that requires a loop, can we use any of the three types?

 

If it is possible for a loop to require 0 times around under some circumstances, a do-while loop cannot be used.  If the number of times the loop will go around is not known when the loop is reached, then a for loop cannot be used.  A while loop can be used / adjusted to handle any looping situation.

 

What is better to do, a while loop or a for while loop?

 

For counter controlled loops – a for loop (not “for while” – there is no such thing.). For loops with a number of repetitions that is unknown (even when the program is running), a while loop is better.

 

Does it benefit anyone to use a for statement if not using one of the 3 conditions?

 

This is a very astute question.  The more you leave off, the less benefit you get. To get full benefit of for statement, use all three.

 

Other than length, what other advantages is there from the for loop?

 

When somebody reading the program (suppose they have the job of updating or fixing the program), a for loop immediately suggests that a counter-controlled loop is being used. So the reader can read faster without loss of understanding.

 

What effects will happen when the increment is placed differently?

 

In the third part of a for loop, it does not matter is a prefix or postfix ++ is used, assuming that the whole 3rd part involves the variable++ or ++variable.

 

How your methods for the skater project gets rid of the high and low score?

 

The pseudocode for it is as follows:

 

set max to bad value – lower than any possible, so first judges rating will be new highest

set min to bad value – higher than any possible, so first judges rating will be new lowest

set total to zero

loop through all scores

add current score to total

            if current score is higher than max

            max = current score

            if current score is lower than min

                        min = current score

END LOOP

Subtract min and max from total

       

 

Why assignment 7 runs so quirky?

 

Hard to tell without looking at it. However, this is the first assignment with complicates combinations of if’s. Each path that a program can take through a program must be tested to ensure that the right instructions have been written.  If some paths are written correctly and others are not, then the program’s behavior may seem quirky.

 

 

How to master loops?

 

Practice.

 

How would I start tomorrow’s problem in class?

 

Underline key parts of the problem description. Try to identify tasks that have to be done at some point in the program, even if you currently can’t put them in order.

 

Loops of all kinds

 

You need to obtain help.  Loops are an essential skill, and we are done covering them.