Download presentation
Presentation is loading. Please wait.
Published byLorin Fletcher Modified over 9 years ago
1
Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009
3
Overview Writing programs that can repeat operations for loops while loops Reference: Text book Ch 4.
4
A Motivating Example Recall the robot_vision_simulator? It could calculate the service of one cadet, based on their uniform colour. What if we want a robot to monitor its surroundings or act repeatedly for some period of time…?
5
Loops Computer programs can repeat operations using loops This saves the programmer (you!) from writing out the same code multiple times Many programming languages have two or three types of loops. MATLAB has two: for loops while loops
6
for Loops
7
Overview: for Loops “Used when you know how many repetitions are required.”
8
Example: Data Processing % loop to print 10 numbers and their squares for x=1:10 x_squared = x*x; fprintf(‘%d squared is %d\n’, x, x_squared); end
9
Nested Loops % code to print all the times-tables from % the 1-times-table up to the 12-times-table for x=1:12 % repeat for each times-table fprintf(‘\nThe %d-times-table:\n’, x); fprintf(‘-------------------\n’); for y=1:12 % repeat for each entry in table x_times_y = x*y; fprintf(‘%d times %d is %d\n’, x, y, x_times_y); end
10
Beware! Time Complexity Often want to optimse the time it takes code to run For one loop the time complexity is equal to the number n of iterations (repetitions) of the loop Written O(n) In a nested loop the time complexity is number of iterations of the first loop multiplied by the number of iterations of the nested loop: Eg: O(n 2 ) This gets really slow if: n is large there many levels of nesting
11
Demo 1: Using for Loops in Functions A Robot Simulator for Movement
12
Syntax: for Loops for ( counter_variable = range ) code to be repeated goes here end Programmer or function user know the range to use a for loop
13
while Loops
14
Overview: while Loops “Used when you don’t know how many repetitions are required, but you know some condition about when to stop repeating.”
15
Demo 2: Using while Loops in Functions A Robot Simulator for Finding Food
16
Problem Solving With while Loops The bouncing height of a ball is determined by its starting height and elasticity: h t+1 = h t x elasticity 2 Where elasticity is between 0 and 1. How many times will the ball bounce between given starting and stopping heights?
17
From Problem to Program… 1.Understand the problem ‘in English’ 2.Develop a rough solution on paper 3.Implement the solution in MATLAB 4.Test your solution
18
Demo 3: Using while Loops in Functions Bouncing Ball
19
Syntax: while Loops while ( Boolean condition goes here ) code to be repeated goes here end
20
Beware! The Infinite Loop while true fprintf(‘This is a never ending loop\n’); end Beware! You could end up with an infinite loop without such an obvious condition This blatant example is the basis of a simple computer virus or denial of service attack
21
Some More Interesting Robots…
22
Summary After today’s lecture you should be able to: Write functions that can repeat operations Use a for loop Use a while loop
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.