Chapter 6 Loops

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A loop based on a counter is a ____________ loop.

counter-controlled

How many times will the following loop execute? for (a = 6; a>0; a--) for (b = 5; b>3; b--) write( a+b+ "\n");

12 times

For a situation in which the number of iterations through the loop is known, which type of loop would be the most compact to write?

for

write an example of an infinite loop.

for ( cat= 3; cat > 0; cat++) Writeline(cat);

Write a for statement that would perform the same action as the following while statement. int num = 100; while ( num > 0) { Writeline(num); num -=20; }

for ( num = 100; num > 0; num -= 20) WriteLine(num);

Write the loop for question 11 as a for loop.

for (a = 10, b = 3; a>1; a -= 2) Write(a*b + "\t");

Write a loop to display every 7th number beginning with 2 and ending at 70. You may use any loop structure.

for (num = 2; num<=70; num +=7) WriteLine(num);

what are the pretest loops?

for and while loops

In addition to the C-style loop structures while, do...while, and for, C# includes another type of loop called _____

foreach

Which loop structure is new to the C family of programming language?

foreach

A loop within a loop is called a ________.

nested loop

A(n) ___________ is when a loop executes one too many or one too few times.

off-by-one error

A technique where a method calls to itself repeatedly is ____

recursion

A method that calls itself repeatedly until it arrives at the solution is a(n) ____________ method.

recursive

a sentinel value, which is an extreme or dummy value is used in a _______ loop.

sentinel-controlled

What does it mean to prime the loop?

to give and initial value before the loop begins.

T/F A for loop can only be nested inside another for loop

False

T/F Recursion is a type of loop structure

False

T/F The while statement is the only type of loop construct that can be used to develop a sentinel-controlled loop.

False

T/F The white statement is a post-test loop, which means that the conditional expression is tested before any of the statements in the body of the loop are performed

False

T/F When a nested loop condition is written, the outer loop is completed prior to the inner loop being executed

False

T/F With nested loops, the outermost loop is always totally completed before the innermost loop is executed.

False

The do...while statement is a post-test loop. T/F

True

int sum = 0; int number = 1; While (number < 100) { sum = sum + number; } The program above produces an infinite loop T/F

True

What would be the result of: int cnt = 0; do { WriteLine("*"); while cnt >0; ?

*

T/ F The loop control variable used with a for statement must be an integral value

False

T/F One reason to use a loop is to enable you to shorten the amount of code needed in a program.

True

If the loop body was changed from counter++; to counter +=2; how many times would the loop be executed? int counter = 0; while (counter < 100) { Writeline(counter); counter ++; }

50 times

What is a posttest loop?

A loop that is executed once before the conditional expression is tested.

What is a state-controlled loop? Explain and give an example.

A yes/no loop, its's repetition depends on the state of a variable. Example:" Do you want to continue?"

What is a sentinel value? Explain and give an example.

An extreme value or dummy value. Example" press -99 to exit, where -99 is an impossible value for a test grade:"

Give an example of one type of collection structure in C#.

Array

State-controlled loops are controlled by ___________.

Boolean flags

A post-test loop means the answer is posted after the loop is run T/F

False

Any while loop can be written as a for loop. T/F

False

T/F Looping is one of the three basic programming structures.

True

Used with a loop, how does a continue statement differ from a break statement?

The continue transfers control back to the condition expression for re-evaluation. The break terminates the loop.

When will the following loop stop executing if number is initialized to be 5? do { Writeline(number); number = number + 1; } while(number < 5);

The loop will stop executing after the first time it runs.

What is one advantage of a sentinel-controlled loop?

The number of iterations does not have to be known.

In order to have more than one statement be executed as part of the loop body, what must be done to the statements?

They must be enclosed in curly braces {}

identify the initializer, condition, and update in the following: for ( i=5; i>3; i--) write( a+b + "\n");

initializer is i = 5, condition is i > 3, and update is i--.

With the while loop the body of the loop________.

is performed as long as the conditional expression evaluates to true.

What is a pretest loop?

it is a loop where the conditional expression is tested before the statement in loop body is executed.

Recursion involves a(n) ____________ calling itself until it arrives at a solution.

method

Given the following statement, what would be the output produced? for (int cnt = 0; cnt < 3; cnt++) Write(cnt);

0 1 2

Given the following code answer the following questions. int total = 5; while ( total <= 80) { Writeline(total); total+=5; } 1.How many times will the total be written? 2.What will be the first value written? 3. What will be the last value written? 4.What will be the value of total after the loop?

1. 16 times 2. 5 3. 80 4. 85

for ( int i = 2; i < 102; i++) Writeline(i + "\t"); How many times will the loop body be executed?

100 times

For ( int i =0; i <2; i++) { for (int k = 0; k < 3; k++) { Write(k); } WriteLine(); } How many lines are printed? what is printed on each line?

2 lines are printed 012

what is the output of the following code? a=10; b=3; while (a>1) { Write (a*b + "\t"); a -= 2; }

30 24 18 12 6

for (int i = 0; i <5; i++) Writeline(i + "\t"); Given the code above, how many times will the loop body be executed?

5 times

T/F A counter-controlled loop requires a read on the outside of the loop and another read as the last statement in the body

False

T/F Given the for statement's form, for (initialization; test; update), the initialization is performed with each iteration through the loop

False

T/F Most times more memory is required for recursive solutions

False

T/F The break statement may only be used with pretest forms of loops.

False

T/F The for statement may not be written without an initialization component.

False

T/F The identifier used in the foreach statement must be an integral value

False

T/F The loop body is always executed at least one time with both the pretest and the posttest.

False

T/F You should always use a state-controlled loop if you plan to write a posttest.

False

What would be the output for the following loop int i = 0 while ( i <25) { i++; Write(i); }

It would produce an infinite loop

How do loops increase programming power of languages?

Loops enable you to identify and block one or more statements to be repeated, type the statements one time, but have the statements repeated any number of times.

The __________.show() method can be used to display a Window that resembles the typical Windows application you are accustomed to seeing.

MessageBox

What are the different types of loop structures that are available in C#? Mention if each is pretest or post-test.

Prestest loops: while loop and for loop Posttest loops: do...while loop

What does it mean to prime a loop?

To give it a starting value

T/F In order to have more than one statement be executed as part of the loop body, that statements must be enclosed in curly braces.

True

T/F The decision regarding which type of loop to use is sometimes just a personal choice of which loop you prefer to write

True

T/F The do...while is used to implement a posttest loop.

True

T/F The expression used in the foreach statement is the collection

True

T/F Throw, return and goto also belong to the category of statements that implement unconditional transfer of control.

True

How does a sentinel-controlled loop differ from a counter-controlled loop in terms of what statements would need to be written prior to the conditional expression?

With a counter-controlled loop, a variable needs to be defined as a counter and initialized to zero. A sentinel-controlled loop normally requires a 'priming of the read' on the outside of the loop. This places a value in the variable that is used for testing.

what is the difference between the pretest loop and posttest loop?

With a posttest loop, the body is always executed at least once, while with the pretest loop, the body will only be executed when the conditional expression is true.

How would you end up in an infinite loop?

an infinite loop occurs when the conditional statement never becomes false.

With recursion, you must identify a(n) ___________case

base or simple

What is the general form of the continue statement?

continue is placed on a line by itself and terminated with a semicolon.

If you know your loop will always be executed at least once, use the _____________ option.

do...while

Give an example of a post-test loop structure in C#?

do...while loop

The posttest loop that we use is the ___________ loop.

do...while loop

What is an off-by-one error?

where a loop process one too many or one too few times.

If you place a semicolon after the conditional expression before the body of the loop, _____________

you produce an empty bodied loop.


Kaugnay na mga set ng pag-aaral

Musculoskeletal System Ch.49 and Ch.50

View Set

Chapter 9, chem 111, Chem 349502

View Set

HL Psychology of Human Relationships

View Set

Biology - CH 11 The Diversity of Bacteria and Archaea

View Set