Вы находитесь на странице: 1из 22

Control Flow and Statement

Assignment Statement Compound Statement Control Statement Standard I/O

Java Statement
Assign St.
Compound St. : var = exp : { }

Condition St. : if St., switch St.

Statement

Control

Loop St. : for St., while St., do-while St. Branch St. : break St., continue St., return St.

Exception Handling St.: try-catch-finally Synchronization St. : synchronized Standard I/O : System.in.read(), System.out.println()

Assignment Statement
Assign the value to variable Form : <variable> = <expression>

remainder = dividend % divisor; i = j = k = 0; x *= y;

Type Conversion

Narrowing type casting : cast operator

[AssignmentSt.java]

Compound Statement
To give one statement grouping several statements Form

{ <declaration> or <statement> }
if (a > b) a--; b++;

[CompoundSt.java]

Local Variable

Can be referred only in the declared block

[AnotherBlock.java], [LocalVariable.java]

Control Statement
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 Statemnt : break St., continue St., return St.

Conditional Statement - if Statement

Form of if Statement

if ( <conditional expression> ) <statement> if ( < conditional expression > ) <statement1> else <statement2>
if (a < 0) a = -a; if (a > b) m = a; else m = b;

Result of conditional expression : Logical Type(true or false) [IfSt.java], [IfElseSt1.java]

Conditional Statement - if 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>

[NestedIf.java]

Conditional Statement - switch Statement

Form of switch statement


switch ( <expr.> ) { case <const. expr. 1> : <statement 1> case < const. expr. 2> : < statement 2> : case < const. expr. n> : < statement n> default : < statement> } break statement

[SwitchSt.java], [SwitchSt2.java]

Repeat Statement - for Statement


Repeat the sequence of statement as many as defined. Form of 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;

Repeat Statement - for Statement

Execution order of for statement


1 2

< < for ( < expr 1> ;Expr2> ; expr 3> )

3 6
False

True

< Statement >

[ForSt.java]

Repeat Statement - for Statement

Infinite Loop using for statement


for ( ; ; ) <statement>

To stop the loop : break statement, return statement

Nested for statement

Multi dimensional array


for (i=0; i<N; ++i) for (j=0; j<M; ++j) matrix[i][j] = 0;

[PrintMatrix.java]

Repeat Statement - while Statement

Form of while statement


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

Order of Execution
(1)
while (< Cond. Expr. >)

(4)
False

(2) True
< Statement >

(3)

Repeat Statement - while Statement

Comparison of for statement and while statement


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

Repeat Statement do - while Statement


After executing the repeating statements, then check the conditional expression Form of do-while statement Although the conditional expression

do
<statement> while (<conditional expression>);

is false, execute the statement one time above at least

[DoWhile.java]

Branch Statement - break Statement


To move control to the out of the block From of break statement

break [label] ;
int i = 1; while (true) { if (i == 3) break; System.out.println("This is a " + i + " iteration"); ++i; }

[BreakSt.java]

Branch Statement - break Statement

Label break statement


Can be used instead of goto statement Form of usage


labelName : Rep. St. 1 { Rep. St. 2 { // . . . break; // . . . break labelName; } // . . . }

Branch Statement continue Statement


To move control to the start of next repeatation From of continue statement

continue [Label] ;

When used in for statement


for (i=0; i<=5; ++i) { if (i % 2 == 0) continue; System.out.println("This is a " + i + " iteration"); }

Branch Statement continue Statement

When used in while statement


i = 0; while (i <= 5) { ++i; if (i % 2) == 0) continue; System.out.println("This is a odd iteration - " + i); }

[ContinueSt.java]

Branch Statement continue Statement

Label continue statement


labelName: Rep. St. 1 { Rep. St. 2 { // ... continue; // ... continue labelName; } }

[LabeledContinue.java]

Branch Statement return Statement


To terminate the execution of method, then pass the method of caller that control Forms of return statement

return; return <expr.>;

[ReturnSt.java]

Standard I/O

Method of input / output to standard file assigned by system


Standard Input / Output

Key Board Screen

Standard Input File : in Standard Output File : out Standard Error File : err

Standard Input / Output


Primitive Package of Java : java.io provides standard I/O Standard Input Method :
System.in.read()

Read a character from keyboard and return the integer type for that code value.

Standard Output Method :


System.out.print(), System.out.println(), System.out.write()

[SimpleIO.java], [CopyInputToOutput.java]

Вам также может понравиться