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.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
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.
Computer Science 1620 Loops.
Get number The ginput command creates crosshairs. When the user clicks the x,y values of the crosshairs are returned.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Computer Science 1620 Programming & Problem Solving.
Loops For loop for n = [ ] code end While loop while a ~= 3 code end.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
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 INSTRUCTOR: MOHAMMAD MOJADDAM.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition 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.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
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.
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)
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.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Design of Bio-Medical Virtual Instrumentation Tutorial 2.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Agenda Basic Logic Purpose if statement if / else statement
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
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.
Chapter 4 Introduction to Control Statements
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
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.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
COMP Loop Statements Yi Hong May 21, 2015.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Beginning Programming for Engineers Matlab Conditional Computation.
Chapter 4 – C Program Control
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
Week 8 - Programming II Today – more features: Loop control
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Control Structures.
Presentation transcript:

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 break terminates the execution of the whole 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

What if there are more than 2 situations? 3 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

The “elseif” command if expression1 {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”

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

Variable values by example index1index2 All possible combinations of the indices are generated.

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

Example – matching of people’s skills and tasks: 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 Job 1 Job 2 Job 3 Job 4 Joe7442 Sue6852 Bob4713 Liz6521

Solution – use nested loops to try all combinations, skipping repeats: First, let’s initialize variables: Job 1234 Joe7442 Sue6852 Bob4713 Liz6521

Next, start nested loops: Check for repeats and skip continue stops the present pass and starts the next pass.

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

The result: Job 1 Job 2 Job 3 Job 4 Joe7442 Sue6852 Bob4713 Liz6521

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: location indicator different prompt

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