1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.

Slides:



Advertisements
Similar presentations
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.1 Chapter 5 Loops.
Advertisements

CMPS 1371 Introduction to Computing for Engineers
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Repeating Actions While and For Loops
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
ENGR 111A - Spring MatLab – Palm Chapter 4, Part 4 Review and Debugging Class 12.1: Palm Chapters &
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
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 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 5: Structured Programming
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Algorithm Design.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Program Design. The design process How do you go about writing a program? –It’s like many other things in life Understand the problem to be solved Develop.
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 tutorial course Lesson 4: Writing your own functions: programming constructs
Introduction to Loops Iteration Repetition Counting Loops Also known as.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Introduction to Computer Programming
Chapter 5: Structured Programming
Chapter 6: Loops.
Repetition Structures Chapter 9
Topics Introduction to Repetition Structures
Chapter 4 MATLAB Programming
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 6: Conditional Statements and Loops
Topics Introduction to Repetition Structures
Control Structures - Repetition
Control Structure Senior Lecturer
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Types of Flow of Control
3 Control Statements:.
Chapter 5 Loops.
Looping III (do … while statement)
Topics Introduction to Repetition Structures
Matlab Basics.
CS Problem Solving and Object Oriented Programming Spring 2019
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending on a condition Repetition Executing a set of statements more than one time (loops) 1

2 The “BREAK” Command When inside a loop (for or while), break terminates the execution of the loop (the whole loop, not just the last pass). When the break command appears in a loop, Matlab jumps to the end command of the loop and continues with the next command (does not go back to the for or while command of that loop). If the break command is inside a nested loop, only the nested loop containing that break is terminated. The break command is usually found within a conditional statement. In loops it provides a method to terminate the looping process if some condition is met, e.g., if the number of loops exceeds a predetermined value, or an error in some numerical procedure is smaller than a predetermined value.

3 Sample with Break

4 The “CONTINUE” Command The “Continue Command” can be used inside a “for loop” or a “while loop” to stop the present pass and start the next pass in the looping process. The “Continue Command” is usually part of a conditional statement. When MATLAB reaches a continue command, it skips to the end of the innermost loop containing the continue and then starts a new pass.

5 Sample with Continue

6 Nesting Similar items contained within e.g., matryoshka dolls

7 Simple Nested IFs if else end else end if else if else end

8 Alternative to Simple Nested IF if else end else end if end if end if end

9 Simple Nested Loops for for end end while while end end

10 A Nested Loop Example Problem: Use a Loop to create a Matrix M with n rows and m columns  The first row elements should get values equal to the number of their respective column  The values for the elements in the first column should be the number of their respective row  The rest of the elements get values equal to the sum of the element just above them and the element to their left  The program is interactive and prompts the user to enter the values for n and m 

Sample M for n=4, m=

12 Recall … Design Steps for Loops Identify a task that can be accomplished if some steps are repeated (These steps form the loop body) Define a starting point Define a stopping point Keep track of (and measure) progress Make sure the loop will stop!  If necessary use the “Break” command or the “Continue” command

13 n = input ('Enter the number of rows: '); m = input ('Enter the number of columns: '); for k = 1:n % Start of the 1st For-end Loop for h = 1:m % Start of the 2nd For-end Loop (nested loop) if k == 1 % Start of the nested "if-elseif-else-end" M(k,h) = h; elseif h == 1 M (k, h) = k; else M(k,h) = M(k,h-1) + M(k-1,h); end % End of the if statement end % End of the 2nd For-end Loop (nested loop) end % End of the 1st For-end Loop M % Display M Solution using 2 Nested “For-Loops”

14 n = input ('Enter the number of rows '); m = input ('Enter the number of columns '); k = 1; while k <= n % Start of the 1st while-end Loop h = 1; while h <= m % Start of the 2nd while-end Loop if k == 1 % Start of the nested "if-elseif-else-end" M(k,h) = h; elseif h == 1 M (k, h) = k; else M(k,h) = M(k,h-1) + M(k-1,h); end % End of the if statement h = h + 1; end % End of the 2nd For-end Loop k = k + 1; end % End of the 1st For-end Loop M % Display M Solution using 2 Nested “While-Loops”

Setup for Example 2 Determine whether a given number is prime: Use the MATLAB rem() function rem(x,y) returns the remainder from x/y examples: rem(12, 5) is 2 rem(24, 6) is 0 rem(3, 5) is 3 15

Algorithm to Determine if Prime Check that number is > 0 If number is 1, it is prime! Initialize divisor = 2 Repeat: let x = rem(number,divisor) if x == 0, then x is ‘not prime’, so stop loop otherwise, set divisor = divisor + 1 If divisor == number, then number is prime! 16

Solution to Determine if Prime number = input('Please enter a positive integer: '); if (number <= 0) %test validity of input disp(‘not a positive integer; program ends'); elseif (number == 1) %and we’re done disp('1 is indeed prime'); else divisor = 2; %initialize factor value while ((rem(number,divisor) ~= 0)) divisor = divisor + 1; end if (number == divisor) fprintf('The number %d is prime\n',number); else fprintf('The number %d is NOT prime\n', number); end 17

Example 2: Find Primes Find all the primes in a given range of numbers Algorithm: obtain and check the range values starting with the lower end, repeat: if number is prime, print it if range is not complete, continue with the next number 18 What type of loop?

Solution for Find Primes in a Range lowest = input('Please enter start of range (>=1): '); highest = input('Please enter end of range: '); if ((highest-lowest <= 0)||(lowest <= 1)) %test validity disp('not an appropriate range; program ends'); else disp('The primes within this range are: '); for number = lowest:highest divisor = 2; %initialize factor value while ((rem(number,divisor) ~= 0)) divisor = divisor + 1; end if (number == divisor) fprintf('%d ',number); end 19 One minor issue remains – can you find it? Hint: consider output from all cases…

Tracing Practice 20