Programming Java
Control Statements
Incheon Paik
1
Java
Computer Industry lab.
Contents
Java
The if Statement and if-else Statement
Blocks of Code
The For Statement
The While Loop and Do-While Loop
The Break, Continue, and Switch Statements
Increment Operator & Decrement Operator
Back-slash codes
Relational and Boolean Logical Operators
Three Terms Operator
Bit Operators
The Assertion Statement
2
Computer Industry lab.
Control Statements
To change the execution order of program
As the method of controlling the execution order
Conditional Statement : if St., switch St.
Repeat Statement : for St., while St., do-while St.
Branch Statement : break St., continue St., return St.
3
Java
Computer Industry lab.
The If Statement and If – else Statement
Form of the if Statement
if ( <conditional expression> ) <statement>
if ( < conditional expression > ) <statement1>
else <statement2>
Result of conditional expression
:
Logical
Type(true or false)
Java
4
Computer Industry lab.
The If and If-Else Statement
Nested if statement
if (<cond. expr.>)
if (<cond. expr.>)
// . . .
<statement>
if (<cond. expr.1>)
<statement 1>
else if (<cond. expr.2>) < statement 2>
…
else if (<cond. expr. n>) < statement n>
else < statement>
A Block of
Code
if (expression) {
statement1;
statement2;
………….
}
5
Java
Computer Industry lab.
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>
<expr. 1> : initialize the control variable
<expr. 2> : check the control variable
<expr. 3> : modify the control variable
s = 0;
for (i=1; i<=N; ++i)
s += i;
Java
// sum from 1 to N : i increment
6
Computer Industry lab.
The For Statement
Execution order of the for statement
1
5
2
for ( < Expr1>;<
Expr3>)
Expr1>;<Expr2> ; <Expr3>
3
6
4
>
False
Java
7
Computer Industry lab.
The Collection-based For Loop
This expression specified the variable, season,
of type Season in this case, that will be assigned
each of the values in the collection in turn.
This expression identifies the collection that is
the source of data values to be iterated over.
In this case it is all the values in the enumeration
Season.
for ( Season season : Season.values() ) {
System.out.println (“The season is now “ + season);
}
// <statement>
A colon separates the two control expressions
in this type of for loop
Java
8
Computer Industry lab.
The Repeat Statement - while Statement
Form of the while statement
while ( cond. Expr. )
<Statement>
Order of Execution
i = 1; s = 0;
while (i <= N) { // summation from 1 to N
s += i; ++i;
}
(1)
while (< Cond.
Cond. Expr.
Expr.>)
(4)
False
(2) True
(3)
< Statement >
9
Java
Computer Industry lab.
The Repeat Statement - while Statement
Comparison of the for statement and the while statement
i = 0;
while (i < N) {
s += i;
++i;
}
for (i = 0; i < N; ++i)
s += i;
Java
10
Computer Industry lab.
The Repeat Statement – do - while Statement
After executing the repeating statements, then check the conditional
expression
Form of the do-while statement
Although
Althoughthe
theconditional
conditionalexpression
expression
do
<statement>
while (<conditional expression>);
isisfalse,
false,execute
executethe
thestatement
statement
one
time
above
one time aboveatatleast
least
11
Java
Computer Industry lab.
The Branch Statement - break Statement
To move control to the out of the block
From of the break statement
break [label] ;
int i = 1;
while (true) {
if (i == 3)
break;
System.out.println("This is a " + i + " iteration");
++i;
}
Java
12
Computer Industry lab.
The Branch Statement - break Statement
Label the break statement
Can be used instead of goto statement
Form of usage
labelName :
Rep. St. 1 {
Rep. St. 2 {
// . . .
break;
// . . .
break labelName;
labelName;
}
// . . .
}
13
Java
Computer Industry lab.
The Branch Statement – continue Statement
To move control to the start of next repeatation
From of the continue statement
continue [Label] ;
When used in the for statement
for (i=0; i<=5; ++i) {
if (i % 2 == 0)
continue;
System.out.println("This is a " + i + " iteration");
}
Java
14
Computer Industry lab.
The Branch Statement – continue Statement
When used in the while statement
i = 0;
while (i <= 5) {
++i;
if (i % 2) == 0)
continue;
System.out.println("This is a odd iteration - " + i);
}
15
Java
Computer Industry lab.
The Branch Statement – continue Statement
Label the continue statement
labelName:
labelName:
Rep. St. 1 {
Rep. St. 2 {
// ...
continue;
// ...
continue labelName;
labelName;
}
}
Java
16
Computer Industry lab.
The Branch Statement – return Statement
To terminate the execution of method, then pass the method of
caller that control
Forms of the return statement
return;
return
.>;
<expr.>;
a]
17
Java
Computer Industry lab.
Increment & Decrement Operator
Operator
++, - Prefix operator
nn==1;1;
xx==++n;
++n;
Postfix operator
nn==1;1;
xx==n++;
n++;
Java
////x=2,
x=2,n=2
n=2
////x=1,
x=1,n=2
n=2
Cannot be use with expressions, only with variables
Cannot be applied at the real type (a + b)++ // error
(a + b)++
18
// error
Computer Industry lab.
Back-Slash Codes
class SpecialCharacters {
¥b back space
public static void main(String args[]) {
¥n new line
System.out.print(“¥u00a0 ¥u00a1
¥u00a2 ¥u00a3”);
¥r carrage return
}
¥f form feed
¥t
}
tab
¥” double quotation
¥’ single quotation
¥0 null
¥¥ back slash
¥uxxxx Unicode (x: hexa decimal)
19
Java
Computer Industry lab.
Bitwise Operators
Operator
&, |, <<, >>, >>>, ^, ~
Operand should be integer type
Precedence
Operator
~
<< >> >>>
&
^
|
Java
Precedence
(H)
(L)
20
Computer Industry lab.
Bitwise Operators
Bitwise AND
10012 & 00112 = 00012
To extract the special area in variable by masking that area
Bit OR
10012 | 00112 = 10112
Exclusive OR
10012 ^ 00112 = 10102
1’s Complement
~ 000010102 = 111101012
21
Java
Computer Industry lab.
Bitwise Operators
Bitwise Shift Operator
Shift lefe(<<)
y
xx<<
<
Shift right(>>)
y
xx>>
>>yy==xx//22y
Unsigned shift right(>>>)
Java
Give this operator because Java does not support unsigned
integer.
22
Computer Industry lab.
Comparing enumeration values
Definition of enumeration type
enum
enumSeason
Season{spring,
{spring,summer,
summer,fall,
fall,winter}
winter}
Check the value
Season
Seasonseason
season==Season.summer;
Season.summer;
if(Season.equals(Season.spring)
if(Season.equals(Season.spring){{
System.out.println(“Spring
System.out.println(“Springhas
hassprung,
sprung,the
thegrass
grassisisriz.”);
riz.”);
}}else
{
else {
System.out.println(“It
System.out.println(“Itisn¥’
isn¥’Spring”);
Spring”);
}}
****Sample
SampleCode
Code::
/> />
23
Java
Computer Industry lab.
Ternary Operator
Operator
Expr1 ? Expr2 : Expr3 (3 Terms Operator)
if (x > y) max = x;
else max = y;
max
max==xx>>yy??xx::yy;;
mm==aa>>bb??(c(c>>aa??cc::a)a)::(c(c>>bb??cc::b)
b);;
Java
24
Computer Industry lab.
Assignment Operators
Expr1
Expr1 op=
op= Expr
Expr22
Expr
Expr11==Expr
Expr11 op
opExpr2
Expr2
Operator
Arithmetic operator : + - * / %
Bitwise operator : & | ^ << >> >>>
sum
sum==sum
sum++ii;;
sum
sum+=
+= ii;;
xx==xx*
*yy++1;1;
xx*=
*=yy++1;1;
x = x * (y+1)
25
Java
Computer Industry lab.
Cast Operators
Data Type Casting Operator
(Data
Type)
Expression
(Data
Type)
Expression
Cast
operator
:(, )
(int)
(int)3.75
3.75
(float)
(float)33
(float)
(float)(1
(1//2)
2)
(float)1/2
(float)1/2
Java
===>
===>
===>
===>
===>
===>
===>
===>
33
3.0
3.0
0.0
0.0
0.5
0.5
26
Computer Industry lab.
Operator Precedence
Operator
Association
() [] .
! ~ ++ -- + - (Data Type)
* / %
+ << >> >>>
< <= > >= instance
== !=
&
^
|
&&
||
?:
= += -= *= /= %= &= ^= |= <<= >>= >>>=
Left Assoc.
Right Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Left Assoc.
Right Assoc.
Right Assoc.
(Low)
27
Java
Precedence
(High)
Computer Industry lab.
Operator Precedence
Java
a=x+y-z;
b = -x ;
c = -x++ ;
d = -++x ;
e = -x + z ;
// Left Association
// Right Association
28
Computer Industry lab.
The Assertion Statements
assert logical_expression;
The assert is a keyword, and a logical_expression is any expre- ssion
that results in a value of true or false.
If logical_expression evaluates to true, then the program continues n
ormally. If logical_expression evaluates to false, the program will
be terminated with an error mess-age starting with:
java.lang.AssertionError
Sample Code “TryAssertions.java”
/>
29
Java
Computer Industry lab.
Exercise
Step 1 , 2
Step 3 Magic Square
Start
Java
You can try by yourself.
blank
8
blank
blocked
1
6
blank
3
5
7
blank
4
9
2
blocked
30
Principle
1.
Basically go right up
direction
2.
If blank, then go to
opposite block
3.
If blocked, go down
Computer Industry lab.
Exercise
Step 4
Java
Refer to Slides #8, #23.
31
Computer Industry lab.