While loops and Nested Loop
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!")