Java Reference
In-Depth Information
SR 6.6
char
id = (first) ? 'A' : 'B';
SR 6.7
System.out.println ("The value is " + ((val <= 10) ? "not" :
""
) +
"
greater than 10.
"
);
6.3
The
do
Statement
SR 6.8
A
while
loop evaluates the condition first. If it is true, it executes the
loop body. The
do
loop executes the body first and then evaluates the
condition. Therefore, the body of a
while
loop is executed zero or more
times, and the body of a
do
loop is executed one or more times.
SR 6.9
The output is the integers 0 through 9, printed one integer per line.
SR 6.10
The code contains an infinite loop. The numbers 10, 11, 12, and so on
will be printed until the program is terminated or until the number gets
too large to be held by the
int
variable
low
.
SR 6.11
Scanner scan =
new
Scanner (System.in);
int
num, sum = 0;
do
{
System.out.print (
"
enter next number (0 to quit) >
"
);
num = scan.nextInt ();
sum += num;
}
while
(num != 0);
System.out.println (sum);
6.4
The
for
Statement
SR 6.12
A
for
loop is usually used when we know, or can calculate, how many
times we want to iterate through the loop body. A
while
loop handles
a more generic situation.
SR 6.13
The output is:
100
SR 6.14
The output is:
60
SR 6.15
The output is:
*
***
*****
*******
*********
***********
Search WWH ::
Custom Search