Professional Documents
Culture Documents
Chap03 04
Chap03 04
Control Statements
Incheon Paik
else <statement2>
if (expression) {
A Block of statement1;
statement2;
Code ………….
}
5 Computer Industry lab.
Java
The For Statement
Repeat the sequence of statements as many as defined.
Form of the for statement
for ( <expr. 1> ; < expr. 2> ; < expr. 3>)
<statement>
s = 0;
for (i=1; i<=N; ++i) // sum from 1 to N : i increment
s += i;
1 2 5
3 True 4
6 < Statement
>
False
(1)
(2) True
(4) (3)
< Statement >
False
9 Computer Industry lab.
Java
The Repeat Statement - while Statement
i = 0;
while (i < N) {
for (i = 0; i < N; ++i) s += i;
s += i; ++i;
}
int i = 1;
while (true) {
if (i == 3)
break;
System.out.println("This is a " + i + " iteration");
++i;
}
Form of usage
labelName :
Rep. St. 1 {
Rep. St. 2 {
// . . .
break;
// . . .
break labelName;
}
// . . .
}
i = 0;
while (i <= 5) {
++i;
if (i % 2) == 0)
continue;
System.out.println("This is a odd iteration - " + i);
}
labelName:
Rep. St. 1 {
Rep. St. 2 {
// ...
continue;
// ...
continue labelName;
[LabeledContinue.java]
}
}
a]
Operator
++, --
Prefix operator
n = 1;
x = ++n; // x=2, n=2
Postfix operator
n = 1;
x = n++; // x=1, n=2
Precedence
Operator Precedence
~ (H)
<< >> >>>
&
^
| (L)
Bitwise AND
1001 & 0011 = 0001
2 2 2
To extract the special area in variable by masking that area
Bit OR
1001 | 0011 = 1011
2 2 2
Exclusive OR
1001 ^ 0011 = 1010
2 2 2
1’s Complement
~ 00001010 = 11110101
2 2
Shift lefe(<<)
x << y = x * 2y
Shift right(>>)
x >> y = x / 2y
eger.
** Sample Code :
http://ebiz.u-aizu.ac.jp/~paikic/lecture/2005-1/code-examples/Java2-1.5/Code/Ch03/CollectionBasedForLoop.java
Operator
Arithmetic operator : + - * / %
x = x *y + 1; x *= y + 1;
x = x * (y+1)
a=x+y-z; // Left Association
b = -x ; // Right Association
c = -x++ ;
d = -++x ;
e = -x + z ;