Uploaded by asdad daad

4- ForLoops

advertisement
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Entry routine
1. Stationery out – you should have your own equipment
2. Exercise books out
3. Planners out
4. Bags underneath table
5. Login to computer account
6. Do Now Task
7. Todays title: For Loops (Counter-Controlled)
8. Draw on the confidence scale where you think you are with the objectives
Reflection
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Starter Activity – A Task to Do
Now
You may not
know all of
these… but have
a go and note
down the
answers
afterwards
Reflection
Listening
Questioning
Collaboration
Communication
Risk-Taking
Starter Activity Answers
Persevering
Planning
Reflection
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Reflection
Objectives/Outcomes
Syllabus
Todays title: FOR Loops (Counter-Controlled)
By the end of this lesson you will be able to:
a) Identify the key parts of a FOR loop
b) Describe what a FOR loop is for
c) Explain how a FOR loop works
d) Use a FOR loop in Visual Basic
Plot on the scale to show how confident you feel having looked at the objectives of the
lesson.
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Reflection
Programming Constructs
Three programming constructs that we always program with:
1. Sequence
2. Selection
3. Iteration
Each line of code is executed one line after the other – in sequence
IF statements
SELECT Case statements
FOR Loops
Conditional Loops (While loops, Do/Until Loops)
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Loops
A loop allows us to perform a common programming task multiple times in a more
efficient manner (using fewer lines of code). Advantage: reduces programming time.
E.g. Outputting hello ten times
Without Loop:
With Loop:
Reflection
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Reflection
FOR Loop
A FOR loop is a counter-controlled loop; meaning that it iterates (repeats) a
fixed number of times.
Every FOR loop starts with
the word ‘FOR’
This is a variable. We usually call it ‘counter’ but it doesn’t have to be.
It does not need to be declared, and only exists within this loop.
This tells us the values that counter will
start and end at. In this case, it will start at
1 and end at 10. It increases by 1 every
time the program reaches the word ‘Next’.
The loop end when ‘counter’ reaches 10.
And ends with the word ‘Next’.
(When the program reads the
word next, it jumps back up to
the beginning of the FOR loop).
The code to be repeated
Write code and summarise text
How many times does this loop iterate (repeat)?
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Reflection
For loop questions
How many times do the following loops iterate? (write questions and answers in books)
1. For counter = 1 to 8 8 times (Counter  1,2,3,4,5,6,7,8)
2. For counter = 3 to 7 5 times (Counter  3,4,5,6,7)
Self – Assess
/7
3. For counter = 0 to 5 6 times (Counter  0,1,2,3,4,5)
4. For counter = 0 to 1 2 times (Counter  0,1)
5. For counter = 1 to 10 Step 2 5 times (Counter  1,3,5,7,9)
6. For counter = 5 to 0 Step -1 6 times (Counter  5,4,3,2,1,0)
7. For counter = 1 to 10 Step 5 2 times (Counter  1,5)
Hint: ‘Step’ tells us how much counter will change by every time the program
reaches the word ‘Next’. By default it is +1 (and therefore we don’t write Step 1)
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Reflection
For loop Tasks
1. Write a program that outputs the phrase ‘Red lolly, Yellow lorry’ 10 times
using a loop.
2. Write a program that outputs the value of ‘counter’ 5 times in a loop.
3. Write a program that outputs the words “The value of counter is: ” and the
value of counter at that point in the loop (5 times)
(E.g. In the third iteration it should output ‘The value of counter is: 3’)
4. Write a program that outputs the words “The value of counter is: ” and the
value of counter at that point in the loop (5 times) IN REVERSE ORDER
5,4,3,2,1,
Listening
Questioning
Collaboration
Communication
Risk-Taking
For loop task ANSWERS
Task 1:
Task 3:
Task 2:
Task 4:
Persevering
Planning
Reflection
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Reflection
Programming Task
Write a program that asks the user to enter a number between 1 and 3. Keep
adding one to their value ten times. Each time you add one, it should check if
the value is:
- equal to 3 (If it is, then output the words ‘your value is now 3’)
- equal to 6 (if it is, then output the words ‘your value is now 6’)
- equal to 9 (if it is, then output the words ‘your value is now 9’)
- equal to 10 (if it is, then output the words ‘Final value is 10’)
Hint: To add 1 to a variable:
Score = Score + 1
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Programming Task Answers
OR
Planning
Reflection
Listening
Questioning
Collaboration
Communication
Risk-Taking
Persevering
Planning
Reflection
Syllabus
Plenary - Self Evaluation
By the end of this lesson you will be able to:
a) Identify the key parts of a FOR loop
b) Describe what a FOR loop is for
c) Explain how a FOR loop works
d) Use a FOR loop in Visual Basic
Plot on the scale to show how confident you feel having looked at the objectives of the
lesson.
Download