5.10. Chapter Review#

  • Note that the course policy is that you should not use generative AI without authorization. If you are suspected to have used generative AI and not able to explain/reproduce your work when requested, all your related assignments throughout the semester will be regraded as 0.

  • Prepare this assignment using a Word document.

  • Number and write answers under each question.

  • Paste code screenshots when required.

You are encourage to keep your test code in the project. To do that:

  • Create a dotnet console app project (Creating a C# Project), if you have not done that, in your USERNAME/workspace/introcscs directory; called it Ch05ForLoop.

  • Place your test code in methods to be called from the Main method. Name your methods properly.

  1. When you have nested for loops, and you reach the bottom of the body of the inner loop, where does execution go next?

  2. What happens when you omit the condition in a for loop?

    Explain and try it out in csharprepl or VS Code and paste the results here.

  3. In the heading of a for loop, how do you initialize or update several variables?

  4. Rewrite

    num /= 2;
    

    equivalently without the operand /=.

  5. Rewrite

    bigName = bigName - 10;
    

    with a statement that only includes bigName once.

  6. Distinguish the effects of these two statements:

    x-=2;
    
    x=-2;
    
  7. What is printed?

    Console.WriteLine("12345678");
    for( int p = 1; p < 6; p++) {
        string formatStr = "{0:F" + p + "}";
        Console.WriteLine(formatStr, 1.2345678);
    }
    

    Explain, then paste here the screenshot of the results of you testing it in csharprepl or VS Code.

  8. What is printed? (Just “,4” has been inserted.)

    Console.WriteLine("12345678");
    for( int p = 1; p < 6; p++) {
        string formatStr = "{0,4:F" + p + "}";
        Console.WriteLine(formatStr, 1.2345678);
    }
    

    Explain, then paste here the screenshot of the results of you testing it in csharprepl or VS Code.

  9. What is printed?

    Console.WriteLine("123456");
    for( int w = 6; w >= -6; w -= 4) {
        string formatStr = "{0," + w + "}|";
        Console.WriteLine(formatStr, "here");
    }
    

    Explain, then paste here the screenshot of the results of you testing it in csharprepl or VS Code.

  10. Even if you want to process every element of a sequence, what would keep you from using a foreach loop?

5.10.1. Additional Iteration Review Questions#

  1. When might you prefer a for loop in place of a while loop? What do you gain?

  2. When might you prefer a while loop or a foreach instead of a for loop?

  3. Describe in general when a foreach loop is going to be easier to use than a while loop.

  4. Each sentence below introduces a problem. What words/combinations suggest a loop/repetition?

    1. Square each number from 1 to n.

    2. Respond until the user says to stop.

    3. Repeat the process until the width is < .00001.

    4. Count the vowels in the sentence that you are given.

    5. See if there are any double letters in the word that you are given.

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

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

  7. 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?

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

  9. 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] != '#'
    
  10. What is printed?