Loops and Conditions
Loop: This is when a set of instructions, or an algorithm, is repeated. To repeat an action a hundred times, it would require having to write a hundred lines of code to repeat the action! Fortunately, there is a way to tell a computer to repeat an action without writing a separate line of code for each repetition. For example: Box of candy Repeat (until empty) EAT CANDY.
Nested loop: This is a loop within a loop. For example: Shampoo Repeat (until soapy) LATHER, THEN RINSE.
Condition: Programmers code conditional statements to get computers to react to different situations. They’re written with IF/ELSE statements: IF something happens, THEN do this. Or ELSE, do something else. For example: IF full, THEN stop eating, ELSE keep eating.
IF statement: In a condition, something happens when the IF condition is met.
ELSE statement: In a condition, when an IF action isn’t met, the ELSE action will run.
So, let's think about these things...
... in context of your morning routines. Remember we talked about morning routines last time? And we said morning routines are algorithms? What's a condition or loop from you morning routines?
... and in context of the PB&J algorithm!
NOTE: What we are doing is called pseudocode!
Spread the glob!
Spread the peanut butter. Spread the peanut butter. Spread the peanut butter. Spread the peanut butter. Spread the peanut butter. Spread the peanut butter. Spread the peanut butter. Spread the peanut butter. Spread the peanut butter. Spread the peanut butter.
What's up with bread!
Is the bag of bread already open? Is it already sliced? Are you using the butt of the bread? We need to know!