Chapter 4 Loops

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

1.Decide what work must be done inside the loop, 2.specify the loop conditions, 3.determine the loop type, 4.set up variables for entering the loop for the first time, 5.process the loop after the loop has finished, 6. trace the loop with typical examples, then...

7. implement the loop in java

There are three types of loops: while Loops for Loops do Loops Each loop requires the following steps:

Initialization Condition Update

for loop steps

Initialize counter, Check condition, execute loop body, update counter, check condition again

boolean found = false; char ch = '?'; int position = 0; while (!found && position < str.length()) { ch = str.charAt(position); if (ch == ' ') { found = true; } else { position++; } } What does this find?

a string that matches a condition

Use a for loop when

a value runs from a starting point to an ending point. or Increment (or decrement) by a constant value.

when a variable starts at 1 when it should have started at 0. (A common error when programming loops. )

an off by one error

Nested Loops Similar to nested if statements, loops can be nested as well. That is, the body of a loop can contain

another loop.

An off-by-one error is a

common error when programming loops.

for (initialization; _________; update)

condition

in a for statement The statement is executed until the

condition becomes false

the do loop goes to the statement and then to the

condition evaluated

The for loop is a

counting loop

in a for statement The initialization section can be used to declare a variable. The condition of a for loop is tested prior to executing the loop body similar to while loop. The body of a for loop will execute zero or more times. The for loop is a

counting loop.

in a for statement The initialization section can be used to

declare a variable.

do { statement; } while ( condition ); is an example of a

do statement

in a for statement The increment portion is executed at the end of

each iteration

In a while loop If the condition is true, the statement is executed. Then the condition is

evaluated again, and if it is still true, the statement is executed again.

in a do Statement The statement is executed once initially, and then the condition is

evaluated.

If the condition is true, the statement is

executed.

The body of a while loop eventually must make the condition

false.

for (int count=1; count <= 5; count++) System.out.println (count); is an example of what kind of statement?

for loop

We distinguish between two major loop types. A count-controlled loop is executed a definite number of times. In an event-controlled loop, the number of iterations is not known in advance—the loop is executed until some event happens. Count control loops can be executes as

for statements

for (__________; condition; update)

initialization

for Statement A for statement has the following syntax: for (what goes here? ) statement;

initialization ; condition ; increment

Nested Loops For each iteration of the outer loop, the inner loop

iterates completely.

The body of a while loop eventually must make the condition false. If not, it is called an infinite loop, which will execute until the user interrupts the program. This is a common

logical error.

The do loop is appropriate when the

loop body must be executed at least once.

The do loop is appropriate when the loop body

must be executed at least once.

Nested Loops Similar to nested if statements, loops can be

nested as well.

If the condition of a while loop is false initially, the statement is

never executed.

Loops

repeatedly execute a part of the program until a goal has been reached.

The while loop is a general loop and can be used with

sentinel or as a counting loop.

the while loop starts with the condition evaluated first and then to the

statement

The values for i are bounded by the relation 1≤i≤10. Because there are ≤ on both bounds, the bounds are called

symmetric.

A while statement is an example of a loop. If you draw a flowchart, the flow of execution loops again to the point where the condition is

tested

During a while statement: If the condition is true, the statement is executed. Then the condition is evaluated again, and if it is still true, the statement is executed again. The statement is executed repeatedly until

the condition becomes false.

in a do Statement The statement is executed once initially, and then the condition is evaluated. The statement is executed repeatedly until

the condition becomes false.

in a for statement The initialization is executed once before

the loop begins

The body of a while loop eventually must make the condition false. If not, it is called an infinite loop, which will execute until

the user interrupts the program.

Do you need to complete one iteration of the loop body before you can tell when to terminate the loop?

then use a do loop

a loop executes instructions repeatedly while a condition is .

true

for (initialization; condition; ________)

update

in a for statement The initialization section can be used to declare a ___________. The condition of a for loop is tested prior to ___________ the loop body similar to while loop.

variable, executing

A while statement has the following syntax:

while ( condition ) { statement; }

A count-controlled loop is executed a definite number of times. In an event-controlled loop, the number of iterations is not known in advance—the loop is executed until some event happens.. Do you need to complete one iteration of the loop body before you can tell when to terminate the loop? no? then what?

while loop

Java has three kinds of loops (repetition statements):

while loop do loop for loop

Nested Loops All loop types can be nested, mostly we make use of nested for or nested

while loops.

As long as the condition remains true, the statements inside the while statement are executed. These statements are called the body of the

while statement

int count = 1; while (count <= 5) { System.out.println (count); count++; } is an example of what kind of statement?

while statement

If the condition of a while loop is false initially, the statement is never executed. Therefore, the body of a while loop will execute

zero or more times.

in a for statement The initialization section can be used to declare a variable. The condition of a for loop is tested prior to executing the loop body similar to while loop. The body of a for loop will execute

zero or more times.


Set pelajaran terkait

MICROM Lecture 11 chapter 13 (Viruses, Viroids, Prions)

View Set

What You / Others Like To Do and Other Useful Words

View Set

Geology 150 Test 4 Final (Glaciers, Wind, Desserts 21, 22, 23, anything about southern california geology & pictures and diagrams from last chapters, lithospheric keel, deepest part of the L.A. Basin, San Gabriel Anorthocite, L.A. Rivers, San Gabriel Mts.

View Set

Review of Chapter 1 Information: Negative & Positive Feedback

View Set

Cognitive Psychology- Practice Questions Ch. 10

View Set

Bio 1406 Lab 1 Collecting and Analyzing Data

View Set

Chapter 9 Toxicology Test Review Notes

View Set

FA Organ Systems Respiration- Pathology

View Set

ACCT 3303 Chapter 4, UTA Sargent

View Set