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.

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

Introduction to Programming
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS 1 Copyright: FALL 2014 Illinois Institute of Technology_ George Koutsogiannakis.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
1 Repetition structures Overview while statement for statement do while statement.
Exception Handling. Lecture Objectives To learn how to throw exceptions To be able to design your own exception classes To understand the difference between.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
18 File handling1June File handling CE : Fundamental Programming Techniques.
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.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
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.
11 Chapter 4 LOOPS AND FILES CONT’D. 22 SENTINEL-CONTROLLED LOOPS Suppose we did not know the number of grades that were to be entered. Maybe, a single.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
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.
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.
Shorthand operators.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Working with arrays (we will use an array of double as example)
Assignment statements using the same variable in LHS and RHS.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Chapter 5: Control Structures II
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
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.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
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.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CSC111 Quick Revision.
Chapter 6 Loops and Files
OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS
Introduction to programming in java
Chapter 5: Control Structures II
Primitive Data, Variables, Loops (Maybe)
Computer Programming Methodology Introduction to Java
Repetition-Counter control Loop
Selected Topics From Chapter 6 Iteration
TK1114 Computer Programming
SELECTION STATEMENTS (1)
While Statement.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
LRobot Game.
Repetition Statements
The for-statement.
Presentation transcript:

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 clause of an if-statement)

Flow chart of a while -statement (1)

Flow chart of a while -statement (2) When the loop-cont-condition is true, the execution takes the downward branch: ■It executes the statements which for the body of the while- statement ■Then it goes back and retests the loop-cont-condition When the loop-cont-condition is false, the execution takes the side branch ■In this case, the execution proceeds (continues) with the statement following the while-statementI.e.,: the while- statement is completed

Structure diagram representing a while-statement

Example while-statement: print the numbers 1 through 10 public class While01 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) // While-statement { System.out.println(a); // Print a a++; // Increment a } System.out.println("Done"); System.out.println("Exit: a = " + a); }

Computer programming (1): Find all divisor of the number 12 I think you would have done this: Check if 12 is divisible by 1 Check if 12 is divisible by 2... Check if 12 is divisible by 12 Note: We do not need to check numbers > 12 because only number ≤ 12 can be divisors ! When the remainder of the division is equal to 0, we print out the divisor

Computer programming (2): Rough algorithm (pseudo code) to find all divisors: input: n = some integer number for (x = 1, 2, 3,..., n) do { if ( n is divisible by x ) then print x; (because x is a divisor !) } We have basically written down what we would do to find all divisors in a pseudo programming language !!!

Computer programming (3): int x = 1 while ( x <= n ) // Run x = 1, 2,..., n { if ( n % x == 0 ) { System.out.println(x); // Print x (because it's a divisor) } x++; // Make sure we more to the next number !! }

Programming example 2: find all common divisors of 2 numbers (1) Problem description: ■Write a Java program that reads in 2 numbers x and y... ■and prints all numbers that are divisors of both x and y A concrete example: ■Input: x = 24 and y = 16 ■Output: 1, 2, 4, 8

Programming example 2: find all common divisors of 2 numbers (2) input x, y; min = min(x, y); // this is the range of the brute force search for (a = 1, 2,...., min) do { if (x and y are divisible by a) { print a; }

Programming example 2: find all common divisors of 2 numbers (3) while ( a <= min ) // Run a = 1, 2,..., min(x,y) { if ( x % a == 0 && y % a == 0 ) { // a is a divisor of x and y System.out.println(a); } a++; }

The break statement When the break statement is executed inside a loop-statement, the loop- statement is terminated immediately The execution of the program will continue with the statement following the loop-statement

The continue statement

Using the while-statement to process data files What is a file: ■File = an electronical document stored inside a computer system that contains information (data) ■A file can be created by humans using a computer program called an editor (e.g., gedit) ■A file can also be created when a computer program needs to store its output data.

General procedure to access a data file General procedure in computer programming to read data from a data file Open the data file With the information X, you can then use a "read something from a file" method to read data from the opened file There are other helpful methods on an opened file.Some method let you check if you have reached the end of the file

Opening a data file How to open a file in Java: File myFile; // Define a "File" type variable myFile = new File("Path-name-of-the-file"); The variable myFile contains information about the opened file The variable myFile will be used in read operations

Scanning an opened file (1) Construct a Scanner object using an opened file: File myFile; // Define a "File" type variable myFile = new File("Path-name-of-the-file"); // Open the file Scanner in; // Define a Scanner typed variable in = new Scanner(myFile); // Construct a Scanner that read // data from opened file"myFile"

Scanning an opened file (2) From this point onwards, you can use ◦in.nextDouble() to read a floating point number from the data file ◦in.nextInt() to read an integer number from the data file ◦in.next() to read a string (word) from the data file

Checking for available input data There are very useful methods available in the Scanner class to test if the input file is empty (exhausted) or not. Check function for input availability on Scanner typed variable in : ■in.hasNextDouble() ■in.hasNextInt() ■in.hasNext()

Programming example : print the content of a data file (1) Open the file "inp1" Construct a Scanner object using the opened file as long as ( there is data in the Scanner object ) { read a word from the Scanner object; print the word; }

Programming example : print the content of a data file (2) import java.io.*; import java.util.Scanner; public class File01 { public static void main(String[] args) throws IOException { File myFile = new File("inp1"); // Open file "inp1" Scanner in = new Scanner(myFile); String x; // Variable to receive a string while ( in.hasNext() ) { x = in.next(); // Read a string (word) System.out.println(x); // Print string read } System.out.println("Done"); }