June 10, 2015ICS102: while & do-while1 while and do-while Statements.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 5: Control Structures II (Repetition)
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Java Programming: From the Ground Up
Loops and Iteration for Statements, while Statements and do-while Statements.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
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.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Chapter 4 Repetition Statements (loops)
Chapter 5: Control Structures II
Loops.
Chapter 5: Control Structures II
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structures II
Repetition-Counter control Loop
Chapter 5: Control Structures II
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Loops October 10, 2017.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
Introduction to Object-Oriented Programming with Java--Wu
Control Statements Loops.
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
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.
Looping and Repetition
Presentation transcript:

June 10, 2015ICS102: while & do-while1 while and do-while Statements

June 10, 2015ICS102: while & do-while 2 Outline Introduction while Loop do-while Loop

June 10, 2015ICS102: while & do-while 3 - Introduction Loops in Java are similar to those in other high-level languages Java has three types of loop statements: The while The do-while The for The code that is repeated in a loop is called the body of the loop Each repetition of the loop body is called an iteration of the loop

June 10, 2015ICS102: while & do-while 4 - while loop A while statement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean expression is checked before the loop body is executed When false, the loop body is not executed at all Before the execution of each following iteration of the loop body, the Boolean expression is checked again If true, the loop body is executed again If false, the loop statement ends The loop body can consist of a single statement, or multiple statements enclosed in a pair of braces ( { } )

June 10, 2015ICS102: while & do-while 5 -- while Loop Syntax while ( ) //only one statement OR while ( ) { //many } while ( number <= 100 ) { sum = sum + number; number = number + 1; } Boolean Expression Statement (loop body) These statements are executed as long as number is less than or equal to 100.

June 10, 2015ICS102: while & do-while 6 -- while Loop Control flow int sum = 0, number = 1 number <= 100 ? false sum = sum + number; number = number + 1; sum = sum + number; number = number + 1; true

June 10, 2015ICS102: while & do-while 7 -do-while Loop A do-while statement is used to execute a portion of code (i.e., the loop body), and then repeat it based on the evaluation of a Boolean expression The loop body is executed at least once The Boolean expression is checked after the loop body is executed The Boolean expression is checked after each iteration of the loop body If true, the loop body is executed again If false, the loop statement ends Don't forget to put a semicolon after the Boolean expression Like the while statement, the loop body can consist of a single statement, or multiple statements enclosed in a pair of braces ( { } )

June 10, 2015ICS102: while & do-while 8 -- do-while Loop Syntax do { sum += number; number++; } while (sum <= ) ; do { } while ( ) ; Boolean Expression Statement (loop body) These statements are executed as long as sum is less than or equal to 1,000,000.

June 10, 2015ICS102: while & do-while 9 -- do-while Loop Control Flow int sum = 0, number = 1 sum += number; number++; sum += number; number++; sum <= ? true false

June 10, 2015ICS102: while & do-while 10 THE END

June 10, 2015ICS102: while & do-while 11 Examples

June 10, 2015ICS102: while & do-while 12 Questions 1. Write a Java program which computes the sum of all the odd numbers between 0 and Write a Java program which reads 20 numbers using a scanner and computes their average. 3. Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the number of even numbers. Assume the input integers are all positive. Use a negative number as a sentinel.

June 10, 2015ICS102: while & do-while 13 Solution using while loop

June 10, 2015ICS102: while & do-while 14 Q1 Solution int n =1; int sum = 0; while (n < 100) { sum += n; n = n + 2; } System.out.println(“The sum is “ + sum); Write a Java program which computes the sum of all the odd numbers between 0 and 100.

June 10, 2015ICS102: while & do-while 15 Q2 Solution Scanner kb = new Scanner(System.in); int cnt = 0; double x; double sum = 0; While (cnt < 20) { x = kb.nextDouble(); sum += x; cnt++; } System.out.println(“The Average is “ + sum/cnt); Write a Java program which reads 20 numbers using a scanner and computes their average.

June 10, 2015ICS102: while & do-while 16 Q3 Solution Scanner kb = new Scanner(System.in); int even_cnt = 0; int odd_cnt = 0; double x = kb.nextInt(); while (x > 0) { if ( mod(x,2) == 0) even_cnt++; else odd_cnt++; x = kb.nextInt(); } System.out.println(“Even numbers are = “ + even_count); System.out.println(“Odd numbers are = “ + odd_count); Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the count of even numbers. Assume the input integers are all positive. Use any negative number as a sentinel.

June 10, 2015ICS102: while & do-while 17 Solution using do-while loop

June 10, 2015ICS102: while & do-while 18 Q1 Solution int n = 1; int sum = 0; do { sum += n; n = n + 2; } While ( n < 100) System.out.println(“The sum is “ + sum); Write a Java program which computes the sum of all the odd numbers between 0 and 100.

June 10, 2015ICS102: while & do-while 19 Q2 Solution Scanner kb = new Scanner(System.in); int cnt = 0; double x; double sum = 0; do { System.out.println(“Enter a number”); x = kb.nextDouble(); sum += x; cnt++; } while (cnt < 20); System.out.println(“The Average is “ + sum/cnt); Write a Java program which reads 20 numbers using a scanner and computes their average.

June 10, 2015ICS102: while & do-while 20 Q3 Solution Scanner kb = new Scanner(System.in); int even_cnt = 0; int odd_cnt = 0; double x = kb.nextInt(); if (x > 0) { do { if ( mod(x,2) == 0) even_cnt++; else odd_cnt++; x = kb.nextInt(); } while ( x > 0) } System.out.println(“Even numbers are = “ + even_count); System.out.println(“Odd numbers are = “ + odd_count); Write a Java program which reads unknown number of integers using a scanner and counts the number of odd numbers and the count of even numbers. Assume the input integers are all positive. Use any negative number as a sentinel.

June 10, 2015ICS102: while & do-while 21 Additional Slides

June 10, 2015 ICS102: while & do-while Chapter while Loop Pitfall - 1 Infinite Loops Both loops will not terminate because the boolean expressions will never become false. Infinite Loops Both loops will not terminate because the boolean expressions will never become false. int count = 1; while ( count != 10 ) { count = count + 2; } 2 2 int product = 0; while ( product < ) { product = product * 5; } 1 1

June 10, 2015 ICS102: while & do-while Chapter while Loop Pitfall - 2 Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory. Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory. double count = 0.0; while ( count <= 1.0 ) { count = count + 1.0/3.0; } 2 2 double count = 0.0; while ( count != 1.0 ) { count = count + 1.0/3.0; } 1 1

June 10, 2015 ICS102: while & do-while Chapter while Loop Pitfall - 3 Goal: Execute the loop body 10 times. count = 1; while (count < 10) {... count++; } 1 1 count = 0; while (count <= 10) {... count++; } 3 3 count = 1; while (count <= 10) {... count++; } 2 2 count = 0; while (count < 10) {... count++; } andexhibit off-by-one error.

June 10, 2015 ICS102: while & do-while Chapter Checklist for Repetition Control 1. Watch out for the off-by-one error (OBOE). 2. Make sure the loop body contains a statement that will eventually cause the loop to terminate. 3. Make sure the loop repeats exactly the correct number of times.