Loops CS 103 February 13, 2009 Author: Nate Hamm.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

How SAS implements structured programming constructs
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
ITC 240: Web Application Programming
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Copyright © Texas Education Agency, Computer Programming For Loops.
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
CS0007: Introduction to Computer Programming Introduction to Arrays.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Loops Doing the same thing over and over and over and over and over and over…
Engineering 1020 Introduction to Programming Peter King Winter 2010.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
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.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Loops CS 103 February 19, 2007 Presented by Nate.
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.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
For loops in programming Assumes you have seen assignment statements and print statements.
PHP Constructs Advance Database Management Systems Lab no.3.
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.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab Programming for Engineers
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Loops CS 103 February 23, Review FOR is a control construct that provides a loop for ii = 1:10 fprintf(‘%d \n’, ii) end FOR is a control construct.
CSD 340 (Blum)1 For Loops See Beginning JavaScript (Paul Wilton) p. 87.
Loop Applications and Review CS 103 February 27, 2004.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
BLOCK  is enclosed by curly brackets {}.  Keep in mind that blocks are always bounded by curly brackets, even if your block has only one code line.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Lab 4: Loops and Iteration Graham Northup
Loops A loop is: Java provides three forms for explicit loops:
Program design Program Design Process has 2 phases:
REPETITION CONTROL STRUCTURE
Lesson 05: Iterations Class Chat: Attendance: Participation
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CS1371 Introduction to Computing for Engineers
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
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.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Repetition and Loop Statements
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.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Chapter (3) - Looping Questions.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Intro to Computer Science CS1510 Dr. Sarah Diesburg
3.1 Iteration Loops For … To … Next 18/01/2019.
A LESSON IN LOOPING What is a loop?
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Loops CS 103 February 13, 2009 Author: Nate Hamm

What is a loop? A control construct that allows for a block of code to be repeated A control construct that allows for a block of code to be repeated In Matlab, loops come in two forms: In Matlab, loops come in two forms: FOR loopsFOR loops WHILE loopsWHILE loops

FOR Loops Syntax: Syntax: FOR variable = expression statementsEND

FOR Loops FOR variable = expression statementsEND The variable is known as an index, and can be treated as a variable by the program. Traditionally, the letter i is used as the index, but in Matlab, i also is used to represent imaginary numbers, so we use ii or another letter.

FOR Loops FOR variable = expression statementsEND In FOR loops, the expression is a counting mechanism. We typically use the colon operator to define how many times the loop should run. For example, 1:10 means to run the loop 10 times (from one to ten)

FOR Loops FOR variable = expression statementsEND The statements are the set of code which is to be executed over and over again during the loop. Every loop needs to conclude with an END

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Before the loop, ii doesn’t exist

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END First time through, ii = 1

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END First time through, ii = 1 Now we print Count: 1

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END First time through, ii = 1 Now we print Count: 1 The loop ends, so we’ll return to the top of the loop

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Second time through, so ii is incremented ii = 2

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Second time through, ii = 2 Now we print Count: 2

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Second time through, ii = 2 Now we print Count: 2 The loop ends, so we’ll return to the top of the loop

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Third time through, ii = 3

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Third time through, ii = 3 Now we print Count: 3 This will continue for awhile…

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Imagine we’re now on the 9 th iteration Ninth time through, ii = 9 Now we print Count: 9 The loop ends, so we’ll return to the top of the loop

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Tenth time through, ii = 10 This will be the last time we go through the loop!

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Tenth time through, ii = 10 Now we print Count: 10

Example: Count to 10 FOR ii = 1:10 fprintf(‘\n Count: %d’, ii) END Tenth time through, ii = 10 Now we print Count: 10 We’ve run the loop 10 times….because that’s as far as we have been asked to go, the loop ends and we’ll go on to whatever code exists after the end statement.

Notes About FOR Loops Used when the required values of your index variable are known before the loop begins Though not necessarily before the program begins. Used when the required values of your index variable are known before the loop begins Though not necessarily before the program begins. Example: count to 10, add 12 numbers, calculate pi to 30 decimal places, etc. Example: count to 10, add 12 numbers, calculate pi to 30 decimal places, etc.

FOR Loop Example Suppose we wanted to write a program that prints out all the positive numbers in an array, x Suppose we wanted to write a program that prints out all the positive numbers in an array, x X = [ ] X = [ ] for ii = 1:10 if x(ii) > 0 fprintf(‘%f \n’, x(ii)) endend But what happens if instead, we ask the user to input x first? Will we know how many elements are in x? But what happens if instead, we ask the user to input x first? Will we know how many elements are in x?

Working Around the Number Limitation When working with an array of numbers that may change, you can use the length function. FOR ii = 1:length(x) When working with an array of numbers that may change, you can use the length function. FOR ii = 1:length(x)

FOR Loop Example Ask the user to input an array, x, and then print out all the positive numbers in the array. Ask the user to input an array, x, and then print out all the positive numbers in the array. X = input(‘Please give me an array: ‘) FOR ii = 1:length(x) if x(ii) > 0 fprintf(‘%f \n’, x(ii)) endEND

Questions?