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

Slides:



Advertisements
Similar presentations
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Advertisements

Boolean Algebra Lecture 7: Supporting Material Dr Kathryn Merrick Tuesday 24 th March, 2009.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Code Clichés and Conventions Lecture 10: Supporting Material Dr Kathryn Merrick Thursday 2 nd April, 2009.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Graphical Programming Languages Lecture 19: Supporting Material Dr Kathryn Merrick Thursday 21 st May, 2009.
Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Programming with Spread Sheets Lecture 18: Supporting Material Dr Kathryn Merrick Tuesday 19 th May, 2009.
CS 100 Introduction to Computing Seminar October 7, 2015.
Control Structures RepetitionorIterationorLooping Part I.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Logical Thinking CS 104 9/12/11. Agenda Today  College Board survey reminder  Note: Simple “how to” guide on Scratch posted on eLearning  Review HW.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
COMP Loop Statements Yi Hong May 21, 2015.
Algorithms and Pseudocode
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Learning Objectives 1. Understand how the Small Basic Turtle operates. 2. Be able to draw geometric patterns using iteration.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Adapted from slides by Marty Stepp and Stuart Reges
Trace Tables In today’s lesson we will look at:
Control Flow (Python) Dr. José M. Reyes Álamo.
Programming Fundamentals
Repetition Structures Chapter 9
Topics Introduction to Repetition Structures
Chapter 5: Repetition Structures
Topics Introduction to Repetition Structures
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter (3) - Looping Questions.
Loops CIS 40 – Introduction to Programming in Python
Introduction to Algorithms and Programming
Building Java Programs
Building Java Programs
Let’s all Repeat Together
Introduction to Repetition Structures
Building Java Programs
Building Java Programs
Topics Introduction to Repetition Structures
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Programming Techniques
Presentation transcript:

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.

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

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

for Loops

Overview: for Loops “Used when you know how many repetitions are required.”

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

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

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

Demo 1: Using for Loops in Functions A Robot Simulator for Movement

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

while Loops

Overview: while Loops “Used when you don’t know how many repetitions are required, but you know some condition about when to stop repeating.”

Demo 2: Using while Loops in Functions A Robot Simulator for Finding Food

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?

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

Demo 3: Using while Loops in Functions Bouncing Ball

Syntax: while Loops while ( Boolean condition goes here ) code to be repeated goes here end

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

Some More Interesting Robots…

Summary After today’s lecture you should be able to: Write functions that can repeat operations Use a for loop Use a while loop