Structured Programming
Stack
(which is really just a sequence) of single selection decision logic structures.
A dual selection decision always has four main parts:
-a decision header, -followed by a body of instructions to execute if the condition evaluates to True, -followed by an else clause, -followed by a body of instructions to execute if the condition evaluates to False.
Syntax for writing else-if ladders:
<statements listed before decision> if <boolean_expresion_1>: <statements to execute when condition 1 is True> elif <boolean_expression_2:> <statements to execute when condition 2 is True> <any number of additional elif clauses> else: <statements to execute when all conditions are False> <statements listed after decision>
Flowchart
A graphical representation of an algorithm that uses few words and simple symbols to make a process easier to understand.
Sentinel Controlled Loop
A loop that runs until a given condition becomes true.
Iteration (or loops)
Allow us to execute a set of instructions more than once.
The condition for a decision is always an expression that evaluates to a boolean value: True or False.
If the conditional expression evaluates to True, then the instructions that follow it will be executed. If the conditional expression evaluates to False, then the instructions that follow it will be skipped.
Single selection decision
Is a decision to execute a set of instructions or skip that set of instructions.
Else-if ladder
Nested ifs formatted a standard way for multiple decision branching.
All programs are structured from these three basic pieces:
Sequence, Decision, and Iteration.
Dual selection decision
We can get the computer to make a decision to execute one of two sets of instructions, "do this" or "do that."
The most basic pattern for a recursive function is to break the function's body into two sections:
a base case, and a recursive case.
A single selection decision always has two main parts:
a decision header, and a body.
Whenever you notice a lot of redundancy in code, this is a clue that we might be able to simplify the code with:
a loop (or perhaps a function, or both).
A while loop always has two main parts:
a loop header, and a body.
indefinite loop
a loop that repeats an indefinite number of times.
While loop
a programming construct used to repeat a set of commands (loop) as long as (while) a boolean condition is true
We can stack and nest flow control structures (sequences, decisions, and loops) to compose more complex:
algorithms
A ____ is always an indented list of statements (instructions). A ____ can be any sequence of statements.
body
The decision body is always an indented list of statements (instructions). The body:
can be any sequence of statements.
Each recursive call should be ___________ to the base case.
closer
Recursive structure syntax:
def recursive_function(<parameters>): if <base_case_condition>: <do_base_case> else: <do_recursive_case>
Sequence is the ________.
default
Common pattern count = count + 1 is called a counter - because
each time you execute this line of code, it modifies a variable by adding a fixed (constant) amount.
A single selection decision is often simply called an:
if statement
Example of a dual selection decision structure:
if user_answer == 'yes': print(' (\\(\\ ') print(' (-.-) ') print(' O_(")(") ') else: print("You did not say 'yes', ") print('so no bunny for you.')
The decision header is always the keyword _________, which is followed by a __________ __________ called the ___________, which is followed by a colon (:).
if, boolean expression, condition
A dual selection decision is often simply called an
if-else statement.
Counter controlled loops
initialize a variable (used to keep track of the current count) before the loop begins. The condition of the loop is dependent on that counting variable value reaching or crossing some boundary (or termination) value. The body of the loop modifies the value of the counting variable in some way (usually by either adding or subtracting a value to/from the variable) to insure that the counter variable eventually reaches or crosses the boundary (or termination) value.
Decision structures are still just instructions, and they can be used anywhere any other instruction can be used:
inside of functions, before and after other decisions, and even in the body of another decision.
Single selection decision structure
instructs the computer to either print or skip those lines. Ex. if user_answer == 'yes': print(' (\\(\\ ') print(' (-.-) ') print(' O_(")(") ')
Loop body
is always an indented list of statements (instructions). The body can be any sequence of statements.
Loop header
is always the keyword while, which is followed by a boolean expression called the condition, which is followed by a colon (:).
Branch
is just an extra path outside of the normal path of execution.
The key feature of a counter
is the fact that we are adding the same amount each time we execute the counter.
Recursion
is when a function calls itself.
The recursive case:
is when the function will call itself again.
Flow control structures allow us to _____________ sequence.
manipulate
stack (or sequence) loops
one after the other
nest loops
one inside the other
As the computer is executing your program, it keeps track of the currently executing instruction with a
program counter
A function will always _________ immediately after one of its __________ statements executes.
return
The flowchart has the advantage of directly representing the flow (the ___________ of steps) through the algorithm because the arrows show explicitly which instruction(s) follow each other's instructions.
sequence
A program is a list (or __________) of statements (or _________) to be executed by a computer.
sequence, instructions
A decision allows us to do:
something special under special conditions.
For non-mutually exclusive options, a ______ of single selection decisions makes the most sense.
stack
When we need to make multiple decisions, one solution is to combine single selection decision structures by:
stacking them, or sequencing them.
Sequence
the computer will execute instructions in the order or sequence they are listed from top to bottom.
Sequential instructions are executed in:
the order they are listed.
A factorial is
the product of all numbers up to a given number
A counter controlled loop is sometimes called a definite loop because
they always repeat a definite number of times.
With a dual selection decision nested inside of another dual selection decision, we can select one of _____ options.
three
else-if-ladder nested inside of a while loop:
users_guess = -1 secret_number = 42 while users_guess != secret_number: users_guess = int(input('Guess my secret number: ')) if users_guess == secret_number: print('Your guess is correct!') elif users_guess > secret_number: print('Your guess is too high.') else: print('Your guess is too low.')
Accumulator Pattern
we are adding (or subtracting) a changing amount (a variable amount).
The ability to make decisions and select and execute appropriate behavior accordingly is:
what makes computers so powerful.
The base case is:
when the function will not call itself again.
Dual selection decisions should be used when options are mutually exclusive; in other words,
when you want to do one of two behaviors based on a condition, but never both.