Java Reference
In-Depth Information
a = i--;
⇒
Make
a
equal to
i
and then decrement
i
by one.
a = ++i;
⇒
Increment
i
by one and then make
a
equal to the new value of
i
.
a = --i;
⇒
Decrement
i
by one and then make
a
equal to the new value of
i
.
⇒
Equivalent to
a=a+b
.
a+=b;
⇒
Equivalent to
a=a/b
.
a-=b;
⇒
Equivalent to
a=a*b
.
a*=b;
a/=b;
⇒
Equivalent to
a=a-b
.
a%=b;
⇒
Equivalent to
a=a%b
.
{
...
}⇒
for(int i=0; i < n; i++)
Execute the body of the
for
loop
n
times.
Execute the body of the
for
loop
n
times. The variable
j
starts at 0 and is incremented by 5 at every iteration of the
loop.
{
...
}⇒
for(int i=0,j=0; i < n; i++,j+=5)
while(
condition
)
{
...
}⇒
Keep executing the body of the
while
loop until the
condition
becomes false.
do
{
Keep executing the body of the loop until the
condition
becomes false. The body of the
do-while
loop is always executed at least
once.
...
}
while(
condition
);
⇒
some loop
{
The
break
statement forces Java to stop executing
the loop and transfer control to the first line after the loop.
...
break;
...
}⇒
The
continue
statement forces Java to stop
executing the current iteration of the loop and proceed to the next iteration. If the
loop is a
for
loop, then the
continue
statement jumps to the third part of the
for
loop. If the loop is a
while
or
do-while
loop, then control jumps to the condition of
the loop.
{
...
continue;
...
}⇒
some loop
a = 103%20
The value of the variable
a
will become equal to 3 because this is the
remainder when one divides the number 103 by 20.
⇒
⇒
s.length()
Returns the length of the
String s
.
s.charAt(3)
⇒
Returns the character at position 4 of the string
s
. Note that counting
starts at 0.
3.8 Important Points
1. All three loops are similar to an
if
statement. If the condition is true, then the body
is executed. The difference is that the body of an
if
statement is executed at most
once. The body of a
while
statement is executed 0 or more times. The body of a
do-while
statement is executed 1 or more times.