11
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Embed Size (px)

Citation preview

Page 1: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

New Mexico Computer Science For All

More Looping in NetLogoMaureen Psaila-Dombrowski

Page 2: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Types of Loops•Already know that there are three

common types of loops in most programming languages:

▫Infinite Loops▫Counted Loops▫Conditional Loops

Page 3: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Components of a Loop

•Loops have some properties in common:▫Body of the loop: The portion of the code

that is repeated.▫Iteration: Number of times the loop is

executed

•Some types of loops have other properties▫Loop Variable: A variable that is

incremented during the looping process▫Loop Condition: A conditional (true of

false) statement that is checked during looping

Page 4: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Infinite Loops•An infinite loop repeats the body of the

loop until one of the following occurs:▫A“stop” command in the program▫Manually turned off.▫A runtime error.

•Already familiar with this in NetLogo

Page 5: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Counted Loops

•Repeated a fixed maximum number of times.

•Must know how many times

▫Before programming▫Be able to calculate maximum number of

times

Page 6: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Counted Loops in Netlogo

•Netlogo has a specific command for counted loops, the REPEAT command

repeat Nnumber [ commands

]

•The commands in the square brackets [] are repeated Nnumber of times.

Page 7: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Example of Counted Loops Netlogo

Page 8: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Conditional Loops

•Repeated while a certain conditional statement is true.

•Must establish the condition statement initially and it must be true for the loop to start

•The condition must become false during the looping process in order for the loop to stop, otherwise the loop becomes infinite.

Page 9: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Conditional Loops in Netlogo

•Netlogo has a specific command for conditional loops, the WHILE command

while [condition] [ commands ]

•The commands in the square brackets [] are repeated while the condition is true.

•When the condition becomes false, the loop is exited.

Page 10: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Example of Conditional Loop in Netlogo

Page 11: New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski

Summary• Three types of loops

▫ Infinite Loops

▫Counted Loops- the REPEAT command

repeat Nnumber [ commands

]

▫Conditional Loops - the WHILE command

while [condition] [ commands ]