Building Java Programs

Slides:



Advertisements
Similar presentations
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Advertisements

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.
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.
©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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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.
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.
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.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
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
Primitive Data, Variables, Loops (Maybe)
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.
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Building Java Programs
Building Java Programs
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.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
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
Lecture 8:The For Loop AP Computer Science Principles.
Building Java Programs
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
The for loop suggested reading:
SSEA Computer Science: Track A
Primitive data, expressions, and variables
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
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Presentation transcript:

Building Java Programs 5/6/2019 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 1

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

Loop walkthrough 1 2 4 for (int count = 1; count <= 4; count = count + 1) { System.out.println("Hello World!"); } System.out.println("Whoo!"); Output: Hello World! Whoo! 3 5 1 2 3 4 5

Increment and decrement shortcuts to increase or decrease a variable's value by 1 Shorthand Equivalent longer version variable++; variable = variable + 1; variable--; variable = variable - 1; int x = 2; x++; // x = x + 1; // x now stores 3 double gpa = 2.5; gpa--; // gpa = gpa - 1; // gpa now stores 1.5

Modify-and-assign operators shortcuts to modify a variable's value Shorthand Equivalent longer version variable += value; variable = variable + value; variable -= value; variable = variable - value; variable *= value; variable = variable * value; variable /= value; variable = variable / value; variable %= value; variable = variable % value; x += 3; // x = x + 3; gpa -= 0.5; // gpa = gpa - 0.5; number *= 2; // number = number * 2;

Nested loops nested loop: A loop placed inside another loop. Output: 5/6/2019 Nested loops nested loop: A loop placed inside another loop. for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); // to end the line Output: ********** The outer loop repeats 5 times; the inner one 10 times. "sets and reps" exercise analogy How would we print a multiplication table? try printing each of the following inside the inner loop: System.out.print(i + " "); System.out.print(j + " "); System.out.print((i * j) + " ");

Nested for loop exercise What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); Output: * ** *** **** *****

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

5/6/2019 Common errors Both of the following sets of code produce infinite loops: for (int i = 1; i <= 5; i++) { for (int j = 1; i <= 10; j++) { System.out.print("*"); } System.out.println(); for (int j = 1; j <= 10; i++) { Both cases produce infinite loops. 21

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.