Class Exercise: Repetition Structure--while statement(pretest)
Description:
A preTest repetition structure, or "loop", may control the execution of a
block of statements. A conditional expression is tested to determine whether
processing is to occur. The statement block is repeated as long as the
condition is true.
1. Make a count-controlled loop that repeats as long as a variable named
count is less than another variable named limit. Initialize count
to 0 and limit to 5. The statement block should contain statements to write
the current value of count and count squared, and also increment count.
2. Code an event-controlled loop(sentinel) that repeats as long as a certain
input value(e.g. -1) has not been encountered. Declare and initialize
needed variables. Prompt and input a numeric point value. Within the
loop statement body, add the number input to an accumulator named sum.
Input another number add to the sum, and continue until the sentinel value
is detected; then write the sum.
3. Construct a event-controlled loop(flag set within statement body) that uses
a variable named done to signal when looping is to end. Initialize done to
false and totalPoints to zero. The loop should continue as long as done
remains false. The loop body should input a value named points, add points
to totalPoints, compare totalPoints to some maximum value, and when it is >=
that maximum value, assign true to done. After looping terminates, output
totalPoints.