COMP 111. Introduction to Computer Science and Object-Oriented Programming

Similar documents
Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Loops / Repetition Statements

Loops (while and for)

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Loops. CSE 114, Computer Science 1 Stony Brook University

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Simple Java Programming Constructs 4

ECE 122. Engineering Problem Solving with Java

Motivations. Chapter 5: Loops and Iteration. Opening Problem 9/13/18. Introducing while Loops

Control Structures II. Repetition (Loops)

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Introduction. C provides two styles of flow control:

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Timing for Ripple Carry Adder

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

CS Kangmei Yang. Page 1

Chapter 4: Control structures. Repetition

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Warmup : Name that tune!

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

LEC03: Decisions and Iterations

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 4: Control structures

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS111: PROGRAMMING LANGUAGE II

1.3 Conditionals and Loops. 1.3 Conditionals and Loops. Conditionals and Loops

Structured Programming. Dr. Mohamed Khedr Lecture 9

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

STUDENT LESSON A12 Iterations

Conditionals, Loops, and Style

Repetition, Looping CS101

Repetition and Loop Statements Chapter 5

Java Coding 3. Over & over again!

(Refer Slide Time: 00:26)

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

1.3 Conditionals and Loops

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

COMP-202 Unit 4: Programming with Iterations

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

1.3 Conditionals and Loops

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter Goals. Contents LOOPS

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Intro to Computational Programming in C Engineering For Kids!

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Chapter 4 Introduction to Control Statements

Iteration statements - Loops

Chapter 3 Problem Solving and the Computer

Chapter 5: Loops and Files

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Data types Expressions Variables Assignment. COMP1400 Week 2

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Repetition, Looping. While Loop

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

Datatypes, Variables, and Operations

3 The L oop Control Structure

CS111: PROGRAMMING LANGUAGE II

Definition MATH Benjamin V.C. Collins, James A. Swenson MATH 2730

Chapter 2: Data and Expressions

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Loops / Repetition Statements

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Course Outline. Introduction to java

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Loops. GEEN163 Introduction to Computer Programming

Information Science 1

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

CS1150 Principles of Computer Science Loops (Part II)

1.3 Conditionals and Loops

switch-case Statements

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Arithmetic Compound Assignment Operators

IT 1033: Fundamentals of Programming Loops

CS 106 Introduction to Computer Science I

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CSC 1051 Data Structures and Algorithms I

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Information Science 1

Transcription:

COMP 111 Introduction to Computer Science and Object-Oriented Programming 5

Feeling Out of the Loop?

Methods - So Far Execute a sequence of statements Declarations Assignment statements Return Maybe an output (e.g. System.out.println ) Execute in order First to last Until last is executed Or a return Or an error And from last week. With Expressions!

Methods - So Far Optionally execute a sequence of statement Alternatively execute this sequence of statement or that sequence of statement With variations Series of comparisons Nested comparisons With Conditons!

But While (as humans) we know how to Determine when an interest-bearing investment reaches a target amount Do long division with remainders Average the scores of any 111 class on Test 1 We cannot write methods that do these! (or at least not easily and in general!)

They All Hinge on Repeating Describe how repetition drives Determining when an interest-bearing investment reaches a target amount Doing long division with remainders Averaging the scores of any 111 class on Test 1

Digging Deeper In each case we repeat something gated by some critical test repeat something as long as until a test is true!

They All Hinge on a Test Describe what key test drives Determining when an interest-bearing investment reaches a target amount Doing long division with remainders Averaging the scores of any 111 class on Test 1

Examples from the World As long as the meat thermometer is less than 160 degrees, bake another 5 minutes While the robot does not sense the wall ahead, move it forward one unit while a test is true statement whatever is next true test statement statement false

Examples from the World Keep going straight until you reach Putnam street Continue adding stuff while the weight is less than 50 lbs do statement while a test is true go on to next true statement statement test false

Examples from the World Sum the usage minutes for each day of the year Compute 33! (33 factorial) initialize counter for (a range of counter values) statement go on to next test counter true statement statement false adjust counter

The Java While Loop Want to repeat while something is true while (test) statements true test false the loop body statement statement Must the loop body be executed at least once?

Consider Compute integer division the hard way count how many times the divisor can be subtracted from the dividend A small aha fits this into an iteration framework? dividend divisor count 13 4 0 why stop?

Moving to the While Compute integer division the hard way count how many times the divisor can be subtracted from the dividend dividend divisor count 13 4 0 9 4 1 5 4 2 1 4 3 start the count at 0; while (the dividend >= than divisor) subtract the divisor; increment the count;

Realizing In Java Want to repeat while something is true public int divideby(int num1, int num2) int count = 0; int dividend = num1; while (dividend >= num2) dividend = dividend - num2; count++; return count;

You Try It? public int divideby(int num1, int num2) int count = 0; int dividend = num1; Trace the value for divideby(20,5) divideby(3,5) divideby(7,7) while (dividend >= num2) dividend = dividend - num2; count++; return count; dividend divisor count 0

The Gotcha! public int divideby(int num1, int num2) int count = 0; int dividend = num1; Trace the value for divideby(5,0) divideby(7,-2) while (dividend >= num2) dividend = dividend - num2; count++; return count; dividend divisor count 0

Another Gotcha! Off-by-one errors! public int divideby(int num1, int num2) int count = 0; int dividend = num1; Bad Test while (dividend > num2) dividend = dividend - num2; count++; return count; Try divideby(4,2) dividend divisor count 0

The Java Do Loop Always executes the body of the loop At least once! do statements while (test); true statement statement test the loop body false

Consider Compute mystery math operation public int mystery(int m, int n) int r; do r = n % m; n = m; m = r; while (r!= 0); mystery(8,20) r n m 20 8 4 8 4 0 4 0 return n;

Consider Compute mystery math operation public int mystery(int m, int n) int r; do r = n % m; n = m; m = r; while (r!= 0); mystery(20,8) r n m 8 20 8 20 8 4 8 4 0 4 0 return n;

Consider Compute mystery math operation public int mystery(int m, int n) int r; do r = n % m; n = m; m = r; while (r!= 0); mystery(3,11) r n m 11 3 2 3 2 1 2 1 0 1 0 return n;

What Is It? Can you name the mystery operation mystery(20,8) = 4 mystery(8,20) = 4 mystery(3,11) = 1

Another Problem What is the sum of the first n positive odd integers? For example the first 5 positive integers are 1, 3, 5, 7, 9 their sum is 25 easy, but what about sum of first 1000?

A Strategy What is the sum of the first n positive odd integers? Start with odd number 1 with a sum of 0 Repeat n times add the odd number to the sum find the next odd number

Realizing the Strategy What is the sum of the first n positive odd integers? Start with odd number 1 with a sum of 0 Repeat n times add the odd number to the sum find the next odd number start the odd number at 1 start the sum at 0; while (less than n times) add the odd number to the sum; find next odd number;

Moving Forward What is the sum of the first n positive odd integers? Start with odd number 1 with a sum of 0 Repeat n times add the odd number to the sum find the next odd number start the odd number at 1 start the sum at 0; while (less than n times) add the odd number to the sum; find next odd number; int oddnum = 1; int sum = 0; while (less than n times) sum = sum + oddnum; oddnum = oddnum + 2;

Thinking About Counting How do you count n times? start a count at 0 while (count < n) add 1 to the count; int count = 0; while (count < n) count++; Note: when n is 5, this counts 0, 1, 2, 3, 4 How would you count 1, 2, 3, 4, 5?

Putting It Together What is the sum of the first n positive odd integers? start the odd number at 1 start the sum at 0; while (less than n times) add the odd number to the sum; find next odd number; int count = 0; while (count < n) count++; int count - 0; int oddnum = 1; int sum = 0; while (count < n) sum = sum + oddnum; oddnum = oddnum + 2; count++;

In a Method public int sumfirstodds(int n) int count = 0; int oddnum = 1; int sum = 0; sumfirstodds(5) n count oddnum sum while (count < n) sum = sum + oddnum; oddnum = oddnum + 2; count++; return sum;

You Try It public int sumfirstodds(int n) int count = 0; int oddnum = 1; int sum = 0; sumfirstodds(1) n count oddnum sum while (count < n) sum = sum + oddnum; oddnum = oddnum + 2; count++; return sum;

Counting Is Common Counting up, counting down, etc. Using a counter (or index) int count = 0; while (count < n) count++;

The Java For Loop Abbreviates the initialization testing adjusting of a counter Implicitly executes them implicit implicit initialize counter test counter false for (init, testing, adjusting) statement true statement statement implicit adjust counter

The Java For Loop Abbreviates the initialization testing adjusting of a counter implicit implicit int count; for (count = 0; count < n; count++) statements implicit count = 0; true count < n statement statement count++ false

Notes the For Loop Note the semicolons Can declare counter within the for construct for (int count = 0; count < n; count++) statements Does the for replace the while loop?

Using the For Loop public int sumfirstodds(int n) int oddnum = 1; int sum = 0; sumfirstodds(5) n count oddnum sum for (count = 0; count < n; count++) sum = sum + oddnum; oddnum = oddnum + 2; return sum;

Would This Work? public int sumfirstodds(int n) int oddnum = 1; int sum = 0; sumfirstodds(5) n count oddnum sum for (count = 1; count <= n; count++) sum = sum + oddnum; oddnum = oddnum + 2; return sum;