<< Chapter < Page Chapter >> Page >

The third clause can contain none, one, or more valid expressions separated by commas.

If there are more than one, they are evaluated in left-to-right order.

When they are evaluated

The expressions in the third clause are evaluated once during each iteration.

However, it is very important to remember that despite the physical placement of the clause in the first line, the expressions in the third clause are notevaluated until after the code in the block has been evaluated.

Typically an update clause

The third clause is typically used to update a counter, but this is not a technical requirement.

This clause can be used for just about any purpose.

However, the counter must be updated somewhere within the block of code or the loop will never terminate.

(Stated differently, something must occur within the block of code to eventually cause the conditional expression to evaluate to false.Otherwise, the loop will never terminate on its own. However, it is possible to execute a return or break within the block to terminate the loop.)

Note the first output value for this program

Because the update in the third clause is not executed until after the code in the block has been executed, the first value displayed by this program is the value zero.

Back to Question 12

Answer 11

E. None of the above

Explanation 11

And the answer is ...

The output produced by this program is:

1 3 5

A simple while loop

This program uses a simple while loop to display the value of a counter, once during each iteration.

Behavior of a while loop

As long as the relational expression in the conditional clause returns true, the block of code immediately following the conditional clause is executed.

When the relational expression returns false, the block of code following the conditional clause is skipped and control passes to the next executablestatement following that block of code.

An entry-condition loop

The while loop is an entry condition loop, meaning that the test is performed once during each iteration before the block of code is executed.

If the first test returns false, the block of code is skipped entirely.

An exit-condition loop

There is another loop, known as a do-while loop, that performs the test after the block of code has been executed once. This guarantees that theblock of code will always be executed at least once.

Just to make things interesting ...

Two statements using the increment operator were placed inside the loop in this program.

Therefore, insofar as the conditional test is concerned, the counter is being incremented by twos. This causes the output to display the sequence 1 3 5.

Nested while loops

The while loop control structure can contain loops nested inside of loops, which leads to some interesting behavior.

Back to Question 11

Answer 10

F. x != 4,5,6

Explanation 10

A multiple-choice structure

This is a form of control structure that is often used to make logical decisions in a multiple-choice sense.

This is a completely general control structure. It can be used with just about any type of data.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask