The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.

Slides:



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

Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Program Design and Development
Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
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)
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.
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
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
CIS Computer Programming Logic
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.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
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.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
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.
Arithmetic expressions containing Mathematical functions.
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.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
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.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Chapter 4 Repetition Statements (loops)
Loop Control Structure.
Outline Altering flow of control Boolean expressions
LRobot Game.
The Boolean (logical) data type boolean
Lecture Notes – Week 3 Lecture-2
PROGRAM FLOWCHART Iteration Statements.
The for-statement.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Looping and Repetition
Presentation transcript:

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 its body as long as a loop conditional is satisfied. I.e.: the loop-statement is executed until the loop condition is not valid

The loop statements in Java (cont.) Purpose of loop-statements: A loop-statement can accomplish a complex task incrementally using smaller steps The body of a loop-statement achieves only a small step towards completing a complex task By executing the body repeatedly, the algorithm can perform the task incrementally using smaller (simpler) steps

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

Different loop-statements in programming languages (cont.) The main loop-statement is the while-statement The for-statement and the do-while-statement can be re- written as a while-statement (but the result can be very verbose) We will first study the while-statement

Syntax and meaning of the while-statement Syntax of the while-statement:

Syntax and meaning of the while-statement (cont.) Explanation: The keyword while announces (to the Java compiler) that we started an while-statement A conditional clause ( LOOP-CONTINUATION- CONDITION ) follows the keyword while The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition clause of an if-statement) This is the condition of the while-statement

Syntax and meaning of the while-statement (cont.) Following the loop-continuation-condition clause, you can write (only) one statement This is the body of the while-statement The body will be executed as long as the loop- continuation-condition is true !!!

Common practice in loop-statements Fact: Common practice in while-loops: The body of any loop-statement will almost always contains multiple statements Use a block as body of loop-statements

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

Example while-statement: print the numbers 1 through 10 Consider the following Java program: 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); }

Example while-statement: print the numbers 1 through 10 (cont.) Execution of this Java program: Program begins: The program reserve memory space for the variable a

Example while-statement: print the numbers 1 through 10 (cont.) Then executes the assignment statement: Stores the value 1 into the variable a

Example while-statement: print the numbers 1 through 10 (cont.) Then executes the while-statement. First, the loop-continuation-condition is tested: The result is true and the while-body is executed

Example while-statement: print the numbers 1 through 10 (cont.) Execution of the while-statement's body: System.out.println(a);

Example while-statement: print the numbers 1 through 10 (cont.) a++;

Example while-statement: print the numbers 1 through 10 (cont.) When the execution of the while-body is completed:

Example while-statement: print the numbers 1 through 10 (cont.) the while-statement resumes from the start again !!!

Example while-statement: print the numbers 1 through 10 (cont.) The execution the while-statement continues. First, the loop-continuation-condition is tested again: The result is true and the while-body is executed

Example while-statement: print the numbers 1 through 10 (cont.) Execution of the while-statement's body: System.out.println(a);

Example while-statement: print the numbers 1 through 10 (cont.) a++;

Example while-statement: print the numbers 1 through 10 (cont.) When the execution of the while-body is completed:

Example while-statement: print the numbers 1 through 10 (cont.) the while-statement resumes from the start again !!!

Example while-statement: print the numbers 1 through 10 (cont.) And so on... The while statement will continue executing until a = 11 because then the loop-continuation-condition a ≤ 10 becomes false (which will cause the while statement to terminate). This while-statement will loop 10 times !

Example while-statement: print the numbers 1 through 10 (cont.) Result: The number 1, 2,..., 10 will be printed (and the variable a will have the value 11).

Example while-statement: print the numbers 1 through 10 (cont.) Example Program: (Demo above code) –Prog file: While01.java How to run the program: Right click on link and save in a scratch directory To compile: javac While01.java To run: java While01

Example while-statement: print the numbers 1 through 10 (cont.) Output: Done Exit: a = 11

Flow chart of a while-statement While-statement:

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

Flow chart of a while-statement (cont.) How to "read" the flow chart: 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

Flow chart of a while-statement (cont.) 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-statement I.e.,: the while-statement is completed

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

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

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

Structure diagram of a while-statement While-statement:

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

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

Structure diagram of a while-statement (cont.) The while-statement is one (single) statement) The while-statement will only complete execution when the LOOP-CONTINUATION-CONDITION becomes false The inner rectangle (shaded with a green color) is the while-loop body The statements in the inner rectangle are executed when the LOOP-CONTINUATION-CONDITION is true

Structure diagram of a while-statement (cont.) Example: a = 1; while ( a <= 10 ) { System.out.println(a); a++; } System.out.println("Done");

Structure diagram of a while-statement (cont.) Structure diagram of this program: The while-statement will keep on executing as long as a ≤ 10 As long as a ≤ 10, the while- loop executes these statements: Print a; a++; (increment a by 1)