Section 05: Loops Flashcards
Lesson 06 and 07
What is a recursive function?
Function that calls itself
Define
Tracing
Way of representing how program will work each step of the way
How can a while loop
be used for input validation?
- Want to check for the validity of the data received
General Algorithm:
- Ask user for some data
- Check if its valid
- As long as data is invalid → go back to step 1; otherwise, continue w program
Therefore, while loop useful
Structure
If
statement:
if condition :
instructions
Indented block of code is executed once, only if condition evluates true
Define and structure
for
loop
for variable in sequence:
instructions
*Indented block of code executed repeatedly, once for each element in a sequence*
- First iteration:
variable
assigned the value of the first element in sequence - Successive Iteration: variable assigned value next element
Define
Range
function
Rather than predefining sequence of values → range function
-
range(n)
gives sequence for numbers 0, 1, … n
-
range(start, n)
produces sequence from start to n -1 -
range(start, n, step)
produces sequence from start to n-1 with gap of step variable
What is the relation between for
loops and while
loops?
Always replace while
loops with a for
loop and visa versa
Generally …
-
While
loop when the number of iteraiton is not known ahead of time -
for
loop when num of iterations is fixed OR want to preform same operation on all elements of a sequence
Define
What is variable scope?
- Innermost possible scope: Function Body
- therefore, Variable created inside body of a loop/conditional statement → variable can be accessed later = assuming the body was executed at least once
Define
Break
and Continue
statements in loops:
Break
statement cause program to exit the current loop
Continue
statement cause program to skip to next iteration
Give more control over loop execuation → make code more difficult to read and debug
Compare
When to use recursive vs. loop functions?
Chose based off of complexity and memory (size of code)
- Aim for time efficiency and if num of recursive calls is large ⇒ use iteration
- Aim efficiency is not an issue and having a shorter code important ⇒ use recursion
Recursion is when a function calls itself within its code, thus repeatedly executing the instructions present inside it.
Iteration is when a loop repeatedly executes the set of instructions like “for” loops and “while” loops.
Define
Control flow determines …
Control flow determines …
- Which part of code should be executed
- Which parts of code should be executed only under certain circumstances
- Which parts of the code should be executed repeatedly
Control flow is not fixed and can change during execution
Structure
Structure of a while
loops:
while condition:
instruction 1
instruction 2
etc …
Repeatedly executed as long as condition evaluates as True
Defne
Iterations:
Iternation: single execution of the instructions in the body of the loop (#num of repetitions)
- Variable acts as loop counter
- No upper limit to iternations: infinite loop
Compare
If statement vs. while loop
If Statement:
* Conditions checked once (before executing body)
* Block of code is executed at most once, if condition evaluates as true
While Loop:
* Conditions checked once per iternation of the loop
* Block of code is executed repeatedly, as long as condition evaluates as true