B 065 PROGRAMMING ITERATION 2 Starter Put the
B 065: PROGRAMMING ITERATION 2
Starter Put the following code in order (write down the line numbers). The program should display the numbers 1 -24 on screen. 1. 2. 3. 4. 5. 6. Console. writeline(pos) Pos = pos + 1 Pos = 1 Loop Do while pos < 25 Dim pos as integer Answer: 6, 3, 5, 1, 2, 4
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.
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.
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.
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.
Today’s Task • Complete the remaining questions in Task 14: For…Next Loops. • Use your programming guide to help you.
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.
Required Reading • Each week you will be given required reading. • If you fail to do this, you will 100% find the lessons which follow it EXTREMELY difficult. • Before next lesson you should have read: • Pre-reading 30 -38
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?
- Slides: 10