CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
The Mathematics Behind the Fast Inverse Square Root Function Code
Advertisements

Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Building Java Programs
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
The for loop.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 6 More Conditionals and Loops
CSCI 161 – Introduction to Programming I William Killian
Topic 5 for Loops -Arthur Schopenhauer
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
Building Java Programs
The for loop suggested reading:
Building Java Programs
Building Java Programs
CIS 110: Introduction to Computer Programming
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

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

CIS 110 (11fa) - University of Pennsylvania Announcements Date of the final is tentatively: MONDAY, DECEMBER 19th, 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/10/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania For Loops 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

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/10/2019 CIS 110 (11fa) - University of Pennsylvania

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/10/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Our First For-loop public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } The for loop construct allows us to express repetitive patterns in a structured way. 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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. Continuation Test Initialization Update Body for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

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

Our Example, Step-by-step (1) public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } Iteration # Value of i Test Output 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

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

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

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

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

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

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

CIS 110 (11fa) - University of Pennsylvania 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--) { for (int i = 256; i > 0; i /= 2) { for (int i = -3; i < 10; i += 2) { 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/10/2019 CIS 110 (11fa) - University of Pennsylvania

“<“ 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/10/2019 CIS 110 (11fa) - University of Pennsylvania

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

Solution: Loopception public class Rectangle { public static void main(String[] args) { // The outer for-loop controls the #/lines for (int i = 0; i < 5; i++) { // 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/10/2019 CIS 110 (11fa) - University of Pennsylvania

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

Algorithm Design and Pseudocode 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania By Example: A Cone Problem: draw the following cone shape. /\ /--\ /----\ /------\ /--------\ “Draw 5 rows each containing a section of the cone.” for (int i = 0; i < 5; i++) { Draw a section of the cone System.out.println(); } 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

Stop Being So Constant What value controls the height of the cone? The height of the cone but not immediately obvious! An example of a magic number. for (int i = 0; i < 5; i++) { 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(); Surprise! An indirect use of the height of the cone. If we change the height, then this number needs to change as well! 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

(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; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return 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. 9/10/2019 CIS 110 (11fa) - University of Pennsylvania

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/10/2019 CIS 110 (11fa) - University of Pennsylvania

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/10/2019 CIS 110 (11fa) - University of Pennsylvania