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?
General Algorithm:
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*
variable assigned the value of the first element in sequenceDefine
Range function
Rather than predefining sequence of values → range function
range(n) gives sequence for numbers 0, 1, … nrange(start, n) produces sequence from start to n -1range(start, n, step) produces sequence from start to n-1 with gap of step variableWhat 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 timefor loop when num of iterations is fixed OR want to preform same operation on all elements of a sequenceDefine
What is variable scope?
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)
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 …
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)
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