While loops and Nested Loop

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

What's the difference between a while loop and a for loop?

A for loop runs a set amount of times, and a while loop will run as long as its conditions is True

While Loops

A loop that repeats as long as a conditional statement within them are True.

A while loop that uses a conditional statement that will always be true is called _____

An Infinite Loop

How to Exit an Infinite Loop

Ctrl + C (Windows)

What keys do you press to get out of an infinite loop?

Ctrl + C (windows)

Describe how a tab(or 4 spaces) is used in nested loops

Tabs (or 4 spaces) are used to show which loop is inside the other. An additional tab or 4 spaces shows that the code is nested inside another inner loop

Nested Loop

When one loop is put inside another loop

Infinite Loops

While Loops that use a Boolean statement that can never become false

Write a loop that will give the following results: C. The program will count from 7 to 11 by twos 3 times

for i in range(3): for j in range(7,11,2): print(j) *variable names may be different

While Loop Example

password = None while password != "myPassword1234": password = input("enter the password: ") if password != "myPassword1234": print("Your password is incorrect.") print("Correct password. Welcome.")

Infinite Loop Examples

while True: while 4>3: while "hello" == "hello" while 5 <= 5:

Write a loop that will give the following results: D. The program will count to 5 over and over again, forever

while True: for i in range(1,6,1): print(i) *variable names may be different

Write a loop that will give the following results: B. The program will print: Good morning, Steve as long as the variable "name" is equal to "Steve"

while name == "Steve": print("Good morning, Steve")

Write a loop that will give the following results: A. The program will print: Go! Go! Go! as long as the variable "x" is more than 50

while x > 50: print("Go! Go! Go!")


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

MKTG320: Chapter 2 Quiz McGrawHill

View Set

Chapter 69: Management of Patients With Neurologic Infections, Autoimmune Disorders, and Neuropathies

View Set

Chapter 45: Caring for Clients with Disorders of the Upper Gastrointestinal Tract

View Set

Quiz: Stories of Personal Exploration

View Set

4.1 Bevezetés a vállalati stratégiába

View Set

CIS 312 Chapter 2 - Assessing Risk

View Set