Chapter 6: Loops Flashcards
Loops
a program construct that repeatedly executes the loop’s statements (known as the loop body) while the loop’s expression is true; when false, execution proceeds past the loop.
Iteration
Each time through a loop’s statements
While Loop
a construct that repeatedly executes an indented block of code (known as the loop body) as long as the loop’s expression is True.
Sentinel Value
a value that when evaluated by the loop expression causes the loop to terminate.
f’
Ex. print(f’something)
(STILL NEED A DEFINITION!)
Infinite Loop
a loop that will always execute because the loop’s expression is always True. (Usually want to avoid these)
,end=”\n”
default of the end of a print statement.
The thing that goes in between the quotations is what goes in between the next print statement(s)
randint()
A function that provides a new random number
You can make a range by providing the min, max
Loop Variable
Where a programmer uses a variable to count the number of iterations.
range(x, y, z)
a range function where:
x = first number(inclusive) (optional)(Default is 0)
y = ending number(exclusive)
z = integer step value/ increment (optional)(Default is 1)
can go backwards:
Ex: range(5, -5, -1)
-1 specifies that it goes downward
When To Use:
For vs While Loop
For Loop:
Use when you are given a number of iterations
While Loop:
Use when you are not given a number of iterations
Nested Loop
A loop inside of another loop
chr()
converts into a character; not a string
character is like string but can only hold one space
How to make rows and columns
num_rows = int(input()) num_cols = int(input())
for i in range(num_rows):
for i in range(num_cols):
print('*', end=' ') print()
break
a statement that causes the loop to exit immediately.