Presentation is loading. Please wait.

Presentation is loading. Please wait.

BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while.

Similar presentations


Presentation on theme: "BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while."— Presentation transcript:

1 BO65: PROGRAMMING ITERATION 1

2 Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while intCounter <10 console.writeline(intCounter) intCounter = intCounter+1 Loop

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 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 Why bother with iteration? Imagine an array of 1000 entries, each containing a payment due from customers. At the end of the month, £20 is added on to each customer. You would need to write at least 1000 lines of code to do this. You could just loop through the array and add it on in about 6 lines of code! Imagine counting the number of letter “e’s” which appear in this slide. This would involve you going through the slide and checking each word, then adding 1 to a tally every time you come across one. Alternatively, a loop could go through the slide and do this for you. A loop is used when you are performing repetitive tasks.

6 Being Cautious About Loops The distinction between checking before starting any processing, and checking at the end of each run through the statements is a fundamental one. Putting the check in the wrong place is one of the commonest causes of errors when dealing with loops in programs. For example: – Would you issue a bill to a customers BEFORE checking if they owed anything or would you check each customers balance before printing the bill.

7 DO Loops We start by looking at loops where the number of repetitions (which might possibly be zero, remember) is determined by checking some condition. These are known as "Do Loops.” By “Do Loop” we just mean “Do this, if something is true or false.” For example “If there is a sale, go through each item in stock and deduct 10%.” When using “Do loops,” there are two places where we might choose to do the check: – before starting each repetition, i.e. at the beginning of the loop – just after finishing each repetition, i.e. at the end of the loop If we check the condition at the end of the loop, we are certain to execute the statements at least once.

8 Using a DO Loop You simply tell the computer to do something whilst a condition is true. It then cycles through the loop until the condition becomes false. The condition is checked EVERY cycle. Do While condition statement(s) Loop Simple examples of such conditions might be: - intOne = 0 total < 100 (intCount < 250) And (strName = "Smith") Let’s look at some examples.

9 Variations of the DO Loop Do Until … Loop - condition tested before first entry to the loop Do Until strName = “chris” console.clear() console.writeline(“Enter your name”) strName = console.readline() Loop Do … Loop Until … - condition tested after first execution of the loop Do console.clear() console.writeline(“Enter your password”) strPassword = console.readline() Loop until strPassword = “chaching” Do … Loop While … - condition tested after first execution of the loop

10 Today’s Task Loops are a very difficult concept to grasp. We will work through some activities, but will go through the first one together, writing an algorithm for it. Prep tasks: Complete questions in Task 14 Ready to program? Complete the programming tasks in Task 14: Loops in Programming Tasks.

11 Task 2 Now you have written the code to each task, try changing the type of loops and see what happens. E.g. if you have used a Do…Loop Until loop, change it to a Do While…Loop, and note down how it is different. Can you explain it?

12 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

13 Plenary Let’s go through your programming tasks. Each person will come up and ask to show their solution to each of the loops programming tasks.


Download ppt "BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while."

Similar presentations


Ads by Google