Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

Programming Java

Control Statements

Incheon Paik

1 Computer Industry lab.


Java
Contents

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


Java
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 Computer Industry lab.


Java
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(tru


e or false)

4 Computer Industry lab.


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

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>

 <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) // sum from 1 to N : i increment
s += i;

6 Computer Industry lab.


Java
The For Statement
 Execution order of the for statement

1 2 5

for ( < Expr1>;<Expr2> ; <Expr3>)

3 True 4
6 < Statement
>
False

7 Computer Industry lab.


Java
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

8 Computer Industry lab.


Java
The Repeat Statement - while Statement

 Form of the while statement


while ( cond. Expr. )
i = 1; s = 0;
<Statement>
while (i <= N) { // summation from 1 to N
s += i; ++i;
 Order of Execution }

(1)

while (< Cond. Expr.>)

(2) True
(4) (3)
< Statement >
False
9 Computer Industry lab.
Java
The Repeat Statement - while Statement

 Comparison of the for statement and the while statement

i = 0;
while (i < N) {
for (i = 0; i < N; ++i) s += i;
s += i; ++i;
}

10 Computer Industry lab.


Java
The Repeat Statement – do - while Statement

 After executing the repeating statements, then check the conditional


expression
 Form of the do-while statement

do Although the conditional expression


is false, execute the statement
<statement> one time above at least
while (<conditional expression>);

11 Computer Industry lab.


Java
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;
}

12 Computer Industry lab.


Java
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;
}
// . . .
}

13 Computer Industry lab.


Java
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");
}

14 Computer Industry lab.


Java
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 Computer Industry lab.


Java
The Branch Statement – continue Statement

 Label the continue statement

labelName:
Rep. St. 1 {
Rep. St. 2 {
// ...
continue;
// ...
continue labelName;
 [LabeledContinue.java]
}
}

16 Computer Industry lab.


Java
The Branch Statement – return Statement

 To terminate the execution of method, then pass the method of calle


r that control
 Forms of the return statement
 return;
 return <expr.>;

a]

17 Computer Industry lab.


Java
Increment & Decrement Operator

 Operator
 ++, --

 Prefix operator

n = 1;
x = ++n; // x=2, n=2

 Postfix operator
n = 1;
x = n++; // x=1, n=2

 Cannot be use with expressions, only with variables


 Cannot be applied at the real type (a + b)++ // error

18 Computer Industry lab.


Java
Back-Slash Codes

\b back space class SpecialCharacters {


public static void main(String args[]) {
\n new line
System.out.print(“\u00a0 \u00a1 \u00a2
\r carrage return \u00a3”);
}
\f form feed
}
\t tab
\” double quotation
\’ single quotation
\0 null
\\ back slash
\uxxxx Unicode (x: hexa decimal)

19 Computer Industry lab.


Java
Bitwise Operators
 Operator
 &, |, <<, >>, >>>, ^, ~
 Operand should be integer type

 Precedence

Operator Precedence
~ (H)
<< >> >>>
&
^
| (L)

20 Computer Industry lab.


Java
Bitwise Operators

 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

21 Computer Industry lab.


Java
Bitwise Operators

 Bitwise Shift Operator

 Shift lefe(<<)
x << y = x * 2y

 Shift right(>>)
x >> y = x / 2y

 Unsigned shift right(>>>)


 Give this operator because Java does not support unsigned int

eger.

22 Computer Industry lab.


Java
Comparing enumeration values
Definition of enumeration type
enum Season {spring, summer, fall, winter}

Check the value


Season season = Season.summer;
if(Season.equals(Season.spring) {
System.out.println(“Spring has sprung, the grass is riz.”);
} else {
System.out.println(“It isn\’ Spring”);
}

** Sample Code :
http://ebiz.u-aizu.ac.jp/~paikic/lecture/2005-1/code-examples/Java2-1.5/Code/Ch03/CollectionBasedForLoop.java

23 Computer Industry lab.


Java
Ternary Operator
 Operator
 Expr1 ? Expr2 : Expr3 (3 Terms Operator)
if (x > y) max = x;
else max = y;
max = x > y ? x : y ;

m = a > b ? (c > a ? c : a) : (c > b ? c : b) ;

24 Computer Industry lab.


Java
Assignment Operators

Expr 1 = Expr 1 op Expr2 Expr1 op= Expr 2

 Operator
 Arithmetic operator : + - * / %

 Bitwise operator : & | ^ << >> >>>

sum = sum + i ; sum += i ;

x = x *y + 1; x *= y + 1;

x = x * (y+1)

25 Computer Industry lab.


Java
Cast Operators
 Data Type Casting Operator

(Data Type) Expression


 Cast operator : ( , )

(int) 3.75 ===> 3


(float) 3 ===> 3.0
(float) (1 / 2) ===> 0.0
(float)1/2 ===> 0.5

26 Computer Industry lab.


Java
Operator Precedence

Operator Association Precedence


Left Assoc. (High)
() [] .
! ~ ++ -- + - (Data Type) Right Assoc.
* / % Left Assoc.
+ - Left Assoc.
<< >> >>> Left Assoc.
< <= > >= instance Left Assoc.
== != Left Assoc.
& Left Assoc.
^ Left Assoc.
| Left Assoc.
&& Left Assoc.
|| Left Assoc.
?: Right Assoc.
= += -= *= /= %= &= ^= |= <<= >>= >>>= Right Assoc. (Lo
w)

27 Computer Industry lab.


Java
Operator Precedence


a=x+y-z; // Left Association
 b = -x ; // Right Association
 c = -x++ ;
 d = -++x ;
 e = -x + z ;

28 Computer Industry lab.


Java
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
normally. 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”
http://ebiz.u-aizu.ac.jp/~paikic/lecture/2005-1/code-examples/Java2-1.5/Code/Ch03/TryAssertions.java

29 Computer Industry lab.


Java
Exercise
 Step 1 , 2
 You can try by yourself.
 Step 3 Magic Square
blank blank blocked
Start
8 1 6 blank Principle
blocked 1. Basically go right up
direction
3 5 7 blank 2. If blank, then go to
opposite block
3. If blocked, go down
4 9 2

30 Computer Industry lab.


Java
Exercise
 Step 4
 Refer to Slides #8, #23.

31 Computer Industry lab.


Java

You might also like