Chapter 5: Control Structures II

Slides:



Advertisements
Similar presentations
Chapter 5: Control Structures II (Repetition)
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 5: Control Structures II (Repetition)
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 4: Control Structures II
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Structures
Java Fundamentals 4.
Topic 4: Looping Statements
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Control Structures II (Repetition)
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Repetition-Sentinel,Flag Loop/Do_While
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Control Structures II
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Control Structures - Repetition
MSIS 655 Advanced Business Applications Programming
A First Book of ANSI C Fourth Edition
Chapter 2.1 Repetition.
Control Statements Loops.
Chapter 2.2 Control Structures (Iteration)
2.6 The if/else Selection Structure
Chapter 5: Control Structures II (Repetition)
Control Statements Loops.
Control Statements:.
Presentation transcript:

Chapter 5: Control Structures II Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Objectives Learn about repetition (looping) control structures. Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structures. Examine break and continue statements. Discover how to form and use nested control structures. Java Programming: From Problem Analysis to Program Design, Second Edition

Why Is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas used to find average grades for students in a class. Java Programming: From Problem Analysis to Program Design, Second Edition

The while Looping (Repetition) Structure Syntax: while (expression) statement Expression is always true in an infinite loop. Statements must change value of expression to false. Java Programming: From Problem Analysis to Program Design, Second Edition

The while Looping (Repetition) Structure Example 5-1 i = 0; //Line 1 while (i <= 20) //Line 2 { System.out.print(i + " "); //Line 3 i = i + 5; //Line 4 } System.out.println(); //Line 5 Output 0 5 10 15 20 Java Programming: From Problem Analysis to Program Design, Second Edition

The while Looping (Repetition) Structure Typically, while loops are written in the following form: //initialize the loop control variable(s) while (expression) //expression tests the LCV { . //update the loop control variable(s) } Java Programming: From Problem Analysis to Program Design, Second Edition

Counter-Controlled while Loop Used when exact number of data or entry pieces is known. General form: int N = //value input by user or specified //in program int counter = 0; while (counter < N) { . counter++; } Java Programming: From Problem Analysis to Program Design, Second Edition

Sentinel-Controlled while Loop Used when exact number of entry pieces is unknown, but last entry (special/sentinel value) is known. General form: Input the first data item into variable; while (variable != sentinel) { . input a data item into variable; } Java Programming: From Problem Analysis to Program Design, Second Edition

Flag-Controlled while Loop Boolean value used to control loop. General form: boolean found = false; while (!found) { . if (expression) found = true; } Java Programming: From Problem Analysis to Program Design, Second Edition

EOF(End of File)-Controlled while Loop Used when input is from files. Sentinel value is not always appropriate. In an EOF-controlled while loop that uses the Scanner object console to input data, console acts at the loop control variable. The method hasNext, of the class Scanner, returns true if there is an input in the input stream; otherwise, it returns false. The expression console.hasNext() acts as the loop condition. Expressions such as console.nextInt() update the value of the loop condition. Java Programming: From Problem Analysis to Program Design, Second Edition

EOF-Controlled while Loop A general form of the EOF-controlled while loop that uses the Scanner object console to input data is: while (console.hasNext()) { //Get the next input and store in an //appropriate variable //Process data } Java Programming: From Problem Analysis to Program Design, Second Edition

EOF-Controlled while Loop Suppose that inFile is a Scanner object initialized to the input file. In this case, the EOF-controlled while loop takes the following form: while (inFile.hasNext()) { //Get the next input and store in an //appropriate variable //Process data } Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Checking Account Balance Input file: Customer’s account number, account balance at beginning of month, transaction type (withdrawal, deposit, interest), transaction amount. Output: Account number, beginning balance, ending balance, total interest paid, total amount deposited, number of deposits, total amount withdrawn, number of withdrawals. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Checking Account Balance Solution: Read data. EOF-controlled loop. switch structure of transaction types. Determine action (add to balance or subtract from balance depending on transaction type). Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Fibonacci Number Fibonacci formula for any Fibonacci sequence: an = an-1 + an-2 Input: First two Fibonacci numbers in sequence, position in sequence of desired Fibonacci number (n). int previous1 = Fibonacci number 1 int previous2 = Fibonacci number 2 int nthFibonacci = Position of nth Fibonacci number Output: nth Fibonacci number. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Fibonacci Number (Solution) if (nthFibonacci == 1) current = previous1; else if (nthFibonacci == 2) current = previous2; else { counter = 3; while (counter <= nthFibonacci) current = previous2 + previous1; previous1 = previous2; previous2 = current; counter++; } Final result found in last value of current. Java Programming: From Problem Analysis to Program Design, Second Edition

The for Looping (Repetition) Structure Specialized form of while loop. Simplifies the writing of count-controlled loops. Syntax: for (initial statement; loop condition; update statement) statement Java Programming: From Problem Analysis to Program Design, Second Edition

The for Looping (Repetition) Structure Execution: Initial statement executes. Loop condition is evaluated. If loop condition evaluates to true, execute for loop statement and execute update statement. Repeat until loop condition is false. Java Programming: From Problem Analysis to Program Design, Second Edition

The for Looping (Repetition) Structure Example 5-8 The following for loop prints the first 10 nonnegative integers: for (i = 0; i < 10; i++) System.out.print(i + " "); System.out.println(); Java Programming: From Problem Analysis to Program Design, Second Edition

The for Looping (Repetition) Structure Example 5-9 The following for loop outputs the word Hello and a star (on separate lines) five times: for (i = 1; i <= 5; i++) { System.out.println("Hello"); System.out.println("*"); } 2. The following for loop outputs the word Hello five times and the star only once: Java Programming: From Problem Analysis to Program Design, Second Edition

The for Looping (Repetition) Structure Does not execute if initial condition is false. Update expression changes value of loop control variable, eventually making it false. If loop condition is always true, result is an infinite loop. Infinite loop can be specified by omitting all three control statements. If loop condition is omitted, it is assumed to be true. for statement ending in semicolon is empty. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Classify Numbers Input: N integers (positive, negative, and zeros). int N = 20; //N easily modified Output: Number of 0s, number of even integers, number of odd integers. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Classify Numbers (Solution) for (counter = 1; counter <= N; counter++) { number = console.nextInt(); System.out.print(number + " "); switch (number % 2) case 0: evens++; if (number == 0) zeros++; break; case 1: case -1: odds++; } //end switch } //end for loop Java Programming: From Problem Analysis to Program Design, Second Edition

The do…while Loop (Repetition) Structure Syntax: do statement while (expression); Statements are executed first and then expression is evaluated. Statements are executed at least once and then continued if expression is true. Java Programming: From Problem Analysis to Program Design, Second Edition

do…while Loop (Post-Test Loop) Java Programming: From Problem Analysis to Program Design, Second Edition

break Statements Used to exit early from a loop. Used to skip remainder of switch structure. Can be placed within if statement of a loop. If condition is met, loop is exited immediately. Java Programming: From Problem Analysis to Program Design, Second Edition

continue Statements Used in while, for, and do...while structures. When executed in a loop, the remaining statements in the loop are skipped; proceeds with the next iteration of the loop. When executed in a while/do…while structure, expression is evaluated immediately after continue statement. In a for structure, the update statement is executed after the continue statement; the loop condition then executes. Java Programming: From Problem Analysis to Program Design, Second Edition

Nested Control Structures Provides new power, subtlety, and complexity. if, if…else, and switch structures can be placed within while loops. for loops can be found within other for loops. Java Programming: From Problem Analysis to Program Design, Second Edition

Nested Control Structures (Example) for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) System.out.print(" *"); System.out.println(); } Output: * ** *** **** ***** Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Summary Looping mechanisms: Nested control structures Counter-controlled while loop Sentinel-controlled while loop Flag-controlled while loop EOF-controlled while loop for loop do…while loop break statements continue statements Nested control structures Java Programming: From Problem Analysis to Program Design, Second Edition