CS1150 Principles of Computer Science Loops

Similar documents
READING STATECHART DIAGRAMS

Five Whys How To Do It Better

Differentiation Applications 1: Related Rates

CONSTRUCTING STATECHART DIAGRAMS

Medium Scale Integrated (MSI) devices [Sections 2.9 and 2.10]

Activity Guide Loops and Random Numbers

Introduction to Models and Properties

Experiment #3. Graphing with Excel

CHM112 Lab Graphing with Excel Grading Rubric

B. Definition of an exponential

Lecture 7: Damped and Driven Oscillations

CS453 Intro and PA1 1

37 Maxwell s Equations

Dataflow Analysis and Abstract Interpretation

MODULE 1. e x + c. [You can t separate a demominator, but you can divide a single denominator into each numerator term] a + b a(a + b)+1 = a + b

ENG2410 Digital Design Arithmetic Circuits

Section 5.8 Notes Page Exponential Growth and Decay Models; Newton s Law

NUMBERS, MATHEMATICS AND EQUATIONS

Bicycle Generator Dump Load Control Circuit: An Op Amp Comparator with Hysteresis

APPLICATION GUIDE (v4.1)

Purpose: Use this reference guide to effectively communicate the new process customers will use for creating a TWC ID. Mobile Manager Call History

AIP Logic Chapter 4 Notes

A New Evaluation Measure. J. Joiner and L. Werner. The problems of evaluation and the needed criteria of evaluation

Thermodynamics Partial Outline of Topics

Bootstrap Method > # Purpose: understand how bootstrap method works > obs=c(11.96, 5.03, 67.40, 16.07, 31.50, 7.73, 11.10, 22.38) > n=length(obs) >

This section is primarily focused on tools to aid us in finding roots/zeros/ -intercepts of polynomials. Essentially, our focus turns to solving.

CHAPTER 24: INFERENCE IN REGRESSION. Chapter 24: Make inferences about the population from which the sample data came.

Unit 1: Introduction to Biology

Temperature sensor / Dual Temp+Humidity

CS 477/677 Analysis of Algorithms Fall 2007 Dr. George Bebis Course Project Due Date: 11/29/2007

Forensic Science. Group: Background information

Trigonometric Ratios Unit 5 Tentative TEST date

2004 AP CHEMISTRY FREE-RESPONSE QUESTIONS

Perfect Punctua+on Part Three: The Semicolon and Colon

Physics 2B Chapter 23 Notes - Faraday s Law & Inductors Spring 2018

Lab #3: Pendulum Period and Proportionalities

o o IMPORTANT REMINDERS Reports will be graded largely on their ability to clearly communicate results and important conclusions.

, which yields. where z1. and z2

Revisiting the Socrates Example

Name: Block: Date: Science 10: The Great Geyser Experiment A controlled experiment

We can see from the graph above that the intersection is, i.e., [ ).

Lesson Plan. Recode: They will do a graphic organizer to sequence the steps of scientific method.

SPH3U1 Lesson 06 Kinematics

Name Student ID. A student uses a voltmeter to measure the electric potential difference across the three boxes.

Revision: August 19, E Main Suite D Pullman, WA (509) Voice and Fax

Purchase Order Workflow Processing

AP Physics Kinematic Wrap Up

Turing Machines. Human-aware Robotics. 2017/10/17 & 19 Chapter 3.2 & 3.3 in Sipser Ø Announcement:

Lecture 13: Markov Chain Monte Carlo. Gibbs sampling

Lecture 6: Phase Space and Damped Oscillations

PHYS 314 HOMEWORK #3

CHAPTER 2 Algebraic Expressions and Fundamental Operations

Fall 2013 Physics 172 Recitation 3 Momentum and Springs

Mathematics and Computer Sciences Department. o Work Experience, General. o Open Entry/Exit. Distance (Hybrid Online) for online supported courses

[COLLEGE ALGEBRA EXAM I REVIEW TOPICS] ( u s e t h i s t o m a k e s u r e y o u a r e r e a d y )

EDA Engineering Design & Analysis Ltd

If (IV) is (increased, decreased, changed), then (DV) will (increase, decrease, change) because (reason based on prior research).

Lab 1 The Scientific Method

3. Classify the following Numbers (Counting (natural), Whole, Integers, Rational, Irrational)

Chapter Summary. Mathematical Induction Strong Induction Recursive Definitions Structural Induction Recursive Algorithms

Spring 2010 Instructor: Michele Merler.

Part a: Writing the nodal equations and solving for v o gives the magnitude and phase response: tan ( 0.25 )

EASTERN ARIZONA COLLEGE Introduction to Statistics

THE LIFE OF AN OBJECT IT SYSTEMS

Biochemistry Summer Packet

Temperature sensor / Dual Temp+Humidity

IB Sports, Exercise and Health Science Summer Assignment. Mrs. Christina Doyle Seneca Valley High School

20 Faraday s Law and Maxwell s Extension to Ampere s Law

Name AP CHEM / / Chapter 1 Chemical Foundations

ENG2410 Digital Design Sequential Circuits: Part A

GENESIS Structural Optimization for ANSYS Mechanical

MATCHING TECHNIQUES Technical Track Session VI Céline Ferré The World Bank

COMP 551 Applied Machine Learning Lecture 11: Support Vector Machines

TRAINING GUIDE. Overview of Lucity Spatial

COMP 551 Applied Machine Learning Lecture 9: Support Vector Machines (cont d)

Lead/Lag Compensator Frequency Domain Properties and Design Methods

Chem 163 Section: Team Number: ALE 24. Voltaic Cells and Standard Cell Potentials. (Reference: 21.2 and 21.3 Silberberg 5 th edition)

Physics 2010 Motion with Constant Acceleration Experiment 1

Hooke s Law (Springs) DAVISSON. F A Deformed. F S is the spring force, in newtons (N) k is the spring constant, in N/m

Part One: Heat Changes and Thermochemistry. This aspect of Thermodynamics was dealt with in Chapter 6. (Review)

A Transition to Advanced Mathematics. Mathematics and Computer Sciences Department. o Work Experience, General. o Open Entry/Exit

ANSWER KEY FOR MATH 10 SAMPLE EXAMINATION. Instructions: If asked to label the axes please use real world (contextual) labels

A proposition is a statement that can be either true (T) or false (F), (but not both).

More Tutorial at

Physical Layer: Outline

Lecture 02 CSE 40547/60547 Computing at the Nanoscale

Workshop 2 Data-Logger Principles

ELE Final Exam - Dec. 2018

SPECIMEN. Candidate Surname. Candidate Number

- *Figure of chemical shift ranges for different types of P (see links)*

Chapter 16. Capacitance. Capacitance, cont. Parallel-Plate Capacitor, Example 1/20/2011. Electric Energy and Capacitance

EE247B/ME218: Introduction to MEMS Design Lecture 7m1: Lithography, Etching, & Doping CTN 2/6/18

Thermodynamics and Equilibrium

CHAPTER Read Chapter 17, sections 1,2,3. End of Chapter problems: 25

A Scalable Recurrent Neural Network Framework for Model-free

1. Transformer A transformer is used to obtain the approximate output voltage of the power supply. The output of the transformer is still AC.

Review Problems 3. Four FIR Filter Types

City of Angels School Independent Study Los Angeles Unified School District

Lecture 12: Chemical reaction equilibria

Synchronous Motor V-Curves

Transcription:

CS1150 Principles f Cmputer Science Lps Yanyan Zhuang Department f Cmputer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Clrad Springs

Review Blean variables Assume x=3, y=1, true r false?!(x<2) y>3 If statement Be careful: multiple/nested if else By default: else is mathced with if? Switch statement Be careful: where t use break CS4500/5500 UC. Clrad Springs

Overview While lp D while lp Fr lp CS1150 UC. Clrad Springs

Opening Prblem: Why Lps? Prblem: 100 times System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); 4

Intrducing while Lps int cunt = 0; while (cunt < 100) { System.ut.println("Welcme t Java"); cunt++; 5

Intrducing while Lps int cunt = 0; while (cunt < 100) { System.ut.println("Welcme t Java"); cunt++; while (lp-cntinuatin-cnditin) { // lp-bdy; Statement(s); Hw It Wrks The lp cntinuatin cnditin - blean expressin - is evaluated If the cnditin is true, the statements in the lp bdy are executed When executin f lp bdy statements is cmplete, cntrl returns t the lp cnditin The lp cntinuatin cnditin is evaluated again When the lp cnditin is false, cntrl ges t statements fllwing the lp Nte: if the lp cntinuatin cnditin evaluates t false the first time, the entire while lp is skipped 6

while Lp Flw Chart while (lp-cntinuatin-cnditin) { // lp-bdy; Statement(s); int cunt = 0; while (cunt < 100) { System.ut.println("Welcme t Java!"); cunt++; 7

Rules fr While Lps The lp cnditin must be a blean expressin Blean expressin must be in parentheses Blean expressins are frmed using relatinal r lgical peratrs Lp cnditin Usually a statement befre while lp "initializes" lp cnditin t true Sme statement within lp bdy eventually change the cnditin t false If the cnditin is never changed t false, the prgram is frever in the lp This is called an "infinite lp" Curly braces are nt necessary if nly ne statement in lp But best practice is t always include curly braces CS4500/5500 UC. Clrad Springs

Trace while Lp int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Initialize cunt (which we ften call cntrl variable) 9

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; (cunt < 2) is true 10

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Print Welcme t Java 11

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Increase cunt by 1 cunt is 1 nw 12

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; (cunt < 2) is still true since cunt is 1 13

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Print Welcme t Java 14

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Increase cunt by 1 cunt is 2 nw 15

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; (cunt < 2) is false since cunt is 2 nw 16

Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; The lp exits. Execute the next statement after the lp. Let s lk at the first example PrintNTimes.java 17

Infinite lp example In this example, nthing in the lp bdy changes the value f the cntrl variable cunt = 1; // Initializes the lp cntrl variable while (cunt <= 5) { System.ut.println("The value f cunt is " + cunt); This is an infinite lp because (cunt <= 5) is always true Nthing changes the value f cunt in the lp bdy If yu accidentally create an infinite lp, use terminate buttn (red square) in Eclipse t make it stp CS4500/5500 UC. Clrad Springs

Placing a semicln at the end f the while-clause creates an infinite lp - be careful! int iteratin = 1; while (iteratin <= 10);{ System.ut.println("Iteratin = " + iteratin); iteratin++; CS4500/5500 UC. Clrad Springs

Off-by-ne Errr Cmmn issue with lps: Lp bdy executes ne mre/less than expected Example: System.ut.println("I'm ging t cunt t three, ready set..."); cunt = 1; while (cunt < 3) { System.ut.println(cunt); cunt++; CS4500/5500 UC. Clrad Springs

Off-by-ne Errr Cmmn issue with lps: Lp bdy executes ne mre/less than expected Example: System.ut.println("I'm ging t cunt t three, ready set..."); cunt = 1; while (cunt < 3) { Output: System.ut.println(cunt); cunt++; I'm ging t cunt t three, ready set... 1 2 CS4500/5500 UC. Clrad Springs

Prblem: Repeat Additin Until Crrect See RepeatAdditinQuiz.java. 22

Ending a Lp with a Sentinel Value Often the number f times a lp is executed is nt predetermined. Yu may use an input value t signify the end f the lp. Such a value is knwn as a sentinel value. Write a prgram that reads and calculates the sum f an unspecified number f integers (e.g., the sum f 2, 3, 5, 7, 11 ). The input 0 signifies the end f the input. See SentinelValue.java. 23

d-while Lp d { // Lp bdy; Statement(s); while (lp-cntinuatin-cnditin); Example: TestDWhile.java The lp bdy is executed The lp cnditin - blean expressin - is evaluated If the lp cnditin is true, then lp bdy is executed again If the lp cnditin is false, cntrl is transferred t the statement fllwing the lp 24

D While Lp Rules (same as while lp) The lp cnditin must be a blean expressin Blean expressin must be in parentheses Blean expressin is frmed using relatinal and lgical peratrs Lp cnditin Generally, sme statement befre the while lp "initializes" the lp cnditin t true Sme statement within the lp bdy must eventually change the cnditin t false If the cnditin is never changed t false, the prgram will be frever stuck in the lp This is called an "infinite lp" Curly braces are nt necessary if nly ne statement in lp but best practice is t always include curly braces CS4500/5500 UC. Clrad Springs

Nte Recall hw placing a semicln at the end f the while-clause creates an infinite lp int iteratin = 1; while (iteratin <= 10); { // Unnecessary semicln System.ut.println("Iteratin = " + iteratin); iteratin++; CS4500/5500 UC. Clrad Springs

Nte In the case f d-while yu must include the semicln since it ends the lp! int iteratin = 1; d { iteratin++; System.ut.println("Iteratin = " + iteratin); while (iteratin <= 5); // Necessary semicln CS4500/5500 UC. Clrad Springs

Lp Design Strategies Fur steps when writing a lp. Step 1: Identify what statements need t be repeated Step 2: Wrap these statements in a lp using while r d while: while (true) { Statements; Step 3: Determine what cnditin the cde shuld check (replace true) Step 4: Add cde in the bdy that eventually causes the cnditin t becme false while (lp-cntinuatin-cnditin) { Statements; Additinal statements fr cntrlling the lp; CS4500/5500 Example: Pwers.java UC. Clrad Springs

Summary While lp D while lp CS1150 UC. Clrad Springs