General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

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.
Flow Charts, Loop Structures
CMPS 1371 Introduction to Computing for Engineers
CS0004: Introduction to Programming Repetition – Do Loops.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Loops and Files.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.
Loops – While, Do, For Repetition Statements Introduction to Arrays
General Computer Science for Engineers CISC 106 Lecture 12 Roger Craig Computer and Information Sciences 3/11/2009.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
While Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops.
CPS120 Introduction to Computer Science Iteration (Looping)
Synthesis ENGR 1181 MATLAB 11. Topics  No new material  Covers topics that will be on the Midterm 2 Exam MATLAB 01 – Program Design MATLAB 02 – Introduction.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Loop Structures.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to MATLAB
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.
Outline Altering flow of control Boolean expressions
3.5- The while Statement The while statement has the following syntax:
CISC101 Reminders All assignments are now posted.
Loops.
CISC101 Reminders Quiz 1 marking underway.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
General Computer Science for Engineers CISC 106 Lecture 11
Presentation transcript:

General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009

Lecture Overview Midterm Review (March 17, 4:30pm to 7pm) in Gore Hall 318 Midterm review sheet will be posted online Go over labs and class notes While loop with complex conditions

Matrix looping cont. function [output] = change8to9( inputMatrix) %changes the first 8 in each row to a 9 in a matrix and returns that altered matrix %input: matrix %output: matrix lengths = size(inputMatrix); %get size of matrix and store in 1x2 array fprintf('size of the matrix m by n is ') disp(lengths) for m = 1:lengths(1) for n = 1:lengths(2) if (inputMatrix(m,n) == 8) %if that position equals an 8 inputMatrix(m,n) = 9; %change that position in the matrix to a 9 using an assignment statement break; %break out of this inner FOR loop end %ends the IF end %ends the inner FOR loop end %ends the outer FOR loop output = inputMatrix; %assign the matrix to the output value (or in other words, "return the (possibly) altered matrix") end %end the function Note: this code is found on the course website under Examples

For loop and break statement break; lets us stop a for loop before it would normally stop However, break; statement makes for unclear code and should be avoided What to use instead? While loop

While Loop while (logical expression) %block of code to execute end First, the logical expression is tested. If it is true, we do one iteration of the loop. If it is false, we go to the next line after the ‘end’ statement. After every iteration of the loop, we test the logical expression again to see if it is still true.

While loop How many times will these run? while (true) %here true is the matlab constant ‘true’ %do something end %it runs forever while (false) %here false is the matlab constant ‘false’ %do something End %never goes inside the while loop If we use a counter, we can do the same thing as a FOR loop

While loop with counter for counter = 1:2:11 disp(counter); end The following while loop produces the same exact output counter = 1; while(counter <= 11) disp(counter); counter = counter + 2; end

ChangeOnlyOne8to9.m function [output] = changeOnlyOne8to9( inputMatrix) %changes only the first 8 to a 9 in a matrix and returns that altered matrix %(The matrix is traversed row by row.) %input: matrix %output: matrix lengths = size(inputMatrix); %get size of matrix and store in 1x2 array eightNotSeenYet = true; %initialize flag to true m = 1; %initialize m to 1 n = 1; %initialize n to 1 while(eightNotSeenYet & (m < lengths(1))) n = 1; %reset n to 1 as we begin a new row while(eightNotSeenYet & (n < lengths(2))) if (inputMatrix(m,n) == 8) %if that position equals an 8 inputMatrix(m,n) = 9; %change that position in the matrix to a 9 \ using an assignment statement eightNotSeenYet = false; %set flag to false end %ends the IF end %ends the inner While loop end %ends the outer While loop output = inputMatrix; %assign the matrix to the output value (or in other word\ s, "return the (possibly) altered matrix") end %end the function Warning, this code will produce an infinite loop on purpose

Matrix loops How to break out of an infinite loop? Ctrl-C (Note: Ctrl – C will also end processes on the unix command line if they are running too long and you want them to stop.) Other examples How to loop through a matrix and set the entire diagonal (starting at 1,1) to zero? Is that easier to do with FOR loops or while loops? How to write a loop to sum the elements in a matrix x.