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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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