CIS 110: Introduction to Computer Programming

Similar documents
Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

CIS 110: Introduction to Computer Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Recap: Assignment as an Operator CS 112 Introduction to Programming

Repetition with for loops

Building Java Programs Chapter 2

CS 112 Introduction to Programming

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

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Topic 6 Nested for Loops

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Building Java Programs

Building Java Programs

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Top-Down Program Development

Primitive data, expressions, and variables

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

Give one example where you might wish to use a three dimensional array

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CIS 110: Introduction to Computer Programming

Warmup : Name that tune!

Topic 6 loops, figures, constants

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Building Java Programs

Building Java Programs

TESTING AND DEBUGGING

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key for review problems

Last Class. While loops Infinite loops Loop counters Iterations

CSCI 1103: Introduction

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

Chapter 4: Control structures. Repetition

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Review problems

Chapter 7. Iteration. 7.1 Multiple assignment

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

1.00 Lecture 5. Floating Point Anomalies

Chapter 4: Control structures

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

CONDITIONAL EXECUTION

CS 112 Introduction to Programming

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

1.3 Conditionals and Loops

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Computer Science is...

Final Exam. COMP Summer I June 26, points

Repetition CSC 121 Fall 2014 Howard Rosenthal

Java Coding 3. Over & over again!

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

AP Computer Science Unit 1. Programs

CS 101 Fall 2005 Midterm 2 Name: ID:

Computer Science is...

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 177 Week 15 Recitation Slides. Review

A Foundation for Programming

Chapter 4 Loops. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Lecture 12: Barrier Synchronization

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

CS 112 Introduction to Programming

Darrell Bethea May 25, 2011

COMP-202 Unit 4: Programming with Iterations

CSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

CS 177 Week 5 Recitation Slides. Loops

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Example: Monte Carlo Simulation 1

CS 112 Introduction to Programming

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

STUDENT LESSON A12 Iterations

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

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

Scope of this lecture. Repetition For loops While loops

CIS 110: Introduction to Computer Programming

Introduction to Computer Programming

CIS 110: Introduction to Computer Programming

Introduction to Java Unit 1. Using BlueJ to Write Programs

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

Topic 5 for loops and nested loops

ALICE: An introduction to progamming

Introduction to Java & Fundamental Data Types

Exercise (Revisited)

1.3 Conditionals and Loops

ECE 122. Engineering Problem Solving with Java

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

Introduction to Object-Oriented Programming

Transcription:

CIS 110: Introduction to Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 1

Outline 1. For-loops! 2. Algorithm Design and Pseudocode 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 2

Announcements Date of the final is tentatively: MONDAY, DECEMBER 19 th, 6-8 PM If you have a conflict, please let me know ASAP. Need more practice? Try Practice-it! Web-based tool where you can work on practice problems that are automatically checked online. Linked off of the course webpage. 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 3

For Loops 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 4

Redundancy in Patterns Problem: write a program that prints out successive cubes. Output: 0^3 = 0 1^3 = 1 2^3 = 8 3^3 = 27 4^3 = 64 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 5

A Solution with Our Current Tools public class Cubes { public static void main(string[] args) { System.out.println( 0^3 = + 0 * 0 * 0); System.out.println( 1^3 = + 1 * 1 * 1); System.out.println( 2^3 = + 2 * 2 * 2); System.out.println( 3^3 = + 3 * 3 * 3); System.out.println( 4^3 = + 4 * 4 * 4); Pretty obvious repetition, but we have no way of dealing with it 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 6

Our First For-loop public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); The for loop construct allows us to express repetitive patterns in a structured way. 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 7

The For-loop The For-loop is a control statement. Doesn t do anything on its own, but instead controls the execution of other statements. Initialization Continuation Test Update Body System.out.println(i + ^3 = + i * i * i); 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 8

For-loop Semantics Loop iteration Test is true Initialization Test Body Update Next int i = 0 i < 5 println i++ Test is false 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 9

Our Example, Step-by-step (1) public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); Iteration # Value of i Test Output 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 10

Our Example, Step-by-step (2) public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); Iteration # Value of i Test Output 1 0 0 < 5 is true 0^3 = 0 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 11

Our Example, Step-by-step (3) public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); Iteration # Value of i Test Output 1 0 0 < 5 is true 0^3 = 0 2 1 1 < 5 is true 1^3 = 1 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 12

Our Example, Step-by-step (4) public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); Iteration # Value of i Test Output 1 0 0 < 5 is true 0^3 = 0 2 1 1 < 5 is true 1^3 = 1 3 2 2 < 5 is true 2^3 = 8 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 13

Our Example, Step-by-step (5) public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); Iteration # Value of i Test Output 1 0 0 < 5 is true 0^3 = 0 2 1 1 < 5 is true 1^3 = 1 3 2 2 < 5 is true 2^3 = 8 4 3 3 < 5 is true 3^3 = 27 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 14

Our Example, Step-by-step (6) public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); Iteration # Value of i Test Output 1 0 0 < 5 is true 0^3 = 0 2 1 1 < 5 is true 1^3 = 1 3 2 2 < 5 is true 2^3 = 8 4 3 3 < 5 is true 3^3 = 27 5 4 4 < 5 is true 4^3 = 64 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 15

Our Example, Step-by-step (7) public class Cubes { public static void main(string[] args) { System.out.println(i + ^3 = + i * i * i); Iteration # Value of i Test Output 1 0 0 < 5 is true 0^3 = 0 2 1 1 < 5 is true 1^3 = 1 3 2 2 < 5 is true 2^3 = 8 4 3 3 < 5 is true 3^3 = 27 5 4 4 < 5 is true 4^3 = 64 6 5 5 < 5 is false 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 16

For-loop Bounds Different sorts of bounds are possible! for (int i = 1; i <= 5; i++) { System.out.print(i + ); for (int i = 5; i > 0; i--) { System.out.print(i + ); for (int i = 256; i > 0; i /= 2) { System.out.print(i + ); for (int i = -3; i < 10; i += 2) { System.out.print(i + ); Output: 1 2 3 4 5 Output: 5 4 3 2 1 Output: 256 128 64 32 16 8 4 2 1 Output: -3-1 1 3 5 7 9 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 17

< versus <= Bounds i = 0; i < 5 versus i = 1; i <= 5 : Both give 5 loop iterations. i = 0, 1, 2, 3, 4 versus i = 1, 2, 3, 4, 5. i = 0; i < 5 is less intuitive, more canonical Most computations are naturally zero-based. For homework 2, either is fine. Try to get used to the zero-based style. 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 18

Yo Dawg, I Heard You Liked Loops Problem: draw a rectangle of stars. ***** ***** ***** ***** ***** 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 19

Solution: Loopception public class Rectangle { public static void main(string[] args) { // The outer for-loop controls the #/lines // The inner for-loop controls the // contents of a single line. for (int j = 0; j < 5; j++) { System.out.print( * ); System.out.println(); 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 20

Careful with Your Nested Loop for (int j = 0; j < 5; j++) { System.out.print( * ); System.out.println(); for (int j = 0; j < i; j++) { System.out.print( * ); System.out.println(); for (int j = 0; j < 5; i++) { System.out.print( * ); System.out.println(); Bounds! ***** ***** ***** ***** ***** 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 21 * ** *** **** Infinite loop!

Algorithm Design and Pseudocode 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 22

Remember Compilation? You (the programmer) The compiler The computer (your new best friend) PUT THE JELLY ON THE BREAD AND THEN PUT IT ALL IN YOUR MOUTH Ilagay ang halaya SA tinapay AT pagkatapos a y ilagay ito LAHAT SA INYONG bibig The computer program Machine code 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 23

English is Still Useful! Sometimes it is difficult to write an algorithm/computer program directly. Problem: ***** ***** ***** ***** ***** Draw 5 rows each containing 5 stars Pseudocode! /*??? */ 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 24

Pseudocode Helps Organize Your Thoughts Stuck and don t know how to make progress? Write an English description of your solution. Transform that English description into code. Draw 5 rows each containing 5 stars for (Each of 5 rows) { Draw 5 stars. for (Each of 5 rows) { System.out.print( * ); for (int j = 0; j < 5; j++) { System.out.print( * ); System.out.println(); 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 25

By Example: A Cone Problem: draw the following cone shape. /\ /--\ /----\ /------\ /--------\ Draw 5 rows each containing a section of the cone. Draw a section of the cone System.out.println(); 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 26

Cone Sections Each line has the following form: <spaces> / <dashes> \ Let s find the pattern for each part! /\ /--\ /----\ /------\ /--------\ Iteration/row (i) Spaces / Dashes \ 0 4 1 0 1 1 3 1 2 1 2 2 1 4 1 3 1 1 6 1 4 0 1 8 1 Formula for spaces: 4 - i Formula for dashes: i * 2 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 27

From Tables to Code Draw a section of the cone System.out.println(); Draw spaces Draw / Draw dashes Draw \ System.out.println(); for (int j = 0; j < 4 i; j++) { System.out.print( ); System.out.print( / ); for (int j = 0; j < i * 2; j++) { System.out.print( - ); System.out.print( \\ ); System.out.println(); 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 28

Stop Being So Constant What value controls the height of the cone? for (int j = 0; j < 4 i; j++) { System.out.print( ); System.out.print( / ); for (int j = 0; j < i * 2; j++) { System.out.print( - ); System.out.print( \\ ); System.out.println(); The height of the cone but not immediately obvious! An example of a magic number. Surprise! An indirect use of the height of the cone. If we change the height, then this number needs to change as well! 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 29

(Bad) Example: Quake III Arena float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; i = 0x5f3759df - ( i >> 1 ); y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); Note: code is written in C but should be (somewhat) understandable! This is the exact source from the game! The constant was a source of much debate! See Fast Inverse Square Root @ Wikipedia for more details. // evil floating point bit level hacking // what the fuck? // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 30

Solution to Magic Numbers: Class Constants Class constants let us document magic numbers by naming them and giving us a central place to change their values if necessary. public class Cone { public static final int HEIGHT = 5; public static void main(string[] args) { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < (HEIGHT 1) i; j++) { System.out.print( ); System.out.print( / ); for (int j = 0; j < i * 2; j++) { System.out.print( - ); System.out.print( \\ ); System.out.println(); 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 31

Syntax of Class Constants public class Cone { public static final int HEIGHT = 5; public static void main(string[] args) { for (int i = 0; i < HEIGHT; i++) { /* Snip! */ Constants are declared outside of methods but inside the class. public static final <type> <name> = <value>; Constants are variables that cannot be reassigned. Convention is to NAME_THEM_LIKE_THIS. 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 32