Java Programming: Repetition Control Structures and Looping in Java - Prof. Donald Adjeroh, Study notes of Computer Science

An in-depth exploration of repetition control structures in java, including count-controlled, sentinel-controlled, flag-controlled, and eof-controlled repetition structures. The syntax and usage of while loops, as well as break and continue statements and nested control structures. Examples are given to illustrate each concept.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-yt3
koofers-user-yt3 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Java Programming: From Problem Analysis to Program Design, Second Edition 1
Chapter 5:
Repetition Control Structures
Repetition (looping) control structures.
Count-controlled, sentinel-controlled, flag-
controlled, and EOF-controlled repetition
structures.
Break and continue statements.
nested control structures.
Java Programming: From Problem Analysis to Program Design, Second Edition 2
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java Programming: Repetition Control Structures and Looping in Java - Prof. Donald Adjeroh and more Study notes Computer Science in PDF only on Docsity!

Java Programming: From Problem Analysis to Program Design, Second Edition 1

Chapter 5:

Repetition Control Structures

Œ Repetition (looping) control structures.

Œ Count-controlled, sentinel-controlled, flag-

controlled, and EOF-controlled repetition

structures.

Œ Break and continue statements.

Œ nested control structures.

Java Programming: From Problem Analysis to Program Design, Second Edition 2

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 3

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 4

The while Looping (Repetition) Structure

Example 5- 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 7

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 8

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 9

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 10

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 13

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 14

Programming Example:

Fibonacci Number

Œ Fibonacci formula for any Fibonacci sequence:

a (^) n = a (^) n -1 + a (^) n -

Œ 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 15

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 16

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 19

The for Looping (Repetition) Structure

Example 5-

  1. 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("*"); }

  1. The following for loop outputs the word Hello five times and the star only once:

for (i = 1; i <= 5; i++) System.out.println("Hello"); System.out.println("*");

Java Programming: From Problem Analysis to Program Design, Second Edition 20

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 21

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 22

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 25

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 26

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 27

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 28

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:

**