CSCI N201: Programming Concepts Copyright ©2005 Department of Computer & Information Science Introducing JavaScript Loops
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Goals Understand how to create while loops in JavaScript.Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript.Understand how to create do/while loops in JavaScript. Understand how to create simple for (counting) loops in JavaScript.Understand how to create simple for (counting) loops in JavaScript.
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Computing Structures Remember, last time we identified that all executable statements fall into one of three categories:Remember, last time we identified that all executable statements fall into one of three categories: –Sequential Structures – those structures where instructions happen in sequence. That is “A before B”, “B before C”, “C before D”, etc. –Looping Structures – Today’s unit. –Decision (Selection) Structures – those structures where code alternate executes based on some Boolean test.
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science What is a Loop? A loop is a programming structure that contains code that will repeat until that code causes something to happen to satisfy or to not satisfy a given condition, thus ending the loop.A loop is a programming structure that contains code that will repeat until that code causes something to happen to satisfy or to not satisfy a given condition, thus ending the loop. There are two basic “families” of loops:There are two basic “families” of loops: –Conditional Loops: Loops that depend solely on some type of Boolean test. –Iterative Loops (a.k.a. For, For … Next loops): Loops that depend on matching a maximum or minimum number of iterations (repetitions).
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Parts of a Loop All loops share some basic parts. These include:All loops share some basic parts. These include: –Condition to test: This can be a Boolean test (for conditional loops) or a test against a maximum or minimum integer value (for iterative loops). –Executable block: The block of code that will execute so long as the loop has/hasn’t yet satisfied the condition tested. –A way to end the loop: In terms of syntax, this is not necessary, but forgetting to include the method for ending the loop somewhere (usually the executable block) results in an endless loop.
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Conditional Loops Conditional loops are based on some type of Boolean test.Conditional loops are based on some type of Boolean test. Conditional loops are useful when the loop’s executable block should execute for an indeterminate length of time. You, as the programmer, don’t know how many times the executable block will execute in practice.Conditional loops are useful when the loop’s executable block should execute for an indeterminate length of time. You, as the programmer, don’t know how many times the executable block will execute in practice.
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Conditional Loops Conditional loops are often defined by where the condition is written, in reference to the executable block:Conditional loops are often defined by where the condition is written, in reference to the executable block: –Pre-test loops: The condition is located before the executable block. There is a possibility that a pre-test loop may never execute. In JavaScript, the while loop is a type of pre-test loop. –Post-test loops: The condition is located after the executable block. A pre-test loop’s executable block will always execute at least once. In JavaScript, the do … while loop is a type of post- test loop.
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science The “while” Loop Allows repeatable code until a given condition is metAllows repeatable code until a given condition is met Useful when the program should go on for an indeterminate length of timeUseful when the program should go on for an indeterminate length of time Must have a way of terminating the structure from within the loop!Must have a way of terminating the structure from within the loop! Pre-Test Loop (Loop may NEVER execute)Pre-Test Loop (Loop may NEVER execute)
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science 3 Basic Parts of a While Loop The condition being testedThe condition being tested The action to be performed while the condition is evaluated to be trueThe action to be performed while the condition is evaluated to be true A method by which the condition can be evaluated to be false, ending the while loopA method by which the condition can be evaluated to be false, ending the while loop
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science While Loop – Everyday Example while (sign is not a stop sign) { keep driving; recognize next sign; } what to do while true condition to test what to do while true how to stop
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science While Loops and User Control Many while loops have a definitive ending specified in the condition programmed by the programmer (i.e. – counters)Many while loops have a definitive ending specified in the condition programmed by the programmer (i.e. – counters) However, most of the time we need code to repeat until the user (not the programmer) decides when it’s time to quitHowever, most of the time we need code to repeat until the user (not the programmer) decides when it’s time to quit You can write a program like this using a combination of while and if/else structuresYou can write a program like this using a combination of while and if/else structures
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Integrating While & If Structures Code Example 1 Code Example 2
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science The “do/while” Loop Allows repeatable code while a given condition is true.Allows repeatable code while a given condition is true. Useful when the program should go on for an indeterminate length of timeUseful when the program should go on for an indeterminate length of time Must have a way of terminating the structure from within the loop!Must have a way of terminating the structure from within the loop! Post-Test Loop (Loop always executes AT LEAST ONCE)Post-Test Loop (Loop always executes AT LEAST ONCE)
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Basic Parts of a Do/While Loop The action to be performed AT LEAST ONCE and while the condition is evaluated to be trueThe action to be performed AT LEAST ONCE and while the condition is evaluated to be true The condition to testThe condition to test A method by which the condition can be evaluated to be false, ending the while loopA method by which the condition can be evaluated to be false, ending the while loop
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Do/While Loop – Everyday Example do { keep driving; recognize next sign; } while (sign is not a stop sign); what to do while true condition to test how to stop
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science The “for” Loop AKA – The Counting Loop OR Counter-Controlled LoopAKA – The Counting Loop OR Counter-Controlled Loop Allows repeatable code until in an iterative loop for a finite amount of times.Allows repeatable code until in an iterative loop for a finite amount of times. Useful when the program runs for a pre- determined length of time.Useful when the program runs for a pre- determined length of time. Must have a way of terminating the structure from within the loop’s header (test against an maximum or minimum value).Must have a way of terminating the structure from within the loop’s header (test against an maximum or minimum value).
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Parts of a For Loop The conditions for the testThe conditions for the test –Starting point for counter –A maximum/minimum value –A way to increment/decrement the counter The action the loop should perform while the counter hasn’t met the max/min.The action the loop should perform while the counter hasn’t met the max/min.
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science For Loop – Example for(var i=0; i<100; i++) { keep driving; } what to do when i<100 Starting Point Maximum Value How to Increment
CSCI N201: Programming Concepts Copyright ©2004 Department of Computer & Information Science Questions?