1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.

Slides:



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

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
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:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
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);
6.2 For…Next Loops General Form of a For…Next Loop
CS 112 Introduction to Programming Variables; Type Casting; Using Variables in for Loops Yang (Richard) Yang Computer Science Department Yale University.
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.
For Loop Tips And Tricks
The for loop.
Chapter 9 Control Structures.
1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
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.
1 Building Java Programs Chapter 2: Primitive Data and Definite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 9 Repetition.
Building Java Programs Chapter 2
Lecture 4: Program Control Flow
Primitive Data, Variables, Loops (Maybe)
Topic 5 for Loops -Arthur Schopenhauer
Repetition-Counter control Loop
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Building Java Programs Chapter 2
Chapter 5 Repetition.
Building Java Programs Chapter 2
Chapter 9 Control Structures.
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
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.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
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:
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:

1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS

2 AS YOU ARRIVE Think of: how many lines of code would it take to count from 1 to 100, using what we just learned so far? 100? 50? 12? 25? 6?

3 THE FOR LOOP

4 REPETITION WITH FOR LOOPS So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so smart"); System.out.println("S-M-R-T... I mean S-M-A-R-T"); Java's for loop statement performs a task many times. System.out.println("Homer says:"); // repeat 4 times for (int i = 1; i <= 4; i++) { System.out.println("I am so smart"); } System.out.println("S-M-R-T... I mean S-M-A-R-T");

5 FOR LOOP SYNTAX for ( initialization ; test ; update ) { statement ;... statement ; } Perform initialization once. Repeat the following: Check if the test is true. If not, stop. Execute the statements. Perform the update. body header

6 INITIALIZATION for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tells Java what variable to use in the loop Performed once as the loop begins The variable is called a loop counter can use any name, not just i can start at any value, not just 1

7 TEST for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tests the loop counter variable against a limit Uses comparison operators: < less than <= less than or equal to > greater than >= greater than or equal to

8 INCREMENT AND DECREMENT shortcuts to increase or decrease a variable's value by 1 ShorthandEquivalent 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

9 MODIFY-AND-ASSIGN shortcuts to modify a variable's value ShorthandEquivalent 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;

10 REPETITION OVER A RANGE System.out.println("1 squared = " + 1 * 1); System.out.println("2 squared = " + 2 * 2); System.out.println("3 squared = " + 3 * 3); System.out.println("4 squared = " + 4 * 4); System.out.println("5 squared = " + 5 * 5); System.out.println("6 squared = " + 6 * 6); Intuition: "I want to print a line for each number from 1 to 6” The for loop does exactly that! for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + (i * i)); } "For each integer i from 1 through 6, print..."

11 LOOP WALKTHROUGH 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!

12 LOOP WALKTHROUGH

13 MULTI-LINE LOOP BODY System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } System.out.println("+----+"); Output: \ / / \ \ / / \ \ / / \

14 EXPRESSIONS FOR COUNTER int highTemp = 5; for (int i = -3; i <= highTemp / 2; i++) { System.out.println(i * ); } Output:

15 SYSTEM.OUT.PRINT Prints without moving to a new line allows you to print partial messages on the same line int highestTemp = 5; for (int i = -3; i <= highestTemp / 2; i++) { System.out.print((i * ) + " "); } Output: Concatenate " " to separate the numbers

16 COUNTING DOWN The update can use -- to make the loop count down. The test must say > instead of < System.out.print("T-minus "); for (int i = 10; i >= 1; i--) { System.out.print(i + ", "); } System.out.println("blastoff!"); System.out.println("The end."); Output: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! The end.

17 TRY THIS Complete the code for the following for loop for(int I = 1; i<=6;i++){ //your code goes here } So that it prints the following, one per line