Top Banner
Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009
22

Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Jan 02, 2016

Download

Documents

Lorin Fletcher
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Flow of Control: Loops

Lecture 9: Supporting Material

Dr Kathryn Merrick

Tuesday 31st March, 2009

Page 2: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.
Page 3: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Overview

Writing programs that can repeat operations

for loops

while loops

Reference: Text book Ch 4.

Page 4: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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…?

Page 5: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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

Page 6: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

for Loops

Page 7: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Overview: for Loops

“Used when you know how many repetitions are required.”

Page 8: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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

Page 9: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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); endend

Page 10: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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(n2) This gets really slow if:

n is large there many levels of nesting

Page 11: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Demo 1:

Using for Loops in Functions

A Robot Simulator for Movement

Page 12: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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

Page 13: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

while Loops

Page 14: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Overview: while Loops

“Used when you don’t know how many repetitions are required,

but

you know some condition about when to stop repeating.”

Page 15: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Demo 2:

Using while Loops in Functions

A Robot Simulator for Finding Food

Page 16: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Problem Solving With while Loops

The bouncing height of a ball is determined by its starting height and elasticity:

ht+1 = ht x elasticity2

Where elasticity is between 0 and 1.

How many times will the ball bounce between given starting and stopping heights?

Page 17: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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

Page 18: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Demo 3:

Using while Loops in Functions

Bouncing Ball

Page 19: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Syntax: while Loops

while ( Boolean condition goes here )

code to be repeated goes here

end

Page 20: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

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

Page 21: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Some More Interesting Robots…

Page 22: Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.

Summary

After today’s lecture you should be able to:

Write functions that can repeat operations

Use a for loop

Use a while loop