Module 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Given the following code, how many times will the string "Hello" print a = 5 b = 10 while a == 5: print ("Hello") A. 1 time B. 5 times C. 0 times D. An infinite number of times

D. An infinite number of times

infinite loop

a loop that never ends

while loop

programming construct used to repeat a set of commands (loop) as long as (while) a boolean condition is true; known as a "condition controlled loop" -- this means that the looping behavior of the structure is dependent on the evaluation of a condition (i.e. a Boolean expression). The syntax for a condition controlled loop is almost identical to the if statement that we covered in the previous module; The trick with while loops is to control the condition that is being evaluated. One way to do this is to create a variable that can be used in a boolean expression to determine if a loop needs to iterate again.

continue statement

A statement that causes the remainder of the current iteration of a loop to be skipped. The flow of execution goes back to the top of the loop, evaluates the condition, and if this is true the next iteration of the loop will begin; A statement that causes a loop to stop its current iteration and begin the next one; will cause the currently executing iteration of a loop to terminate and the next iteration will begin; allows you to stop, not the entire loop, but just the current iteration that the loop is in; immediately ends the current iteration of the current loop; It simply causes the loop to "jump" to the next iteration. If the condition evaluates to True the loop iterates again, but if it evaluates to false it will not.

loop structure

A statement that continues to repeat actions while a test condition remains true; a structure that allows us to repeat a series of other statements on a number of times

The "break" statement: A. Can be used to immediately end a loop iteration B. Can be used to skip to the next loop iteration C. Can be used to end your program completely D. Can be used to negate a boolean condition

A. Can be used to immediately end a loop iteration

What is the value of x and y respectively at the end of this while loop? x = 0 y = 1 while x < 5: y = y + x x = x + 2 A. x = 6, y = 7 B. x = 0, y = 1 C. x = 6, y = 13 D. x = 4, y = 11 E. x = 6, y = 6

A. x = 6, y = 7

flags

Boolean variables are often used with while loops to control their execution. We sometimes refer to these as "flags" and we can easily check their value (True or False) to determine if we should allow a loop to continue iterating or not.

What is the ending value of the variable 'a' when the following program is run? a = 0 c = 0 while c < 3: if c % 2 == 0: a += 1 c += 1 A. 0 B. 1 C. 2 D. 3

C. 2

while loop is considered a ...

... "pre-test" loop, meaning that it only iterates upon the successful evaluation of a condition. This means that you always need to "set up" your loop prior to Python being able to work with it (i.e. setting up a control variable).

while loops are often used because...

... they execute an indeterminate number of times. You can cause a while loop to execute for a set # of times (i.e. loop exactly 5 times) by using an accumulator variable.

In Python, a while loop works as follows:

1. Evaluate a Boolean expression 2. If it is False, skip the block of statements associated with the while loop 3. If it is True: Execute the block of statements associated with the while loop Go back to step 1

The "continue" statement: A. Can be used to immediately end a loop iteration B. Can be used to skip to the next loop iteration C. Can be used to end your program completely D. Can be used to negate a boolean condition

B. Can be used to skip to the next loop iteration

T/F: The first thing that happens in a while loop is that it executes the entire code block located inside the while loop once. A. True B. False

B. False

What is the format for the while clause in Python? A. while condition B. while condition : C. while condition statement D. while condition : statement

B. while condition :

When will the following loop terminate? while keep_on_going != 999 - 1 : A. When keep_on_going refers to a value equal to 999 B. When keep_on_going refers to a value not equal to than 999 C. When keep_on_going refers to a value equal to 998 D. When keep_on_going refers to a value not equal to 998

C. When keep_on_going refers to a value equal to 998

Consider the following program: a = 1 while True: a += 1 print ("#" * a) if a % 5 == 0: break How many lines of output will be generated? A. two lines B. three lines C. four lines D. five lines

C. four lines

What is the structure that causes a statement or a set of statements to execute repeatedly? A. Sequence B. Decision C. Module D. Repetition

D. Repetition

repetition structure

the control structure that directs the computer to repeat one or more instructions until some condition is met, at which time the computer should stop repeating the instructions -- also called a loop or iteration; This involves writing your code one time and then placing it into a special statement that causes Python to repeat it as many times as necessary. Programmers usually refer to this as a "loop".

cntrl + c

will "interrupt" an infinite loop

break command

will immediately end the current loop; Once a break statement is encountered the loop immediately stops and Python resumes execute on the line directly after the end of the loop.


संबंधित स्टडी सेट्स

Chapter 3: Psychosocial Theories and Therapy

View Set

ACCT 442 FINAL Hoyle 8 quiz & homework

View Set

Chapter 15 Questions and Answers

View Set

Most Common Phrasal Verbs in English

View Set

HOST 156 FINAL FINAL FINAL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

View Set