Repetition Part 2 Flashcards
Dont use a loop for
alternative choices/ course of action
If you have a loop and it should be a branch, you will have a
logic error
Pseudo Code
Looks like a code but just english like statements
Types of nesting (3)
branch within a loop
Loop within a branch
loop within a loop
Branch in a loop:
(2 ex)
Ex: assignment A1 (We have a loop for each room and the branches are the menu)
- ex: while the user enter an invalid value for age ( too high/low) an error message is displayed
Loop in a branch:
loop in a loop
ex
modulo operator %
ex+what it is
The modulo operation (abbreviated “mod”, or “%” in many programming languages) is the remainder when dividing. For example, “5 mod 3 == 2” which means 2 is the remainder when you divide 5 by 3.
The break instruction (2)
When is it used?+ what does it add?
- Used to terminate the repetition of a loop which is seperate from the main Boolean expression
- Adds another exit point (makes it hard to trace)
Alternatives to a break
- add the second Boolean expression as part of the loop’s main boolean expression (while …. and NOT…..) instead of (while… if … break)
- Use a flag
Infinite loops (3)
What they are+ caused by +ex
- loops that never end
- ## Can be caused by logical by not error in loop control