The for-statement.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
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.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
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.
Shorthand operators.
The character data type char
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Working with arrays (we will use an array of double as example)
Assignment statements using the same variable in LHS and RHS.
Repetition Statements while and do while loops
Bordoloi and Bock Control Structures: Iterative Control.
Mixing integer and floating point numbers in an arithmetic operation.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
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.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
The for loop.
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.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
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.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CSC111 Quick Revision.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Control Structures.
SELECTION STATEMENTS (1)
Manipulating Pictures, Arrays, and Loops part 2
Loop Control Structure.
Outline Altering flow of control Boolean expressions
LRobot Game.
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
Lecture Notes – Week 3 Lecture-2
Repetition Control Structure
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Lecture Notes – Week 4 Chapter 5 (Loops).
February , 2009 CSE 113 B.
Repetition Statements
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

The for-statement

Previously discussed: different loop-statements in Java Java provides 3 types of loop-statements: We will now study the for-statement 1. The while-statement 2. The for-statement 3. The do-while-statement  

Previously discussed: find all divisor of a number n Algorithm to find all divisors of a number n: The variable x takes on the values 1, 2, 3, ..., n one at a time For each value, we check whether n is divisible by x If so, we print the value x (and obtain all divisors) for (x = 1, 2, 3, ..., n) do { if ( n is divisible by x ) print x; // x is a divisor of n }

The for-statement The for-statement is ideally suited for making the following type of program: The for-statement can best be explained with an example.... 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.

Example for-statement: the most common way to use a for-statement The for-statement was originally invented to let some variable take on a series of value

Example for-statement: the most common way to use a for-statement (cont.) The most common form of the for-statement is as follows: for ( var = START_VALUE ; var <= STOP_VALUE ; var = var + INCR ) { /* for-body (statements) */ }

Example for-statement: the most common way to use a for-statement (cont.) Meaning: The variable var will take on the following values: For each of the value, the statements in the for-body are executed START_VALUE START_VALUE + INCR START_VALUE + 2×INCR ... Up to the largest value that is ≤ STOP_VALUE

Example for-statement: the most common way to use a for-statement (cont.) public class For01 { public static void main(String[] args) int a; // Example "for-statement" for ( a = 1 ; a <= 10 ; a = a + 1 ) System.out.println(a); // Print a } System.out.println("Done"); Output: 1 2 (1+1×1) 3 (1+2×1) 4 (1+3×1) 5 (1+4×1) 6 (1+5×1) 7 (1+6×1) 8 (1+7×1) 9 (1+8×1) 10 (1+9×1) Done

Example for-statement: the most common way to use a for-statement (cont.) Notice that in the for-statement: a = 1 specifies the starting value a <= 10 specifies when the for-loop will stop a = a + 1 specifies how the variable a will change each time through the loop

Example for-statement: the most common way to use a for-statement (cont.) Example Program: (Experiment with it yourself outside class !)               Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/For01.java How to run the program:             Right click on link and save in a scratch directory To compile:   javac For01.java To run:          java For01

Example for-statement: the most common way to use a for-statement (cont.) Example 2: changing the START_VALUE public class For01 { public static void main(String[] args) int a; // Changing the starting value: for ( a = 5; a <= 10; a = a + 1 ) System.out.println(a); // Print a } System.out.println("Done"); Output: 5 6 (5+1×1) 7 (5+2×1) 8 (5+3×1) 9 (5+4×1) 10 (5+5×1) Done

Example for-statement: the most common way to use a for-statement (cont.) Notice that in the for-statement: a = 5 specifies the starting value a <= 10 specifies when the for-loop will stop a = a + 1 specifies how the variable a will change each time through the loop

Example for-statement: the most common way to use a for-statement (cont.) Example 3: changing the STOP_VALUE public class For01 { public static void main(String[] args) int a; // Changing the continuation condition: for ( a = 1; a <= 3; a = a + 1 ) System.out.println(a); // Print a } System.out.println("Done"); Output: 1 2 (1+1×1) 3 (1+2×1) Done

Example for-statement: the most common way to use a for-statement (cont.) Notice that in the for-statement: a = 1 specifies the starting value a <= 3 specifies when the for-loop will stop a = a + 1 specifies how the variable a will change each time through the loop

Example for-statement: the most common way to use a for-statement (cont.) Example 4: changing the INCR (increment) value public class For01 { public static void main(String[] args) int a; // Changing the increment statement: for ( a = 1; a <= 10; a = a + 2 ) System.out.println(a); // Print a } System.out.println("Done"); Output: 1 3 (1+1×2) 5 (1+2×2) 7 (1+3×2) 9 (1+4×2) Done

Example for-statement: the most common way to use a for-statement (cont.) Notice that in the for-statement: a = 1 specifies the starting value a <= 10 specifies when the for-loop will stop a = a + 2 specifies how the variable a will change each time through the loop: increment by 2!!

Example for-statement: the most common way to use a for-statement (cont.) Class exercise: what is the output of this for-loop public class For01 { public static void main(String[] args) int a; // Changing the increment statement: for ( a = 4; a <= 14; a = a + 3 ) System.out.println(a); // Print a } System.out.println("Done");

Example for-statement: the most common way to use a for-statement (cont.) Answer: 4 7 (4 + 1×3) 10 (4 + 2×3) 13 (4 + 3×3) Done

Syntax and meaning of the for-statement The general syntax of the for-statement is as follows:

Example for-statement: the most common way to use a for-statement (cont.) Because: We start with the value 4 We increase the value with 3 each time Hence: 4, 7, 10, 13 The next value in the series is 16, but 16 is not ≤ 14... So we stop

Syntax and meaning of the for-statement (cont.) Explanation: The keyword for announces (to the Java compiler) that we started an for-statement Following the keyword for, there are 3 clauses: 1. An init-statement: this is a statement that is executed once at the start of the for-statement

Syntax and meaning of the for-statement (cont.) 2. A LOOP-CONTINUATION-CONDITION: this is a Boolean expression that determines whether the for-statement will be terminated (This clause has the same function as in the LOOP-CONTINUATION-CONDITION of an while-statement) 3. A INCR-STATEMENT: this is a statement that is executed at the end of the loop.

Syntax and meaning of the for-statement (cont.) The INCR-STATEMENT is executed: After the for-loop body Before the for-statement repeats to test the LOOP-CONTINUATION-CONDITION

Syntax and meaning of the for-statement (cont.) The for-statement is completed with one statement This is the body of the for-statement The body will be executed as long as the loop-continuation-condition is true !!!       After the body has been executed, the INCR-STATEMENT is executed before repeating the check of the LOOP-CONTINUATION-CONDITION

Common practice in for-statements Common practice in for-loops: Use a block as body of for-statements

Common practice in for-statements (cont.) A typical for-loop looks like this:

Flow chart of a for-statement (I have used colors to highlight the correspondence of the pieces in the Java language syntax and in the flow chart)

Flow chart of a for-statement (cont.) Flow chart representing the for-statement:

Flow chart of a for-statement (cont.) This is the program flow when LOOP-CONTINUATION-CONDITION is true:

Flow chart of a for-statement (cont.) In this case, the for-statement will: Execute the statements in the body Execute the INCR-STATEMENET Repeat the for-statement from the LOOP-CONTINUATION-CONDITION

Flow chart of a for-statement (cont.) This is the program flow when LOOP-CONTINUATION-CONDITION is false:

Flow chart of a for-statement (cont.) In this case, the for-statement will: Terminate The program will proceed with the statement that follows the for-statement

Flow chart of a for-statement (cont.) Example: for ( a = 1 ; a <= 10 ; a++ ) { System.out.println(a); } System.out.println("Done");

Flow chart of a for-statement (cont.) Flow chart of this program:

Flow chart of a for-statement (cont.) Note: this is the same flow chart we saw before belonging to this program fragment: a = 1; while ( a <= 10 ) { System.out.println(a); a++; } System.out.println("Done");

Flow chart of a for-statement (cont.) Execution: Program "loops" as long as a ≤ 10 Program exits loop when a > 10

Structure diagram of a for-statement

Structure diagram of a for-statement (cont.) Structure diagram representing a while-statement:

Structure diagram of a for-statement (cont.) How to "read" the diagram: The outer rectangle containing the LOOP-CONTINUATION-CONDITION represents the for-statement:

Structure diagram of a for-statement (cont.) The for-statement is one (single) statement) The for-statement will only complete execution when the LOOP-CONTINUATION-CONDITION becomes false

Structure diagram of a for-statement (cont.) The inner rectangle (shaded with a green color) is the while-loop body When the LOOP-CONTINUATION-CONDITION is true, the for-statement will execute: The statements in the inner rectangle (the for-statement's body) The INCR-statement