A controlled loop uses a true/false condition to control the number of times that it repeats

1.A _____ -controlled loop uses a true/false condition to control the number oftimes that it repeats.A) BooleanB) conditionC) decisionD) countTable for Individual Question Feedback Points Earned:1.0/1.0Correct Answer(s):B

Correct2.A _____ -controlled loop repeats a specific number of times.1.0/1.0D

Get answer to your question and much more

Correct3.Each repetition of a loop is known as a(n) _____ .1.0/1.0D

Get answer to your question and much more

Correct4.The while loop is a _____ type of loop.1.0/1.0A

Get answer to your question and much more

Correct5.A(n) _____ loop has no way of ending and repeats until the program isinterrupted.A) indeterminateB) interminableC) infiniteD) timelessTable for Individual Question Feedback Points Earned:1.0/1.0Correct Answer(s):C

A __ controlled loop uses a true/false condition to control the number of times that it repeats
a. Boolean
b. condition
c. decision
d. count

A ___ controlled loop repeats a specific number of times.
a. Boolean
b. condition
c. decision
d. count

Each repetition of a loop is known as an
a. cycle
b. revolution
c. orbit
d. iteration

The while loop is a ___ type of loop
a. pretest
b. posttest
c. prequalified
d. post iterative

The Do-While loop is a __ type of loop.
a. pretest
b. posttest
c. prequalified
d. post iterative

The for loop is a ___ type of loop.
a. pretest
b. posttest
c. prequalified
d. post iterative

An__ loop has no way of ending and repeats until the program is interrupted.
a. indeterminate
b. interminable
c. infinite
d. timeless

A ___ loop always executes at least once.
a. pretest
b. posttest
c. condition-controlled
d. count-controlled

A___ variable keeps the running total
a. sentinel
b. sum
c. total
d. accumulator

A __ is a special value that signals when there are no more items from a list of items to be processed. This value cannot be mistaken as an item from the list.
a. sentinel
b. flag
c. signal
d. accumulator

Control Structures - Repetition

Repetition Statements

  • Repetition statements are called loops, and are used to repeat the same code multiple times in succession.
  • Python has two types of loops: Condition-Controlled and Count-Controlled
  • Condition-Controlled loop uses a true/false condition to control the number of times that it repeats - while. Basic syntax:
    while condition:
       statement(s) # notice the indent from of this line relative to the while
    
  • Count-Controlled loop repeats a specific number of times - for. Basic syntax:
    for variable in [value1, value2, etc.]:
       statement(s) # notice the indent from of this line relative to the for
    

while loops

  • while a condition is true, do some task.
  • while loop is known as a pretest loop, which means it tests its condition before performing an iteration
  • The boolean_expression in these formats is sometimes known as the loop continuation condition
  • The loop body must be a block, or a single statement (like with the if-statements)
  • How it works
    • The boolean expression is a test condition that is evaluated to decide whether the loop should repeat or not.
      • true means run the loop body again.
      • false means quit.

Examples

 
# This program calculates sales commissions.

# Create a variable to control the loop.
keep_going = 'y'

# Calculate a series of commissions.
while keep_going == 'y':
    # Get a salesperson's sales and commission rate.
    sales = float(input('Enter the amount of sales: '))
    comm_rate = float(input('Enter the commission rate: '))

    # Calculate the commission.
    commission = sales * comm_rate

    # Display the commission.
    print 'The commission is $%.2f' % commission
    
    # See if the user wants to do another one.
    keep_going = raw_input('Do you want to calculate another ' + \
                       'commission (Enter y for yes): ')


Code: commission.py

In all but some rare cases, loops must contain within themselves a way to terminate. Something in the loop usually makes the test condition false. When this is missing the loop is called an infinite loop. You have to interrupt the program to exit an infinite loop. Avoid infinite loops most of the time.

# This program demonstrates an infinite loop.

# Create a variable to control the loop.
keep_going = 'y'

# Warning! Infinite loop!
while keep_going == 'y':
    # Get a salesperson's sales and commission rate.
    sales = float(input('Enter the amount of sales: '))
    comm_rate = float(input('Enter the commission rate: '))

    # Calculate the commission.
    commission = sales * comm_rate

    # Display the commission.
    print 'The commission is $%.2f' % commission

Code: infinite.py


The for loop

  • The for loop as a Count-Controlled loop iterates a specific number of times.
  • Basic syntax:
    for variable in [value1, value2, etc.]:
       statement(s) # notice the indent from of this line relative to the for
    
  • How it works
    • The first line is called the for clause
    • In the for clause, variable is the name of a variable.
    • Inside the brackets is a sequence of values, that is comma separated. (in Python this is called a list).
    • Executing is as follows:
      1. The variable is assigned the first value in the list.
      2. The statements in the block are executed.
      3. The variable is assigned the next value in the list, if any repeat step 2 else the loop terminates.
    • The variable can be used in the loop.

Examples of for loops

Example:

# This program demonstrates a simple for loop
# that uses a list of numbers.

print('I will display the numbers 1 through 5.')
for num in [1, 2, 3, 4, 5]:
    print num

Output:

I will display the numbers 1 through 5.
1
2
3
4
5

Example:

# This program also demonstrates a simple for
# loop that uses a list of numbers.

print('I will display the odd numbers 1 through 9.')
for num in [1, 3, 5, 7, 9]:
    print(num)

Output:

I will display the odd numbers 1 through 9.
1
3
5
7
9

Example:

# This program also demonstrates a simple for
# loop that uses a list of strings.

for name in ['Winken', 'Blinken', 'Nod']:
    print name

Output:

Winken
Blinken
Nod

Using the range function with the for loop

  • range simplifies count-controlled for loops
  • Example:
    for num in range(5):
    	print num
    
  • Numbers from 0 through and up till, but not including, 5 are generated. Equivalent to:
    for num in [0, 1, 2, 3, 4]:
    	print num
    
  • Passing a second argument to range the first is used as the starting point and the second is used as the ending limit
    for num in range(1, 5):
    	print num
    
    Output:
    1
    2
    3
    4
    
  • By default the range function increments by 1, passing a third argument defines the step amount:
    for num in range(1, 10, 2):
    	print num
    
    Output:
    1
    3
    5
    7
    9
    
  • The target value can be used in the loop:
    # Print the table headings.
    print '%-10s%-10s' % ('Number', 'Square')
    print '--------------------'
    
    # Print the numbers 1 through 10
    # and their squares.
    for number in range(1, 11):
        square = number**2
        print '%10d%10d' % (number, square)
    
    Output:
    Number    Square    
    --------------------
             1         1
             2         4
             3         9
             4        16
             5        25
             6        36
             7        49
             8        64
             9        81
            10       100
    
  • A few common types of algorithms using for-loops:
    • A counting algorithm
    • A summing algorithm -- Adding up something with a loop
    • A product algorithm -- Multiplying things with a loop
    • Sentinel controlled loops
  • Examples using nested loops:
    • Simple nested loop example (User controlled loop iteration)
    • Prints out a rectangle (User controlled loop iteration)
    • Prints out a triangle (User controlled loop iteration)

What controlled loop uses a true/false condition to control the number of times that it repeats?

A condition-controlled loop uses a true/false condition to control the number of times that it repeats. 2. A count-controlled loop repeats a specific number of times.

What is a condition controlled loop?

Condition Controlled Loops. ∎ A condition controlled loop is programming structure that. causes a statement or set of statements to repeat as long as a. condition evaluates to True.

What is a count

A count-controlled loop repeats a specific number of times. In Python you use the while statement to write a condition-controlled or sentinel loop, and you use the for statement to write a count-controlled loop.

What is a condition controlled loop quizlet?

condition-controlled loop. A loop that uses true/false condition to control the number of times it repeats.