Section 05: Loops Flashcards

Lesson 06 and 07

1
Q

What is a recursive function?

A

Function that calls itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define

Tracing

A

Way of representing how program will work each step of the way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can a while loop be used for input validation?

A
  • Want to check for the validity of the data received

General Algorithm:

  1. Ask user for some data
  2. Check if its valid
  3. As long as data is invalid → go back to step 1; otherwise, continue w program

Therefore, while loop useful

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Structure

If statement:

A

if condition : instructions

Indented block of code is executed once, only if condition evluates true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define and structure

for loop

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define

Range function

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the relation between for loops and while loops?

A

Always replace while loops with a for loop and visa versa

Generally …

  1. While loop when the number of iteraiton is not known ahead of time
  2. for loop when num of iterations is fixed OR want to preform same operation on all elements of a sequence
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define

What is variable scope?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define

Break and Continue statements in loops:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Compare

When to use recursive vs. loop functions?

A

Chose based off of complexity and memory (size of code)

  1. Aim for time efficiency and if num of recursive calls is large ⇒ use iteration
  2. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define

Control flow determines …

A

Control flow determines …

  1. Which part of code should be executed
  2. Which parts of code should be executed only under certain circumstances
  3. Which parts of the code should be executed repeatedly

Control flow is not fixed and can change during execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Structure

Structure of a while loops:

A

while condition:
instruction 1
instruction 2
etc …

Repeatedly executed as long as condition evaluates as True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Defne

Iterations:

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Compare

If statement vs. while loop

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly