Download presentation
Presentation is loading. Please wait.
Published byJack Warren Modified over 8 years ago
1
PROGRAMMING ITERATION 2
2
Starter Put the following code in order (write down the line numbers). The program should display the numbers 1-24 on screen. 1.Console.writeline(pos) 2.Pos = pos + 1 3.Pos = 1 4.Loop 5.Do while pos < 25 6.Dim pos as integer Answer: 6, 3, 5, 1, 2, 4
3
Objectives Understand what is meant by iteration in programming. Explore different types of iteration: DO loops and FOR loops. Understand the use of iteration in programming.
4
Recap: What is Iteration? Iteration is simply the repetition of a sequence of (one or more) statements. In programming, this is often referred to as a loop. The statements might not be executed at all (zero repetitions), or may be executed at least once. Eventually, something must stop the repetition, allowing the program to continue further.
5
I know the number of iterations! The good thing about DO loops is that you do not need to know how many times you need to loop. But what if you do know? What if you know you need to loop through code a fixed number of times (e.g. 10?) To do this, you can use a FOR loop. The basic syntax is: For controlvar = startval To endval statement(s) Next controlvar Animated FOR explanation. Let’s loop at a couple of coded examples.
6
What if you want to step in different values? What if you don’t always want to go up in 1’s? E.g. What if you wanted to print the odd numbers in 1-9. You would need to go up in 2’s! To do this, you just add the STEP keyword into your FOR declaration. For controlvar = startval To endval Step stepval statement(s) Next controlvar Again, let’s go back and look at some coded examples.
7
Today’s Task Have a go at the For Loop worksheet on the intranet.
8
Objectives Understand what is meant by iteration in programming. Explore different types of iteration: DO loops and FOR loops. Understand the use of iteration in programming.
9
Plenary Let’s go through your programming tasks. Explain the difference between: – FOR LOOPS – FOR..STEP LOOPS – DO…LOOPS – When would you use each of the above?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.