Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II."— Presentation transcript:

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

2 Contents Introduction for Repetition Statement d0-while Repetition Statement switch Multiple-Selection Statement break/continue Statement Logical Operator Triangle Example

3 Introduction Essentials of Counter-Controlled Repetition  Control Variable  Initial Value for Control Variable  Increment  Loop-Continuation Condition

4 for Repetition Statement Syntax  Expression 1: Initialization  Expression 2: Loop-continuation Condition  Expression 3: Incrementing/Decrementing 4 for(expression1; expression2; expression3) statement; for(expression1; expression2; expression3){ statement1; statement2; }/* End of for-loop */

5 for Repetition Statement Syntax Example for (int counter = 1; counter <= 5; counter++ ) Initial value of control variableIncrement of control variable Control variablenameFinal 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.

6 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 */

7 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 */

8 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

9 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);

10 d0-while Repetition Statement count=0; do{ System.out.printf(“%d “, counter); count++; }while(count <=5); /* End of do-while loop */ 0 1 2 3 4 5

11 switch Multiple-Selection Statement Syntax switch(constant expression){ case 1: break; case 2: break; …… default: break; }/* End of switch */

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

13 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 */

14 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 */

15 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();

16 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 (^) 17 (grade >= 70 && grade <= 90)(grade >= 70 || grade <= 60)

18 Logical Operator && : Condition AND || : Condition OR 18 expression1expression2 expression1 && expression2 false truefalse truefalse true expression1expression2 expression1 || expression2 false true falsetrue

19 Logical Operator ^ : Exclusive OR  True Conditions: One of its operand is true and the other is false. ! : Negation 19 expression1expression2 expression1 ^ expression2 false true falsetrue false expression ! expression falsetrue Truefalse

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. 20 (gender ==female) && (age >=65) (gender ==female) && (++age >=65) (gender ==female) || (age >=65) (gender ==female) || (++age >=65)

21 Logical Operator Short-Circuit Evaluation (gender ==female) && (++age >=65) genderage beforeage after male60 female6061 (gender ==female) || (++age >=65) genderage beforeage after male6061 female60

22 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’

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

24 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 */

25 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 */

26 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 */ a aa aaa aaaa aaaaa aaaaaa # ## ### #### ##### ###### ####### ######## ######### ##########

27 www.themegallery.com


Download ppt "INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II."

Similar presentations


Ads by Google