Computer Science Ch. 4

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The break Statement in Loops

A break statement can be used to end a loop immediately. The break statement ends only the innermost loop or switch statement that contains the break statement. break statements make loops more difficult to understand. Use break statements sparingly (if ever).

The continue Statement in Loops

A continue statement Ends current loop iteration Begins the next one Text recommends avoiding use Introduce unneeded complications

The for Statement

A for statement executes the body of a loop a fixed number of times. Example for (count = 1; count < 3; count++) System.out.println(count);

Infinite Loops

A loop which repeats without ever ending is called an infinite loop. If the controlling boolean expression never becomes false, a while loop or a do-while loop will repeat without ending. A negative growth rate in the preceding problem causes totalBugVolume always to be less than houseVolume, so that the loop never ends.

Java Loop Statements

A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is called the body of the loop. A loop could be used to compute grades for each student in a class. There must be a means of exiting the loop.

The do-while Statement

Also called a do-while loop Similar to a while statement, except that the loop body is executed at least once Syntax do Body_Statement while (Boolean_Expression); Don't forget the semicolon!

The while Statement

Also called a while loop A while statement repeats while a controlling boolean expression remains true The loop body typically contains an action that ultimately causes the controlling boolean expression to become false.

Controlling Number of Loop Iterations

If the number of iterations is known before the loop starts, the loop is called a count-controlled loop. Use a for loop. Asking the user before each iteration if it is time to end the loop is called the ask-before-iterating technique. Appropriate for a small number of iterations Use a while loop or a do-while loop.

The for-each Statement

Possible to step through values of an enumeration type Example enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES} for (Suit nextSuit : Suit.values()) System.out.print(nextSuit + " "); System.out.println();


Kaugnay na mga set ng pag-aaral

Pediatric Allergy and Immunology

View Set

Sensation and Perception- TEST ONE

View Set

Biology IGCSE syllabus chapter 8

View Set