The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Computer Programming Lab(4).
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Working with arrays (we will use an array of double as example)
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
Methods.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Introduction to programming in java
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Sophomore Scholars Java
Slides by Evan Gallagher
Slides by Evan Gallagher
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 5: Control Structures II
Repetition-Counter control Loop
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
Java Methods Making Subprograms.
While Statement.
Java Methods Making Subprograms.
Lecture Notes – Week 2 Lecture-2
CIS 110: Introduction to Computer Programming
Building Java Programs
The for-statement.
Building Java Programs
6.2 for Loops Example: for ( int i = 1; i
Building Java Programs
Presentation transcript:

The for-statement

Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement

The for-statement (1) The for-statement is ideally suited for making the following type of program: Let some variable x take on a series of value one at a time For each value taken on by a variable x, the body of the for-statement is executed once. repeat ( for x = 1, 2, 3, ) execute this statement;

The for-statement (2)

Flow chart of a for-statement

The most common way to use a for- statement (2)

The most common way to use a for- statement (3) public class For01 { public static void main(String[] args) { int a; for ( a = 1 ; a <= 10 ; a = a + 1 ) { System.out.println(a); // Print a } System.out.println("Done"); }

The most common way to use a for- statement (1) The for-statement was originally invented to let some variable take on a series of value for ( var = START_VALUE ; var <= STOP_VALUE ; var = var + INCR ) { /* for-body (statements) */ } For each of the value, the statements in the for-body are executed

Flow chart of this program:

Programming example 1: find all divisors of a number (1) Write a Java program that reads in an integer n... and prints all its divisors Input: n = 12 Output: 1, 2, 3, 4, 6, 12

Programming example 1: find all divisors of a number (2) Check if 12 is divisible by 1 Check if 12 is divisible by 2... Check if 12 is divisible by 12 We do not need to check numbers > 12 because only number ≤ 12 can be divisors !

Programming example 1: find all divisors of a number (3) for (x = 1, 2, 3,...., 10) do { print x; } This notation can be converted to a for-statement very naturally: for ( x = 1; x <= 10; x = x + 1 ) { print x; }

Programming example 1: find all divisors of a number (4) public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, x; n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) // Run x = 1, 2,..., n { if ( n % x == 0 ) { // x is a divisor of n System.out.println(x); }

Programming example 2: compute the sum n (1) input: n = some integer number sum = 0; // Clear the running total ! for ( x = 1, 2, 3,..., n ) do { Add x to sum } Print sum;

Programming example 2: compute the sum n (2) public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, x, sum = 0; System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) // Run x = 1, 2,..., n { sum = sum + x; // Add x to sum } System.out.println(sum); // Print final sum }

Programming example 3: compute factorial n! (1) Write a Java program that reads in an integer n... and prints the factorial n! = 1×2×3×...×n Input: n = 5 Output: 120 (because 1 × 2 × 3 × 4 × 5 = 120)

Programming example 3: compute factorial n! (2) input: n = some integer number product = 1; // Set running product to 1 for ( x = 1, 2, 3,..., n ) do { Multiply x into product } Print sum;

Programming example 3: compute factorial n! (3) public class Factorial01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, x, product = 1; System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) { product = product * x; // Multiply x into product } System.out.println(product); // Print final product }

The break and continue statements Introduction There are 2 special statements that can affect the execution of loop statements (such as a for-statement) The special statements are: ■ break ■ continue We will study their meaning and how to use these special statements inside the while-statement

Effect of the break and continue statements on the for-statement (1)

The continue statement Syntax: continue; Effect: ◦When the continue statement is executed inside a loop- statement, the program will skip over the remainder of the loop-body to the end of the loop body ◦Note: ■What happens next when the program reaches the end of a loop depends on the type of loop statement !!!

Effect of the break and continue statements on the for-statement (2)