Topic 5 for Loops -Arthur Schopenhauer

Slides:



Advertisements
Similar presentations
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:
Advertisements

CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
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.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Copyright © Texas Education Agency, Computer Programming For 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.
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);
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:
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
The for loop.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UCT Department of Computer Science Computer Science 1015F Iteration
Adapted from slides by Marty Stepp and Stuart Reges
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Introduction To Repetition The for loop
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
CSC240 Computer Science III
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Control Statements Loops.
Building Java Programs
Building Java Programs
CMPT 102 Introduction to Scientific Computer Programming
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.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
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
The for loop suggested reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Repetition Statements (Loops) - 2
Control Statements Loops.
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
Looping Structures.
CIS 110: Introduction to Computer Programming
Chapter 4: Loops and Iteration
Presentation transcript:

Topic 5 for Loops -Arthur Schopenhauer “Always to see the general in the particular is the very foundation of genius.” -Arthur Schopenhauer Based on slides for Building Java Programs by Reges/Stepp, found at http://faculty.washington.edu/stepp/book/ CS305j Introduction to Computing for Loops

Explain and look at the syntax and examples of What we will do today Explain and look at the syntax and examples of for loops CS305j Introduction to Computing for Loops

System.out.print command System.out.println prints a line of output and then advances to a new line. Java has another command named System.out.print that prints the given output without moving to the next line. This allows you to print partial messages that can appear on the same line as each other. Example: System.out.print("Olivia and"); System.out.print("Isabelle are"); System.out.println("my daughters."); System.out.print("I am Kelly's husband."); Output: Olivia andIsablle aremy daughters. I am Kelly's husband. CS305j Introduction to Computing for Loops

Repetition with for loops So far, when we wanted to perform a task multiple times, we have written redundant code: System.out.println("CS305J"); System.out.println(); // print 5 blank lines System.out.println(); System.out.println("Introduction to Computing"); Java has a statement called a for loop statement that instructs the computer to perform a task many times: for (int i = 1; i <= 5; i++) { } CS305j Introduction to Computing for Loops

The for loop for loop: A block of Java code that executes a group of statements repeatedly until a given test fails. General syntax: for (<initialization> ; <test> ; <update>) { <statement(s)> ; } The top line is usually called the loop header, and the statement(s) inside are called the loop body. The <initialization> usually declares a loop counter variable that is used in the test, update, and body of the loop. Semantics (behavior) of for loop: Start out by performing the <initialization> once. if the <test> is a true statement execute all the statements in the body of the loop in the order they appear, one time After each time the loop body is executed, perform the <update> and then check the <test> again. if the <test> is a false skip to the first statement after the body of the loop CS305j Introduction to Computing for Loops

Example for loop of ints The simplest way to use a for loop is to loop over integers in a given range: for (int i = 1; i <= <value>; i++) Example: for (int i = 1; i <= 6; i++) { System.out.println(i + " squared is " + (i * i)); } Output: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 6 squared is 36 CS305j Introduction to Computing for Loops

For loop flow diagram The following flow diagram describes the execution of a for loop: test is false execute initialization check the test skip to 1st statement after body of loop test is true execute statements in body of loop execute update CS305j Introduction to Computing for Loops

Loop walkthrough Let's walk through the following for loop: int i = 1; for (int i = 1; i <= 3; i++) { System.out.println(i + " squared is " + (i * i)); } int i = 1; is 1 <= 3? Yes, so execute the body and update. System.out.println(1 + " squared is " + (1 * 1)); i++; // i = 2 is 2 <= 3? Yes, so execute the body and update. System.out.println(2 + " squared is " + (2 * 2)); i++; // i = 3 is 3 <= 3? Yes, so execute the body and update. System.out.println(3 + " squared is " + (3 * 3)); i++; // i = 4 is 4 <= 3? No, so exit the loop. CS305j Introduction to Computing for Loops

Another example for loop System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } Output: +----+ \ / / \ CS305j Introduction to Computing for Loops

Single-line for loop When a for loop only has one statement in its body, the { } braces may be omitted. for (int i = 1; i <= 6; i++) System.out.println(i + " squared is " + (i * i)); However, this can lead to mistakes where a line appears to be inside a loop, but is not: for (int i = 1; i <= 3; i++) System.out.println("This is printed 3 times"); System.out.println("So is this... or is it?"); Output: This is printed 3 times So is this... or is it? CS305j Introduction to Computing for Loops

Some for loop variations The initial and final values for the loop counter variable can be arbitrary numbers or expressions: Example: for (int i = -3; i <= 2; i++) { System.out.println(i); } Output: -3 -2 -1 1 2 If the loop counter i was initialized to 1 and the test was changed to i <= 6, how would you change the loop body to get the same output show to the left? CS305j Introduction to Computing for Loops

Complex Example Complex Example: for (int i = 1 + 3 * 4; i <= 5298 % 100; i++) System.out.println(i + " squared is " + (i * i)); The above for loops is syntactically correct but it is difficult to understand. Strive to make you for loops, like your code, easy to understand. CS305j Introduction to Computing for Loops

Downward-counting for loop The update can also be a -- or other operator, to make the loop count down instead of up. This also requires changing the test to say >= instead of <= System.out.print("T-minus "); for (int i = 5; i >= 1; i--) { System.out.print(i + " "); } System.out.println("Blastoff!"); Output: T-minus 5 4 3 2 1 Blastoff! Rewrite this using an upward counting loop CS305j Introduction to Computing for Loops

Degenerate loops Some loops execute 0 times, because of the nature of their test and update. // The loop for (int i = 10; i < 5; i++) { System.out.println("How many times do I print?"); } Some loops execute endlessly (or far too many times), because the loop test never fails. These are called infinite loops. // An infinite loop. for (int i = 10; i >= 1; i++) { System.out.println("Runaway Java program!!!"); CS305j Introduction to Computing for Loops

for Loop Exercises Write a for loop to produce the following output 30, 20, 10, 0, -10, -20 -7, -3, 1, 5, 9, 13 A single line of output with 200 !'s 1,000,000 !'s, with 5 !'s per line. Loops = power! CS305j Introduction to Computing for Loops

useful for loop example Heron's method for calculating square roots problem: Find sqrt(n) Algorithm: Make a guess at the solution. (x1) x2 = (x1 + (n / x1)) / 2 Repeat for x3, x4, x5, ... Write a Java program that implements Heron's method to find the square root of 133,579 using 20 iterations of the algorithm. CS305j Introduction to Computing for Loops