<< Chapter < Page Chapter >> Page >

Returning a value of type int in the for loop will satisfy the type requirement because type int will be automatically converted to type double as it is returned. (Conversion from type int to type double is a widening conversion.)

Back to Question 14

Answer 13

A. Compiler Error

Explanation 13

The scope of a local variable

In general, the scope of a local variable extends from the point at which it is declared to the curly brace that signals the end of the block in which it isdeclared.

This applies to for loop in an interesting way

While it is allowable to declare a variable within the first clause of a for loop, the scope of that variable is limited to the block of code contained in the loop structure.

The variable cannot be accessed outside the loop.

Attempts to access variable out of scope

This program attempts to access the value of the variable named cnt after the loop terminates.

The program displays the following compiler error under JDK 1.3. This error results from the attempt to display the value of the counter after theloop terminates.

Ap037.java:15: cannot resolve symbol symbol : variable cntlocation: class Worker System.out.println(cnt + " ");

Back to Question 13

Answer 12

C. 0 1 2 3 4 5

Explanation 12

A simple for loop structure

This program illustrates a simple for loop that displays the value of its counter using a call to the print method inside the loop.

After the loop terminates, the program displays the value of the counter one last time using a call to println .

Three clauses separated by semicolons

The first line of a for loop structure always contains three clauses separated by semicolons.

The first and third clauses may be empty, but the semicolons are required in any case.

The first clause ...

The first clause is executed once and only once at the beginning of the loop.

It can contain just about any valid Java expression.

It can even contain more than one expression with the individual expression separated by commas.

When the first clause contains more than one expression separated by commas, the expressions are evaluated in left-to-right order.

The second clause

The second clause is a conditional clause. It must contain an expression that returns a boolean value.

(Actually, this clause can also be empty, in which case it is apparently assumed to be true. This leads to an infinite loop unless there is somecode inside the loop to terminate it, perhaps by executing a return or a break statement.)

An entry-condition loop

The for loop is an entry condition loop, meaning that the conditional expression is evaluated once immediately after the first clause isexecuted, and once per iteration thereafter.

Behavior of the for loop

If the conditional expression returns true, the block of code following the closing parenthesis is executed.

If it returns false, the block of code is skipped, and control passes to the first executable statement following the block of code.

(For the case where the block contains only one statement, the matching curly brackets can be omitted.)

The third clause

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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