Building Java Programs

Slides:



Advertisements
Similar presentations
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
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:
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
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.
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
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.
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
For Loop Tips And Tricks
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.
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 9 Repetition.
Chapter 4 Repetition Statements (loops)
Building Java Programs Chapter 2
Lecture 4: Program Control Flow
Topic 5 for Loops -Arthur Schopenhauer
CiS 260: App Dev I Chapter 4: Control Structures II.
Building Java Programs Chapter 2
Chapter 5 Repetition.
Outline Altering flow of control Boolean expressions
Building Java Programs Chapter 2
Chapter 9 Control Structures.
Lecture Notes – Week 3 Lecture-2
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Building Java Programs
Building Java Programs
Building Java Programs
Lab5 PROGRAMMING 1 Loop chapter4.
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Loops.
Building Java Programs
The for loop suggested reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
CIS 110: Introduction to Computer Programming
Presentation transcript:

Building Java Programs Chapter 2 Lecture 3: Variables and the for Loop reading: 2.2 – 2.3 1

First Computer Program Charles Babbage Ada Lovelace 1791 – 1871 1815 – 1852 Photo by Bruno Barral License

Division 15 r1 3 46 3 16 15 1 Discrete (int) Division Continuous (double) Division 3.0 / 46.0 = 15.33… 3 / 46.0 = 15.33… 3.0 / 46 = 15.33… 46 / 3 = 15 46 % 3 = 1 15.33… 3 46.00 3 16 15 10 9 15 r1 3 46 3 16 15 1

Variables reading: 2.2 4

For loops reading: 2.3 17

for loop syntax for (initialization; test; update) { header statement; ... } Perform initialization once. Repeat the following: Check if the test is true. If not, stop. Execute the statements. Perform the update. body header Example flow Initialization Test Body Update Exit For Loop

Control structures Control structure: a programming construct that affects the flow of a program's execution Controlled code may include one or more statements The for loop is an example of a looping control structure

Loop walkthrough 1 2 4 for (int i = 1; i <= 4; i++) { System.out.println(i + " squared = " + (i * i)); } System.out.println("Whoo!"); Output: 1 squared = 1 2 squared = 4 3 squared = 9 4 squared = 16 Whoo! 3 5 1 2 3 4 5

Complex lines What nested for loops produce the following output? ....1 ...2 ..3 .4 5 We must build multiple complex lines of output using: an outer "vertical" loop for each of the lines inner "horizontal" loop(s) for the patterns within each line outer loop (loops 5 times because there are 5 lines) inner loop (repeated characters on each line)

Outer and inner loop First write the outer loop, from 1 to the number of lines. for (int line = 1; line <= 5; line++) { ... } Now look at the line contents. Each line has a pattern: some dots (0 dots on the last line), then a number ....1 ...2 ..3 .4 5 Observation: the number of dots is related to the line number.

Mapping loops to numbers for (int count = 1; count <= 5; count++) { System.out.print( ... ); } What statement in the body would cause the loop to print: 4 7 10 13 16 System.out.print(3 * count + 1 + " ");

Nested for loop exercise Make a table to represent any patterns on each line. ....1 ...2 ..3 .4 5 To print a character multiple times, use a for loop. for (int j = 1; j <= 4; j++) { System.out.print("."); // 4 dots } line # of dots 1 4 2 3 5 -1 * line -1 -2 -3 -4 -5 -1 * line + 5 4 3 2 1

Nested for loop solution Answer: for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); } System.out.println(line); Output: ....1 ...2 ..3 .4 5

Nested for loop exercise What is the output of the following nested for loops? for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); } for (int k = 1; k <= line; k++) { System.out.print(line); System.out.println(); Answer: ....1 ...22 ..333 .4444 55555

Nested for loop exercise 1/18/2019 Nested for loop exercise Modify the previous code to produce this output: ....1 ...2. ..3.. .4... 5.... Answer: for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); } System.out.print(line); for (int j = 1; j <= (line - 1); j++) { System.out.println();