Repetition part 1 Flashcards
Pseudo Code
fake code
write me a sentinel loop that add all the numbers tgt
Loop in a branch example
Branch in a loop example
end=” “ vs end=””
1 odd and 1odd
’<’ not supported between
instances of ‘str’ and ‘int’
wrire a code that adds all the sum of the number u enter before it. For ex: enter 4 (1+2+3+4)
When there is no branching/loops, the code runs….
Top to bottom (Sequential)
whether or not a part of the program repeats is determined by
a loop control
For loops: (2)
What it can do
- can count through a numerical sequence (1,2,3)
- iterate the sequence of characters in a string
For loops can only count trhough a
mumerical sequence through a sequence of addition or subtraction
While loop (2)
when it can be used
can be used any time repetition is needed
- can be used if its not know in advance how many times that loop will repeat
give me a countdown while loop example:
you cannot implement —– in For loop
logic
The only mathmatical operation you can do with For loop is
addition/subtraction
give me a countdown for loop
Erroneous FOR loops (2)
What it is +ex
the logic of the loop is sucg tgat the end condition has already been reached with the start condition
- ex: when the programmer combine a loop that combines counting up with a loop that counts down
while loop counting up/down pseudo code
starting value
end value
counting up/down by
Sentinel controlled loops
when does it stop?
the stopping consition for the loop occurs when the sentinel value is reached (less then 0)
Post Test Loops (3)
Python+when to use (2)?
- python doesnt have one
- used if you need a loop to always execute at least once even if the Boolean expression evaluated to false the first time that the loop is encountered.
- You want to check the body of the loop before checking the stopping condition
Post test loop are guaranteed execution because:
The Boolean expression is checked after the body executes
Give me an example of a post test loop
How to make a post test loop
- prime the loop control so its guarantee execution
Ex: age<1 (LOOP CONTROL)
while (age<0)
print(“Type in answer”)
Pre- test loop
When is it used?
- you want the stopping condition to be checked before the loop body is executed (Used when you want a loop to execute zero or more times)
Pre- test loop executes
+ ex:
zero or more times
age = 12
while (age< 0):