Nesting. Abstraction & Nesting. Example. if x is less than y output x is smaller else output y is smaller. CS256 Computer Science I Kevin Sahr, PhD

Similar documents
Flow of Control. Conditional Statements. Conditional Statements. CS256 Computer Science I Kevin Sahr, PhD. Lecture 15: The If-Statement

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Lecture 7 Tao Wang 1

Information Science 1

Information Science 1

Repetition Structures

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

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

CS110D: PROGRAMMING LANGUAGE I

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Lecture 5 Tao Wang 1

Dr. Scheme evaluates expressions so we will start by using the interactions window and asking Dr. Scheme to evaluate some expressions.

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

SME 1013 Programming for Engineers

PIC 10A. Review for Midterm I

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Final Examination Semester 3 / Year 2010

Programming Language. Machine Code. CS200: Computer Science I. Module 3 Programming Languages

Programming Lecture 4

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

#4: While Loop Reading: Chapter3

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Chapter 4 Introduction to Control Statements

Control Structures in Java if-else and switch

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

CS 483. Jana Kosecka CS Dept Eng. Building

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Sorting. Bubble Sort. Selection Sort

STUDENT LESSON A12 Iterations

LECTURE 04 MAKING DECISIONS

Writeup for first project of CMSC 420: Data Structures Section 0102, Summer Theme: Threaded AVL Trees

Principles of Computer Science I

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Loops (while and for)

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

8/27/2016. ECE 120: Introduction to Computing. Graphical Illustration of Modular Arithmetic. Representations Must be Unambiguous

C Programming for Engineers Structured Program

LOOPS. Repetition using the while statement

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

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

Outline. Program development cycle. Algorithms development and representation. Examples.

10-2 Circles. Warm Up Lesson Presentation Lesson Quiz. Holt Algebra2 2

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

Math 291: Lecture 4. Dr. Fagerstrom. Minnesota State University Moorhead web.mnstate.edu/fagerstrom

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

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Mathematics 134 Calculus 2 With Fundamentals Exam 2 Answers/Solutions for Sample Questions March 2, 2018

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

Warm Up Problem. Write any equivalent MIPS program corresponding to the following code: int wain ( int a, int b){ int c = a + b; return c*a; }

Microsoft Visual Basic 2005: Reloaded

Programming Lecture 4

CS 302: Introduction to Programming in Java. Lecture 9

Repetition Structures II

Rational Numbers on the Coordinate Plane. 6.NS.C.6c

Top-Down Program Development

Chapter 11. Analysis of Algorithms

Lecture Transcript While and Do While Statements in C++

while (condition) { body_statements; for (initialization; condition; update) { body_statements;

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

This is the basis for the programming concept called a loop statement

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Instruction-Level Parallelism Dynamic Branch Prediction. Reducing Branch Penalties

CS/COE 1501

Without savetrees. An Example Document

CS112 Lecture: Repetition Statements

You will need the following items: scissors, plate, 5 different colored pencils, protractor, paper to answer questions

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Chapter 1: Foundations for Algebra

Agent Design Example Problems State Spaces. Searching: Intro. CPSC 322 Search 1. Textbook Searching: Intro CPSC 322 Search 1, Slide 1

Computer Systems C S Cynthia Lee Today s materials adapted from Kevin Webb at Swarthmore College

Chapter 4: Conditionals and Loops

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CS164: Midterm I. Fall 2003

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CS112 Lecture: Loops

Conditionals and Loops

Computer Aided Engineering Design Prof. Anupam Saxena Department of Mechanical Engineering Indian Institute of Technology, Kanpur.

Separable Kernels and Edge Detection

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

Other Loop Options EXAMPLE

CS 106 Introduction to Computer Science I

LECTURE 5 Control Structures Part 2

Transcription:

CS256 Computer Science I Kevin Sahr, PhD Lecture 19: Nested Conditionals and Loops 1 Nesting remember that conditional statements and loops contain single statements (or single block statements) this statement can itself be a conditional or loop a conditional or loop that occurs inside another conditional or loop is called a nested conditional or loop 2 Abstraction & Nesting abstraction is the key to writing complicated nested constructs view each entire nested conditional or loop as a single abstracted unit relative to what it is nested in 3 EX: pseudocode that outputs which of two numbers is smaller: x is less than y output x is smaller output y is smaller problem: what x and y are equal? will need to use a nested conditional 4

Abstract Pseudocode recall that we can write pseudocode at dferent levels of abstraction what matters is that you can get from the pseudocode to Java source code once we understand the - on the previous slide, we can abstract that pseudocode as: output the smaller of x and y 5 Pseudocode we can then write pseudocode to handle the case when x and y are equal: x is equal to y output equal output the smaller of x and y 6 Building the Code start with the code for the equality case: (x == y) Output.showMessage( equal ); output the smaller of x and y 7 Code now substitute in the code for the -clause unit: (x == y) Output.showMessage( equal ); (x < y) Output.showMessage( x is smaller ); Output.showMessage( y is smaller ); note no s could use but not necessary, since the nested - is a single statement relative to the outer - 8

EX: given a numeric grade (0.0-10.0), output the corresponding letter grade 9 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) this code is hard to read recall that Java doesn t care about whitespace, so we can reformat it to make our meaning clearer 10 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) 11 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) 12

(grade >= 8.0) (grade >= 7.0) (grade >= 6.0) 13 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) see Grade.java for a complete program 14 ( 2) EX: our code from earlier (x == y) Output.showMessage( equal ); (x < y) Output.showMessage( x is smaller ); Output.showMessage( y is smaller ); 15 ( 2) should be formatted: (x == y) Output.showMessage( equal ); (x < y) Output.showMessage( x is smaller ); Output.showMessage( y is smaller ); 16

input word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). let s begin by breaking this down into smaller chunks that must be accomplished 17 word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). we note that the highlighted phrase is an instance of the algorithm: positive radius we have detailed pseudocode for this algorithm (see last lecture) 18 word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). let s write an algorithm named calculate area for the highlighted portion: area = 3.14 * radius * radius output area 19 word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). note that classying the area will be a nested construct so let s abstract-out the details of that part by writing an algorithm for just that part 20

Pseudocode for Algorithm classy area area is greater than 100 output BIG area is greater than 50 output MEDIUM output SMALL 21 we now have three abstract algorithms: input positive radius calculate area classy area now we can write the entire program pseudocode, ignoring the details of these abstract sub-algorithms note that our word problem indicates that the program must handle multiple circles; we can use either: a counter-controlled loop, or a sentinel loop we choose a counter-controlled loop 22 Word Problem Program Pseudocode input positive #-of-circles do #-of-circles times input positive radius calculate area classy area now we just need to substitute in the code for each sub-unit of pseudocode see Circles2.java for the complete source code 23 Nested Loops note that this program contains an input validation loop nested inside of a counter-controlled loop when working with nested loops it is especially important to think of the inner loop as a single unit, independent of the outer loop the inner loop will execute completely every time the outer loop executes once 24

Nested Loops EX: How many times will "A" be output? count1 = 0; while (count1 < 10) count2 = 0; while (count2 < 20) count2 = count2 + 1; count1 = count1 + 1; We can use abstraction to understand this code 25 25 Nested Loops Note that the inner loop is a countercontrolled loop count1 = 0; while (count1 < 10) count2 = 0; while (count2 < 20) count2 = count2 + 1; count1 = count1 + 1; 26 26 Nested Loops let s replace the inner loop with our abstract counter-controlled loop pseudocode count1 = 0; while (count1 < 10) output A 20 times count1 = count1 + 1; 27 27 Nested Loops the outer loop is also a counter-controlled loop, so let s replace that as well repeat 10 times output A 20 times How many times will "A" be output? 10 * 20 = 200 28 28

Lecture 19 Vocabulary nested conditional nested loop 29