Chapter 4 Computer Science

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

What should be entered to make the loop print 60 70 80 x = 50 while (x < 80): x = x + ____ print (x)

10

What is output? c = 1 sum = 0 while (c < 10): c = c + 3 sum = sum + c print (sum)

21

What is output? Select all that apply. c = 0 while (c < 5): c = c + 3 print (c)

3, 6

What is output? Select all that apply. c = 0 while (c < 10): c = c + 5 print (c)

5, 10

What is output? Select all that apply. c = 3 while (c < 10): c = c + 2 print (c)

5, 7, 9, 11

We use loops to: Process Numeric Data Ask true/false questions Repeat blocks of code Ask users to enter information

Repeat blocks of code

Which of the following is NOT true about while loops? A way of repeating a code Use conditions like if statements Give us more control over how the program runs Used to find remainders

Used to find remainders

Suppose you change the program in the last problem so the user enters exactly 17 numbers. The loop would then be an example of a(n) _____ loop

count variable

x = int(input ("Enter a value: ")) c = 0 sum = 0 while (c < 10): x = int(input ("Enter a value: ")) c = c + 1 sum = sum + x print ("\n\nSum: " + str(sum)) What type of loop is it?

count variable loop

The two ways to end a loop are:

count variables and user input

While loops should always be ____________,

indented

Setting a starting value for a variable is called _____________.

initializing

This code loops until the user types in 17. It finds and prints the sum of all even numbers entered. n = int(input ("Enter a number, 17 to stop.")) sum = 0 while (n != 17): if (n % 2 == 0): sum = sum + n n = int(input ("Enter a number, 17 to stop.")) print ("Sum: " + str(sum)) This is an example of a(n) ____ loop

user input

Consider the following code: x = 9 sum = 0 while (x < 19): x = x + 1 num = int(input("Enter a number: ")) sum = sum + num print (str(x) + ": " + str(num) + " Total: " + str(sum)) Which of the following is the loop control variable?

x

What is output? Select all that apply. c = 0 while (c < 5): c = c + 1 print (c)

1, 2, 3, 4, 5

Why do we use WHILE loops?

Because of efficiency

Consider the following code: c = 10 while (c > 5): print ("Next number: " + str(c)) c = c - 1 It should print out: Next number: 10 Next number: 9 Next number: 8 Next number: 7 Next number: 6 What is wrong?

The c = c - 1 is not inside the loop. This will cause the loop not to stop.

Consider the following code: num = int(input("Enter a number, negative to stop")) while (num >= 0): print ("You entered: " + str(num)) num = int(input("Enter a number, negative to stop")) print ("Done.") What is wrong? The num >= 0 test condition is not correct There is nothing wrong with the code. The line num = int(input("Enter a number, negative to stop")) should be indented so it is inside the loop There should not be a : after the while (num >= 0)

The line num = int(input("Enter a number, negative to stop")) should be indented so it is inside the loop

Loops are used to _____________ code.

repeat


Kaugnay na mga set ng pag-aaral

environmental science mid term part 4.3

View Set

Linux Essentials Chapter 13 System and User Security

View Set

PSB 4006 - Chapter 8: Wakefulness and Sleep

View Set

Chapter 4 The Stargazer's Guide to the Night Sky

View Set

8. Disk and File System Management

View Set

Chapter 8 Racial and ethnic theories

View Set

English IV — Frankenstein Chapters 7-14

View Set

NCLEX LPN Physiological Adaptation 1

View Set

CITI training responsible conduct

View Set

RMI 3502 Topic 5 - Insurance Regulation

View Set