Java - Trang ď Chap03-04

16 112 0
Java - Trang ď Chap03-04

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Programming Java Control Statements Incheon Paik 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 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 Java Computer Industry lab The If Statement and If – else Statement „ Form of the if Statement „ if ( ) „ if ( < conditional expression > ) else „ Result of conditional expression : Logical Type(true or false) Java Computer Industry lab The If and If-Else Statement „ Nested if statement if () if () // if () else if () < statement 2> … else if () < statement n> else < statement> „ A Block of Code if (expression) { statement1; statement2; ………… } Java Computer Industry lab The For Statement „ „ Repeat the sequence of statements as many as defined Form of the for statement for ( ; < expr 2> ; < expr 3>) „ „ „ : initialize the control variable : check the control variable : modify the control variable s = 0; for (i=1; i;< Expr3>) Expr1>; ; False Java 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); } // A colon separates the two control expressions in this type of for loop Java Computer Industry lab The Repeat Statement - while Statement „ Form of the while statement while ( cond Expr ) „ Order of Execution i = 1; s = 0; while (i ) (4) False (2) True (3) < Statement > 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 – - while Statement After executing the repeating statements, then check the conditional expression Form of the do-while statement „ „ Although Althoughthe theconditional conditionalexpression expression while (); 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 { Rep St { // 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>, ^, ~ „ 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(>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:: http://ebiz.u-aizu.ac.jp/~paikic/lecture/2005-1/code-examples/Java2-1.5/Code/Ch03/CollectionBasedForLoop.java http://ebiz.u-aizu.ac.jp/~paikic/lecture/2005-1/code-examples/Java2-1.5/Code/Ch03/CollectionBasedForLoop.java 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” http://ebiz.u-aizu.ac.jp/~paikic/lecture/2005-1/code-examples/Java2-1.5/Code/Ch03/TryAssertions.java 29 Java Computer Industry lab Exercise „ Step , „ „ Step Magic Square Start Java You can try by yourself blank blank blocked blank blank blocked 30 Principle Basically go right up direction If blank, then go to opposite block If blocked, go down Computer Industry lab Exercise „ Step „ Java Refer to Slides #8, #23 31 Computer Industry lab ... http://ebiz.u-aizu.ac.jp/~paikic/lecture/200 5-1 /code-examples /Java2 -1 .5/Code/Ch03/CollectionBasedForLoop .java http://ebiz.u-aizu.ac.jp/~paikic/lecture/200 5-1 /code-examples /Java2 -1 .5/Code/Ch03/CollectionBasedForLoop .java. .. mess-age starting with: java. lang.AssertionError Sample Code “TryAssertions .java http://ebiz.u-aizu.ac.jp/~paikic/lecture/200 5-1 /code-examples /Java2 -1 .5/Code/Ch03/TryAssertions .java 29 Java. .. 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

Ngày đăng: 09/12/2017, 02:06

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan