Week 8 - Programming II Today – more features: Loop control

Slides:



Advertisements
Similar presentations
Complexity Analysis (Part II)
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
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.
Computer Science 1620 Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Loops – While, Do, For Repetition Statements Introduction to Arrays
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
COMP Loop Statements Yi Hong May 21, 2015.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
A First Book of C++ Chapter 4 Selection.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 4 Repetition Structures
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Repetition Structures Chapter 9
Chapter 3: Decisions and Loops
Matlab Training Session 4: Control, Flow and Functions
Topics Introduction to Repetition Structures
EGR 2261 Unit 4 Control Structures I: Selection
Loop Structures.
Chapter 4 Loops DDC 2133 Programming II.
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 6: Conditional Statements and Loops
Topics Introduction to Repetition Structures
LOOPS.
Iterations Programming Condition Controlled Loops (WHILE Loop)
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.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
3.5- The while Statement The while statement has the following syntax:
CS2011 Introduction to Programming I Loop Statements (II)
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Topics Introduction to Repetition Structures
Chapter 4 Repetition Structures
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Control Structures.
Presentation transcript:

Week 8 - Programming II Today – more features: Loop control Extending if/else Nesting of loops Debugging tools Textbook chapter 7, pages 216-218 (sections 7.2.3, 7.5, 7.6 )

Loop Controls Loops contain sets of commands that you want to do repeatedly. You might want to: Skip commands in the current iteration Stop the loop itself Why continue once you’ve found what you’re looking for !!

Skipping Ahead: Continue Continue – jumps to next loop iteration: for k = 1:25000 if x(k) > 0 continue end { more commands } skip ahead

Early Termination: Break Break – ends the loop: for variable = {array of length n} ….. break end go to commands beyond end

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 loop

only needed 10 years

Example – accept input, appending it to vector, until a negative number is entered: allow up to 1000 values, if necessary

negative value stops the input

provide hi/lo feedback Example – Hi-Lo: a guessing game with feedback select hidden number input guess correct? yes no provide hi/lo feedback 5 tries? win lose

Extensions of if/else if expression As introduced, if/else allows for two choices: if expression {commands if expression is true } else {commands if false } end

What if there are more than 2 situations? find the largest of 3 variables a, b, c a ≥ b ≥ c a ≥ c ≥ b b ≥ a ≥ c b ≥ c ≥ a c ≥ b ≥ a c ≥ a ≥ b 4 situations: convert a compass angle to a direction: 0º  east 90º  north 180º  west 270º  south

Could use “nested” if/else commands

or

if expression1 The “elseif” command {commands if expression1 is true } elseif expression2 {commands if expression2 is true } else {commands if both expressions are false } end

Examples: Note – many elseifs are allowed, but only 1 “else”

Loops within Loops – Nesting for index1 = array1 {outer loop commands} for index2 = array2 {inner loop commands} end {more outer loop commands} can be more than 2 levels deep

Variable values by example index1 index2 Variable values by example 1 3 1 6 1 9 2 3 2 6 2 9 3 3 3 6 3 9 4 3 4 6 4 9

Example – computing a table of z = x2+y2 for x and y equal to the integers 1, 2,…6:

Example – matching of people’s skills and tasks: Job 1 Job 2 Job 3 Job 4 Joe 7 4 2 Sue 6 8 5 Bob 1 3 Liz Situation: 4 tasks 4 people with different skills to do them Skill table as shown Goal – assign tasks to maximize the sum Example solution of 20

Solution – use nested loops to try all combinations, skipping repeats: First, let’s initialize variables:   Job 1 2 3 4 Joe 7 Sue 6 8 5 Bob Liz

Next, start nested loops: Check for repeats and skip

Test a valid assignment for quality: And then terminate the 4 for loops:

Job 1 Job 2 Job 3 Job 4 Joe 7 4 2 Sue 6 8 5 Bob 1 3 Liz The result:

Debugging Tools so far, Run

Debugging ≡ finding and correcting errors (bugs) in programs Useful debugging tools: Ability to stop a program in the middle of its execution (at a breakpoint) Ability to examine variable values at that point Ability to modify variable values at that point

controls for creating and removing breakpoints

indicator of breakpoint location (can have multiple breakpoints)

What shows up at the breakpoint Command window: Editor window: different prompt location indicator

Can single step (F10) or continue (F5) to the next breakpoint (or end)