6.5. While Loop Review#
When might you prefer a
forloop in place of awhileloop? What do you gain?When might you prefer a while loop or a foreach instead of a for loop?
Describe in general when a
foreachloop is going to be easier to use than awhileloop.Each sentence below introduces a problem. What words/combinations suggest a loop/repetition?
Square each number from 1 to n.
Respond until the user says to stop.
Repeat the process until the width is < .00001.
Count the vowels in the sentence that you are given.
See if there are any double letters in the word that you are given.
Compare do-while and while loops: How do you think about which one to use?
In general, what causes an infinite
whileloop?A
whileloop is generally terminated when the program evaluates the condition in its heading and it becomes false. How else can a program exit from awhileloop?When inside a loop, a return statement should generally only appear as a sub-statement of what kind of statement?
Which of these conditions is safer in general, with arbitrary
stringsandinti?s[i] != '#' && i >= 0 && i < s.Length i >= 0 && i < s.Length && s[i] != '#'
What is printed?
int value = 3;
while (value > 0)
{
Console.WriteLine(value);
value--;
}
Console.WriteLine("Done");
How would the output change if value started at zero? Explain when the condition is tested.