Download presentation
Presentation is loading. Please wait.
Published byTabitha Heath Modified over 8 years ago
1
Repetition Looping
2
Types for while do..while
3
for The for loop allows you to iterate through a variable for a specific range of values. You must supply three expressions in a for statement: a variable that is set to an initial value, a conditional statement that determines when the looping ends, and an expression that changes the value of the variable with each loop. For example, the following code loops five times. The value of the variable i starts at 0 and ends at 4, and the output will be the numbers 0 through 4, each on its own line. var i:int; for (i = 0; i < 5; i++) { trace(i); }
5
while The while loop is like an if statement that repeats as long as the condition is true. For example, the following code produces the same output as the for loop example: var i:int = 0; while (i < 5) { trace(i); i++; } One disadvantage of using a while loop instead of a for loop is that infinite loops are easier to write with while loops. The for loop example code does not compile if you omit the expression that increments the counter variable, but the while loop example does compile if you omit that step. Without the expression that increments i, the loop becomes an infinite loop.
7
do..while The do..while loop is a while loop that guarantees that the code block is executed at least once, because the condition is checked after the code block is executed. The following code shows a simple example of a do..while loop that generates output even though the condition is not met: var i:int = 5; do { trace(i); i++; } while (i < 5); // output: 5
8
Break 12341234 var num:Number; do { num == Math.random(); } while (num != 0.42); break is a sort of panic button for loops. Using it will cause the loop to simply end its execution, no matter how true the condition is. break can be useful for building in fail-safes in while and do loops, since those can be prone to infinite loops. Consider: This do loop, while not technically an infinite loop, will most likely trigger the script timeout limit before ever generating exactly 0.42. We can build a safety mechanism like so: 123456789123456789 var num:Number; var loopCounter:int = 0; do { num == Math.random(); loopCounter++; if (loopCounter > 500) { break; } } while (num != 0.42);
9
Exercise Solve only one of the following problems. Create a suitable GUI to display your solutions. 1. Create a program to solve the following problems: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
10
2. Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,... By considering the terms in the Fibonacci sequence whose values do not exceed 1000, find the sum of the even-valued terms.
11
3. The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 1000?
12
4. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
13
5. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.