CS 1 chapter 5.5 the do while loop

Ace your homework & exams now with Quizwiz!

What is the do while loop's format?

do xxxx statement; while (expression);

What is the do while loops format with multiple statements

do { xxxx statement; xxxx statement; } while (expression);

The do while loop is a posttest loop, what does this mean?

it means it does not test its expression until it has completed an iteration. as a result the do while loop always performs at least one iteration, even if the expression is false to begin with.

Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 97 asterisks. Use no variables other than k.

k =1; do { xxxx cout << "*"; xxxx k++; } while (k <= 97);

in x = 1; do xxxx cout << x << endl; while (x < 0); What does this do?

this is a do while loop and is posttest. so it will print 1 because it iterates then tests the statement. Your should use a do loop when you want to make sure the loop executes at least once.

int x = 1; while (x < 0) xxxx cout << x << endl; what does this do?

this is a normal while loop that does not print 1 because the expression is false.

Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

total=0; k=1; do { total += k*k; k++; }while (k<=50);

What is an example of using a do while loop?

if you wanted to average a users test scores and give them the option to average more test scores again use a do while loop. because the do loop will average scores then check for the conditions to average more scores.

Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a do...while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.

j=1; do { xxxx cout << "*"; xxxx j++; } while (j<=n);


Related study sets

Ch.2 : Overview of computer crime

View Set

Chapter 12 Econ 202 exam Questions

View Set

REL 151: D9 The Divided Kingdom 1-2 Kings

View Set