For Loop Tips And Tricks

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Chapter 6 - Visual Basic Schneider
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
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 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.
Chapter 9 Control Structures.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
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.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 9 Repetition.
Programming Logic and Design Fourth Edition, Comprehensive
Topic 5 for Loops -Arthur Schopenhauer
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5 Repetition.
Decision statements. - They can use logic to arrive at desired results
Chapter 9 Control Structures.
Building Java Programs
Building Java Programs
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
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.
Building Java Programs
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
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
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Presentation transcript:

For Loop Tips And Tricks 1. The outermost loop typically controls the number of lines printed. Nested loops typically control repeating patterns within each individual output line. Consider the following desired output: ***** and its corresponding nested “for” loop structure for (int i = 1; i <=3 ; i++) { 3 output lines for (int j = 1; j <=5; j++) { Repeated “*” System.out.print(“*”); } System.out.println();

If the length of each subsequent output line repeating pattern is getting longer, try an outermost loop that INCREMENTS. If the length of each subsequent output line repeating pattern is getting shorter, try an outermost loop that DECREMENTS. Then use the value of the outermost loop counter to affect the inner loop repeating pattern. Consider the following 2 examples: Decreasing Output Length Increasing Output Length **** * *** ** ** *** * ****

for (int i = 4; i >= 1; i--) { for (int i = 1; i <= 4; i++) { Decreasing Output Length Increasing Output Length (Outer Loop Is Decrementing) (Outer Loop Is Incrementing) **** * *** ** ** *** * **** for (int i = 4; i >= 1; i--) { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { for (int j = 1; j <= i; j++) { System.out.print(“*”); System.out.print(“*”); } } System.out.println(); System.out.println(); } }

4. There is typically one nested “for” loop inside the outer-most “for” loop for each repeating pattern in the lines of output. Consider the following two outputs: 1 ***1 *2 **22 **3 *333 ***4 4444 There is one repeating pattern (stars) in the first output. The number is not a repeating pattern because there is never more than one number on any given output line. There are 2 repeating patterns (stars and numbers) in the second output.

Considering the output with 1 repeating pattern (stars): *2 **3 ***4 Based on the tips/tricks that we’ve learned we can expect: 1. An outer-most INCREMENTING (because the output line size is getting larger) “for” loop that counts to 4 (number of output lines). 2. A single nested “for” loop for the repeating stars (“*”) that uses the value of the outermost loop counter to affect the inner repeating pattern because the number of stars is changing. 3. A print statement after the nested “for” loop to output the numeric.

Code: for (int i = 1; i <= 4; i++) { 4 output lines for (int j = 1; j < i; j++) { repeating “*” pattern System.out.print(“*”); } System.out.println(i); non-repeating numeric Output: 1 *2 **3 ***4

Now considering the output with 2 repeating patterns (stars & numbers): ***1 **22 *333 4444 Based on the tips/tricks that we’ve learned we can expect: An outer-most “for” loop that counts to 4 (number of output lines). You can either increment or decrement the outer-most loop control variable because the total number of character output per line (4) isn’t changing. 2. 2 nested “for” loops, 1 for the repeating stars (“*”) and one for the repeating numerics.

Code: for (int i = 1; i <= 4; i++) { 4 output lines for (int j = 4; j > i; j--) { Repeating “*” pattern System.out.print(“*”); } for (int k = 1; k <= i; k++) { Repeating numeric pattern System.out.print(i); System.out.println(); Output: ***1 **22 *333 4444

There are typically multiple nested “for” loops when there is a pattern of patterns in an individual output line. Consider the following desired output: ****!****!****! We have a repeating pattern (“*”) which is probably a nested “for” loop, followed by an exclamation point (“!”) that is probably printed after exiting the nested “for” loop. However, we also see a pattern of the pattern “****!”. This is an indication that an additional level of nested “for” loops is present. There are 3 repetitions of this pattern “****!” per output line, so we expect this additional nested for loop to increment from 1 to 3.

Code: for (int i = 1; i <= 2; i++) { 2 output lines for (int j = 1; j <=3; j++) { 3 repetitions of ****! for (int k = 1; k <= 4; k++) { Repeating ”*” pattern System.out.print(“*”); } System.out.print(“!”); Non-repeating “!” System.out.println(); Output: ****!****!****!