instructor: shih-shinh huang windows programming using java chapter5: control statements part ii

27
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II

Post on 19-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

4

for Repetition Statement

Syntax

Expression 1: Initialization Expression 2: Loop-continuation Condition Expression 3: Incrementing/Decrementing

for(expression1; expression2; expression3)statement;

for(expression1; expression2; expression3){statement1;statement2;}/* End of for-loop */

for Repetition Statement

Syntax Example

for ( int counter = 1; counter <= 5; counter++ )

Initial value of control variable Increment of control variable

Control variable name Final value of control variable keyword

Loop-continuation condition

counter++

Establish initial value of control variable.

Determine if final value of control variable has been reached.

counter <= 5 statements

true

false

int counter = 1

Body of loop (this may be multiple statements)

Increment the control variable.

for Repetition Statement

Remarks The variable scope of the control variables

are only in the for statement. If the loop-continuation condition is

initially false, it does not execute the for loop.

count=0;

for(int count=0; count < 10; count++){

………

}/* End of for-loop */

for Repetition Statement

Remarks All expressions in for statement is

optional. If the loop-continuation condition is omitted,

Java creates an infinite loop. You can omit increment expression, if the

program calculates the increment in the loop’s body.for(; ;){

………

}/* End of for-loop

*/Infinite Loop

for(int i=0;i<10 ;){

i=i+5;

}/* End of for-loop

*/

for Repetition Statement

Examples for(int i=1 ; i <=100 ; i++)

for(int i=100 ; i>1 ; i--)

for(int i=7 ; i <=77 ; i+=7)

for(int i=20 ; i >= 2 ; i-=2)

Vary the control variable from 1 to 100 in increments of 1.

Vary the control variable from 100 to 1 in decrements of 1

Vary the control variable from 7 to 77 in increments of 7

Vary the control variable from 20 to 2 in decrements of 2

d0-while Repetition Statement

Description It tests the loop-continuation condition

after executing the body. The body always executes at least once.

true

false

action(s)

condition

Initialization;

do{

Statement;

Increment;

}while(Loop-continuation Condition);

switch Multiple-Selection Statement

Syntax

switch(constant expression){

case 1:

break;

case 2:

break;

……

default:

break;

}/* End of switch */ break;

case: 1 case action(s)true

false

.

.

.

break;

case action(s) break;

false

false

case: n case action(s) break;

default action(s)

true

true

case: 2

12

switch Multiple-Selection Statement

Description It is the statement to perform different

actions based on the constant expression values.

Each action is associated with the value Constant Integral Expression Constant String Expression.

It contains a sequence of case labels and an optional default.

switch Multiple-Selection Statement

Description The break statement is used to exit the switch statement.

switch(constant expression){

case 1:

break;

case 2:

break;

……

}/* End of switch */

switch(constant expression){

case 1:

case 2:

break;

……

}/* End of switch */

switch Multiple-Selection Statement

public void ShowGradeLetter(int grade){

switch(grade/10){

case 10:

case 9: System.out.println(“Rank: A”);

break;

case 8: System.out.println(“Rank: B”);

break;

case 7: System.out.println(“Rank: C”);

break;

default: System.out.println(“Rank: D”);

break;

}/* End of switch */

}/* End of ShowGradeLetter */

break/continue Statement

break Statement The break statement is used in a while, for, do…while, and switch.

It causes immediate exit from the statement.

Execution continues with the first statement after the control statement.for (int i = 1; i <= 10; i++) {

if (i == 5)break;

System.out.printf(“%d\n", i);}/* End of if-condition */System.out.println();

break/continue Statement

continue Statement The break statement is used in a while, for, do…while, and switch.

It skips the remaining statements in the loop.

It proceeds with the next iteration of loop.for (int i = 1; i <= 10; i++) {

if (i == 5)continue;

System.out.printf(“%d\n", i);}/* End of if-condition */System.out.println();

17

Logical Operator

Description It enables you to form more complex

conditions.Operators

Conditional AND (&&) Conditional OR (||) Logical NOT (!) Boolean Logical exclusive OR (^)(grade >= 70 && grade <= 90) (grade >= 70 || grade <= 60)

18

Logical Operator

&& : Condition AND

|| : Condition OR

expression1 expression2 expression1 && expression2

false false false

false true false

true false false

true true true

expression1 expression2 expression1 || expression2

false false falsefalse true truetrue false truetrue true true

19

Logical Operator

^ : Exclusive OR True Conditions: One of its operand is true

and the other is false.

! : Negation

expression1 expression2 expression1 ^ expression2

false false falsefalse true truetrue false truetrue true false

expression !expressionfalse trueTrue false

20

Logical Operator

Short-Circuit Evaluation The parts of an expression are evaluated

only until it’s condition value is known.

It will have different result if the expression has side effect.

(gender ==female) && (age >=65)

(gender ==female) && (++age >=65)

(gender ==female) || (age >=65)

(gender ==female) || (++age >=65)

Logical Operator

Short-Circuit Evaluation (gender ==female) && (++age >=65)

gender age before age after

male 60 60

female 60 61

(gender ==female) || (++age >=65)

gender age before age after

male 60 61

female 60 60

Triangle Example

Requirements Design a class that can display triangle

patterns. The layer of triangle and character in the

pattern can be specified by the user.

Layer = 5

Character = ‘*’

Layer = 4

Character = ‘a’

Triangle Example

Design Fields

int m_iLayer: the layer of the triangle char m_chTriChar: the char used for triangle

pattern. Methods

Triangle(int, int): constructor ShowTriangle(void): the function to draw

triangle.

Triangle Example

public class Triangle {

private int m_iLayer;

private char m_chTriChar;

/* constructor */

public Triangle(int layer, char ch){

m_iLayer = layer;

m_chTriChar = ch;

}/* End of constructor */

/* show the triangle pattern */

public void ShowTriangle(){

…………

}/* ENd of ShowTriangle */

}/* End of Triangle */

Triangle Example

public class Triangle {

private int m_iLayer;

private char m_chTriChar;

public Triangle(int layer, char ch){

……

}/* End of constructor */

public void ShowTriangle(){

for(int i=0; i < m_iLayer; i++){

for(int j=0; j <= i; j++){

System.out.printf("%c",m_chTriChar);

}/* End of for-loop (j) */

System.out.printf("\n");

}/* End of for-loop (i) */

}/* ENd of ShowTriangle */

}/* End of Triangle */

Triangle Example

public class TriangleTest {

public static void main(String args[]){

Triangle triangle1 = new Triangle(6, ‘b');

triangle1.ShowTriangle();

Triangle triangle2 = new Triangle(10, ‘#’);

triangle2.ShowTriangle();

}/* End of main */

}/* End of TriangleTest */

aaaaaaaaaaaaaaaaaaaaa#######################################################

www.themegallery.com