Chapter 4: Loops and Control Structures - Prof. Y. Daniel Liang, Study notes of Computer Science

Various aspects of loops in c++ programming, including the use of do-while, while, and for loops, loop control statements (break and continue), and examples of loop usage. It also discusses the differences between do-while and while loops.

Typology: Study notes

Pre 2010

Uploaded on 08/04/2009

koofers-user-q0y-1
koofers-user-q0y-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 4 Loops
1. count < 100 always true at Point A. count < 100 always
false at Point C. count < 100 is sometimes true or sometimes
false at Point B.
2. It would be wrong if it is initialized to a value between 0 and 100, because it could
be the number you attempt to guess.
3. (a) Infinite number of times.
(b) The loop body is executed nine times. The printout is 2, 4, 6, 8 on
separate lines.
(c) The loop body is executed nine times. The printout is 3, 5, 7, 9 on
separate lines.
4. The difference between a do-while loop and a while loop is the order of
evaluating the continuation-condition and executing the loop body. In a while loop,
the continuation-condition is checked and then, if true, the loop body is executed. In a
do-while loop, the loop body is executed for the first time before the continuation-
condition is evaluated.
int sum = 0 ;
int number;
cin >> number;
do
{
sum += number;
cin >> number;
} while (number != 0 );
5. Same. When the i++ and ++i are used in isolation, their effects are same.
6. The three parts in a for loop control are as follows:
The first part initializes the control variable.
The second part is a Boolean expression that determines whether the loop will
repeat.
The third part is the adjustment statement, which adjusts the control variable.
7. The loop keeps doing something indefinitely.
8. No. The scope of the variable is inside the loop.
9.
max is 5
pf3
pf4
pf5

Partial preview of the text

Download Chapter 4: Loops and Control Structures - Prof. Y. Daniel Liang and more Study notes Computer Science in PDF only on Docsity!

Chapter 4 Loops

  1. count < 100 always true at Point A. count < 100 always false at Point C. count < 100 is sometimes true or sometimes false at Point B.
  2. It would be wrong if it is initialized to a value between 0 and 100 , because it could be the number you attempt to guess.
  3. (a) Infinite number of times. (b) The loop body is executed nine times. The printout is 2, 4, 6, 8 on separate lines. (c) The loop body is executed nine times. The printout is 3, 5, 7, 9 on separate lines.
  4. The difference between a do-while loop and a while loop is the order of evaluating the continuation-condition and executing the loop body. In a while loop, the continuation-condition is checked and then, if true, the loop body is executed. In a do-while loop, the loop body is executed for the first time before the continuation- condition is evaluated. int sum = 0 ; int number; cin >> number; do { sum += number; cin >> number; } while (number != 0 );
  5. Same. When the i++ and ++i are used in isolation, their effects are same.
  6. The three parts in a for loop control are as follows: The first part initializes the control variable. The second part is a Boolean expression that determines whether the loop will repeat. The third part is the adjustment statement, which adjusts the control variable.
  7. The loop keeps doing something indefinitely.
  8. No. The scope of the variable is inside the loop.

max is 5

number 0

sum is 14 count is 4

max is 5 number 0

  1. Yes. The advantages of for loops are simplicity and readability. Compilers can produce more efficient code for the for loop than for the corresponding while loop.
  2. while loop: long sum = 0; int i = 0; while (i <= 1000) { sum += i++; } do-while loop : long sum = 0; int i = 0; do { sum += i++; } while (i <= 1000);
  3. No. Try n1 = 3 and n2 =3.
  4. The keyword break is used to exit the current loop. The program in (A) will terminate. The output is Balance is 1. The keyword continue causes the rest of the loop body to be skipped for the current iteration. The while loop will not terminate in (B).
  5. Yes. for (int i = 1; sum < 10000; i++) sum = sum + i;

while (number < 20) { number++; if (number != 10 && number != 11) sum += number; } cout << "The sum is " << sum; return 0; }

(a) Line 1: The semicolon (;) at the end of the for loop heading should be removed. Line 4: the semicolon (;) at the end of the if statement should be removed. Line 5: Missing a semicolon for the first println statement. Line 9: The semicolon (;) at the end of the while heading should be removed. Line 17: Missing a semicolon at the end of the do-while loop. (b) The loop will be executed only one time, because the test condition for the while clause is true.

Tip for tracing programs: Draw a table to see how variables change in the program. Consider (a) for example. i j output 1 0 0 1 1 2 0 0 2 1 1 2 2 3 0 0 3 1 1 3 2 2 3 3 4 0 0 4 1 1 4 2 2 4 3 3 4 4 (A). 0 0 1 0 1 2 0 1 2 3

(B).

(C).

1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx (D). 1G 1G3G 1G3G5G 1G3G5G7G 1G3G5G7G9G

x is - The reason: When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. 2147483647 + 1 is actually -

(A) n times (B) n times (C) n-5 times (D) The ceiling of (n-5)/3 times