Chapter Review Questions

  1. While loops are a very important part of your programming tools. Put in your own words: when should you think to use a while loop? What words or phrases in the description of a problem suggest the use of a loop?

    More concretely, 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.

  2. Loops are also among the hardest things for many students – with lots of things to think about. There is a sequence of general process questions that you can ask yourself to help you organize your work. What are they in your words? Do you know them well, or have them written down in a place you can easily jump to?

  3. Compare do-while and while loops: How do you think about which one to use?

  4. In an interactive while loop you need to continuously get data from the user. Where do you generally put the code to get more data?

  5. In general, what causes an infinite while loop?

  6. What is wrong with this statement: When the condition in a while loop heading becomes false, the loop statement immediately terminates.

  7. A while loop will terminate when the program evaluates the condition in its heading and the value becomes false. What is the important difference in this statement from the previous incorrect statement?

  8. When is the next step in a program to evaluate the condition in a while loop heading? (There are two situations.)

  9. A while loop is generally terminated when the program evaluates the condition in its heading and it becomes false. How else can a program exit from a while loop?

  10. We generally construct a loop so its body is a compound statement, composed of a sequence of statements inside. If this body is a sequence of simple statements, does it make sense for one of them to be a return statement?

  11. When inside a loop, a return statement should generally only appear as a sub-statement of what kind of statement?

  12. Which of these conditions is safer in general, with arbitrary string s and int i?

    s[i] != '#' && i >= 0 && i < s.Length
    
    i >= 0 && i < s.Length && s[i] != '#'
    
  13. What is printed?

    //          012345678901234567890
    string s = "Is coding cool?  Yes!"
    Console.WriteLine(s.Trim());
    string t = s.Substring(9, 8);
    Console.WriteLine(t.Replace(" ", "/"));
    Console.WriteLine(t.Trim().Replace(" ", "/"));
    Console.WriteLine(s.StartsWith("is"));
    Console.WriteLine(s.ToLower().StartsWith("is"));
    int i = s.IndexOf("co"), j = s.IndexOf("co", i+1),
        k = s.IndexOf("co", j+1);
    Console.WriteLine(i + " " + j + " " + k);