Lesson 36: for() Loops (W11D1)

Similar documents
Lesson 26: ArrayList (W08D1)

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Lesson 12: OOP #2, Accessor Methods (W03D4)

Lesson 38: Conditionals #2 (W11D3)

Lesson 24: Recursive Algorithms #1 (W07D3)

Lesson 39: Conditionals #3 (W11D4)

Lesson 04: Our First Java Program (W01D4

Repetition with for loops

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

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

CIS October 16, 2018

CIS March 1, 2018

Object-Oriented Programming

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Final Examination Semester 2 / Year 2011

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Building Java Programs

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

JAVA OPERATORS GENERAL

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

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

Selenium Class 9 - Java Operators

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

BSc (Hons) Computer Science with Network Security/ BSc (Hons) Software Engineering/ BSc (Hons) Web Technologies. Examinations for 2016 Semester 1

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

University of Palestine. Mid Exam Total Grade: 100

Zheng-Liang Lu Java Programming 45 / 79

Topic 6 Nested for Loops

Introduction to Java Programming

Contents. 8-1 Copyright (c) N. Afshartous

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Exercise (Revisited)

St. Edmund Preparatory High School Brooklyn, NY

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Variables and Java vs C++

Introduction to the Java Basics: Control Flow Statements

Over and Over Again GEEN163

CIS November 14, 2017

King Saud University College of Computer and Information Sciences Computer Science Department

CIS 110: Introduction to Computer Programming

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

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

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

TOPIC 8 MORE ON WHILE LOOPS

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

Loops. CSE 114, Computer Science 1 Stony Brook University

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

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

Fundamentals of Programming Data Types & Methods

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Building Java Programs

( &% class MyClass { }

*Java has included a feature that simplifies the creation of

Lesson 35..Two-Dimensional Arrays

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

CIS October 19, 2017

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Operators Questions

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CS 101 Spring 2007 Midterm 2 Name: ID:

APCS Semester #1 Final Exam Practice Problems

Building Java Programs

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

Last Class. More on loops break continue A bit on arrays

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

Recap: Assignment as an Operator CS 112 Introduction to Programming

Arrays. 1 Index mapping

CS1150 Principles of Computer Science Loops (Part II)

1.00 Lecture 4. Promotion

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Building Java Programs

PSP Preparatory Exercise - Solution. Time Recording Log. Student I. C. Light Date 1/12/99 Instructor W. S. Humphrey Class CS 225

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

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

Nested Loops. A loop can be nested inside another loop.

Warmup : Name that tune!

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

CS 112 Introduction to Programming

Example: Monte Carlo Simulation 1

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

AP COMPUTER SCIENCE A

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

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

Transcription:

Lesson 36: for() Loops (W11D1) Balboa High School Michael Ferraro October 26, 2015 1 / 27

Do Now Create a new project: Lesson36 Write class FirstForLoop: Include a main() method: public static void main(string[] args) { printevenstoten(); Implement printevenstoten(): //using a while() loop, print the even //numbers from 2 to 10, inclusive... 2 / 27

Aim Students will learn how to write and use for() loops and work on 5 programming exercises from PS #5. 3 / 27

printevenstoten() via Modulo (%) int i = 2; while ( i <= 10 ) { if ( i % 2 == 0 ) { //even i++; 4 / 27

printevenstoten(), Another Way int i = 2; while ( i <= 10 ) { i += 2; 5 / 27

printevenstoten(), Another Way int i = 2; while ( i <= 10 ) { i += 2; //what other stopping conditions would work? 6 / 27

printevenstoten(), Another Way int i = 2; while ( i < 11 ) { //and... i += 2; 7 / 27

printevenstoten(), Another Way int i = 2; while ( i < 12 ) { i += 2; 8 / 27

Important Parts of a while() Loop int i = 2; while ( i <= 10 ) { i += 2; Initialization 9 / 27

Important Parts of a while() Loop int i = 2; while ( i <= 10 ) { i += 2; Initialization Stopping Condition 10 / 27

Important Parts of a while() Loop int i = 2; while ( i <= 10 ) { i += 2; Initialization Stopping Condition Loop Variant 11 / 27

Another Look at for() We ve used for() loops before: ArrayList<String> bones = new ArrayList<String>(); bones.add("femur"); bones.add("fibula"); bones.add("tibia"); for( String bone : bones ) { System.out.println( bone ); This is the for-each or enhanced form of the for() loop 12 / 27

Another Look at for() The other use for a for() loop is to function much like a while() loop. Update printevenstoten(): int i = 2; for ( ; i <=10 ; ) { i += 2; 13 / 27

Syntax of for() Recall the important aspects of a while() loop: Initialization Stopping Condition Loop Variant 14 / 27

Syntax of for() Recall the important aspects of a while() loop: Initialization Stopping Condition Loop Variant Syntax of a for() loop: for( init n ; stopping cond n ; loop variant ) {... 15 / 27

Syntax of for() Recall the important aspects of a while() loop: Initialization Stopping Condition Loop Variant Syntax of a for() loop: for( init n ; stopping cond n ; loop variant ) {... Any part may be omitted, as you saw in our updated printevenstoten() 16 / 27

Making Better Use of for() Update printevenstoten() again... int i = 2; for ( ; i <=10 ; ) { i += 2; 17 / 27

Making Better Use of for() Update printevenstoten() again... int i; for ( i = 2 ; i <=10 ; ) { i += 2; 18 / 27

Making Better Use of for() Update printevenstoten() again... int i; for ( i = 2 ; i <=10 ; i += 2 ) { 19 / 27

Making Better Use of for() Even the declaration of i can be moved: for ( int i = 2 ; i <=10 ; i += 2 ) { This is much more compact than a while() loop! 20 / 27

An Important Difference How high does i get? for ( int i = 2 ; i <=10 ; i += 2 ) { System.out.println("i = " + i); Try this out and see if you can identify the problem. 21 / 27

An Important Difference How high does i get? for ( int i = 2 ; i <=10 ; i += 2 ) { System.out.println("i = " + i); Conclusion: Vars declared inside for() loop have scope within the loop only. 22 / 27

Exercise #1 Inside main(), write an equivalent set of statements using for() that mimics the following: int a = 0; while ( a <= 6 ) { System.out.println("a is " + a); a++; 23 / 27

Exercise #2 Inside main(), write an equivalent set of statements using for() that mimics the following: int n = 6; int a = 0; while ( a < n ) { System.out.println("a is " + a); a += 2; 24 / 27

Exercise #3 Inside main(), write an equivalent set of statements using for() that mimics the following: int i = 10; while ( i > 0 ) { System.out.println("i is " + i); i--; 25 / 27

Exercise #4 Inside main(), write an equivalent set of statements using for() that mimics the following: int i = 10; while ( i > 0 ) { System.out.println("i is " + i); i--; System.out.println("i reaches " + i); 26 / 27

HW Continue working on 5 of PS #5 make sure you re done by next class! 27 / 27