Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CMPS 1371 Introduction to Computing for Engineers
CS0004: Introduction to Programming Repetition – Do Loops.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
Logic Structure - focus on looping Please use speaker notes for additional information!
Programming Logic and Design Fifth Edition, Comprehensive
CPS120 Introduction to Computer Programming The Programming Process.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Loops & More 1.Conditionals (review) 2.Running totals and Running products 3.the for loop, examples 4.Nesting for loops, examples 5.Common errors 1.
Repetition Structures
REPETITION CONTROL STRUCTURE
Repetition Structures Chapter 9
Python: Control Structures
CHAPTER 5A Loop Structure
CS1371 Introduction to Computing for Engineers
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
JavaScript: Control Statements.
LOOPS.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Arrays, For loop While loop Do while loop
Introduction to pseudocode
Loops CIS 40 – Introduction to Programming in Python
Repetition Structures
Introduction to Problem Solving and Control Statements
Chapter 6: Repetition Statements
Loop Strategies Repetition Playbook.
Let’s all Repeat Together
Note the rights settings.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Software Development Techniques
Presentation transcript:

Introduction to Loops For Loops

Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations Graphing Logical operations Making decisions based on data What if I were to ask you to perform a computation 10 times? 100 times? 1,000,000 times? Until you find the number I want you to get?

What is a Loop? A loop is a block of code that repeats itself A loop can be programmed to repeat a fixed number of times or it can be programmed to repeat until some condition is no longer met Counter Based Loop For Loop Conditional Based Loop While Loop

Loop Type Comparison Counter Based Runs a predetermined number of times Uses a counter variable Automatically updates the loop variable Conditional Based Runs until some condition is met Uses any variable(s) Loop variable(s) must be updated manually Both are based on a condition, it is simply that the endpoint of the counter based loop is known ahead of time (unless you make a mistake!), whereas the conditional based loop is not

Which loop type would you use? You want to calculate the position of a moving object based on the initial velocity, angle, and time since it was launched, stopping when the object comes to rest. You want to calculate the position of a moving object based on the initial velocity, angle, and time since it was launched, for the first 5 seconds of its trajectory. You need to create a set of 100 random data points to simulate measurements that your new sorting machine might encounter to test the sorting algorithm you’ve developed. You want to collect performance data on a new engine for 1,000,000 revolutions, unless the measurements indicate that there is a problem, in which case you need to turn off the engine. You need to ask the user of your program to input a number, but your program cannot continue unless the value is within a specific range. Conditional Loop Counter Loop Conditional Loop

General Items About Loops Loop condition is only checked once during each iteration Conditional test is based on the loop control variable(s): Initialized prior entering the loop Updated within the loop When the loop control variable(s) take on value(s) that cause the condition to fail, the loop will exit and continue on with the rest of the program The code that is repeated by the loop is called the loop body Each time the loop repeats is called an iteration

FOR loops FOR loops are counter based loops Sample Construction: for control_variable = begin:step:end MATLAB statements end The control variable will count from the beginning value to the end value based on the step size specified It will repeat the statements included inside the loop (between the for and end ) each time it counts Once the control variable is outside of the specified range, the loop will stop repeating

FOR loops FOR loops are counter based loops Sample Construction: for k = 1:1:10 MATLAB statements end k is set to 1 All the MATLAB statements are executed k is incremented to 2 All the MATLAB statements are executed a second time k is incremented to 10 All the MATLAB statements are executed a 10 th time k is incremented to 11 The value of k no longer meets the requirements and the loop terminates

FOR loops Another Sample Construction: for k = 18:-2:0 MATLAB statements end How many times will this loop execute? Answer: 10 times (k = 18, 16, 14, 12, 10, 8, 6, 4, 2, 0) Note: It make absolutely no sense to set up the loop this way unless your program makes some clever use of k that requires it to decrement by 2

Example 1: Simple FOR Loop sum=1; product = 2; for k=1:3 sum = sum + 0.5^k; product = product*2; end ksumproduct ^1 = 1.52*2 = ^2 = 1.754*2 = ^3 = *2 = 16

Example for k = 1:1:5 fprintf('%i \n', k); end The loop control variable, k, is initialized to: tested against the stop value: changed by each iteration Output:

Example for k = 5:-1:1 fprintf('%i', k); end The loop control variable, k, is initialized to: tested against the stop value: changed by each iteration Output:

Example n = 10; s = 2; for k = 1:s:n fprintf('%i \n', k); end The loop control variable, k, is initialized to: tested against the stop value: changed by each iteration Output:

Example for k = 1:0.5:5 fprintf('%0.1f \n', k); end The loop control variable, k, is initialized to: tested against the stop value: changed by each iteration Output:

Comments on FOR loop MATLAB automatically updates the loop control variable (index for the loop – k in our examples) Don’t try to change the loop control variable inside the loop; you wouldn’t want to put the line k = 7 in the loop – it won’t work You can use the loop control variable to determine other variable values Don’t use i and j as index variables in MATLAB because i and j are sqrt(-1) Many other languages commonly use i and j as index variables, but don’t use them here!

Test Your Understanding