1 / 39

一则广告

一则广告. 急招四名有经验的兼职 JSP 程序员 国庆需要加班; 做 JSP 界面工作(不包括美工和 serverlet); 最好是一个互相比较熟悉,有一定配合的团队,有一个项目领导最好; 联系人:我, libo@buaa.edu.cn. 第三章 表达式与流控制. 运算符与表达式 流控制. 运算符与表达式. 与 C 不同之处: Java 是强类型语言,类型限制比 C 严格,运算 符的操作对象的类型会受到更多限制。 Java 不提供指针运算符,增加了对象操作符 instanceof , 字符串运算符 + 和零填充的右移 >>> 等。.

rona
Download Presentation

一则广告

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 一则广告 • 急招四名有经验的兼职JSP 程序员 • 国庆需要加班; • 做JSP 界面工作(不包括美工和serverlet); • 最好是一个互相比较熟悉,有一定配合的团队,有一个项目领导最好; • 联系人:我,libo@buaa.edu.cn

  2. 第三章 表达式与流控制 • 运算符与表达式 • 流控制

  3. 运算符与表达式 • 与C不同之处: • Java是强类型语言,类型限制比C严格,运算 • 符的操作对象的类型会受到更多限制。 • Java不提供指针运算符,增加了对象操作符 • instanceof,字符串运算符+和零填充的右移>>>等。

  4. Java操作符类别 • 算术运算操作符:+,-,*,/, %, ++, - - • 关系操作符:>, >=, < <=, ==, != • 位操作符:>>, <<, >>>, & , |, ^(逐位异或),~ (按位取反) • 逻辑操作符:&, |, !, ^(异或),&&, || • 赋值操作符:=, +=, -=, *=, /=, %=,&=, |=, ^=, <<=, >>=, >>>= • 其它操作符: ?: , [], . , ( ),(type), new, instanceof

  5. 算术运算操作符

  6. 算术运算操作符

  7. 混合类型算术运算结果类型

  8. 关系操作符

  9. 位操作符 - 移位运算

  10. 位操作符 -逻辑运算

  11. 位操作符 - 异或

  12. 位操作示例 public class BitwiseDemo { static final int VISIBLE = 1; static final int DRAGGABLE = 2; static final int SELECTABLE = 4; static final int EDITABLE = 8; public static void main(String[] args) { int flags = 0; flags = flags | VISIBLE; flags = flags | DRAGGABLE; if ((flags & VISIBLE) == VISIBLE) { if ((flags & DRAGGABLE) == DRAGGABLE) { System.out.println("Flags are Visible and Draggable."); } }

  13. flags = flags | EDITABLE; if ((flags & EDITABLE) == EDITABLE) { System.out.println("Flags are now also Editable."); } } } 运行结果: Flags are Visible and Draggable. Flags are now also Editable.

  14. 逻辑操作符

  15. 赋值操作符

  16. 其它操作符

  17. 逻辑操作符与位操作符 • 逻辑操作符与位操作符相同的,根据操作数判定是何种运算符 • &, | -- 称为不短路与、或; • &&,|| -- 称为短路与、或。

  18. 用+ 运算符连接字符串 • String salutation = “Dr. ”; • String name = “Pete” + “Seymour” ; • String title = salutation + name ; • 则title 值:Dr. Pete Seymour

  19. 右移操作符 >> 和 >>> >> :带符号右移 -256 >> 4 = -256/24 = -16 1010 … >> 2  111010... >>>:无符号右移,以0 填充 1010 … >>> 2  001010 ...

  20. Java 强制类型转换 一般形式:(type) expression 例:(float)x/2 对强制类型转换的限制: 整型与浮点型可以相互转换,但基本类型 和数组、对象等复合类型之间不能互相转换。

  21. 运算符的优先级

  22. 流控制  while  do … while  for If … else  switch

  23. while 语句 一般格式: while (expression) { statement(s) }

  24. While语句举例 public class WhileDemo { public static void main(String[] args) { String copyFromMe = "Copy this string until you " + "encounter the letter 'g'."; StringBuffer copyToMe = new StringBuffer(); int i = 0; char c = copyFromMe.charAt(i); while (c != 'g') { copyToMe.append(c); c = copyFromMe.charAt(++i); } System.out.println(copyToMe); } } 结果: Copy this strin

  25. do-while语句 一般格式: do { statement(s) } while (expression);

  26. do-while语句示例 public class DoWhileDemo { public static void main(String[] args) { String copyFromMe = "Copy this string until you " + "encounter the letter 'g'."; StringBuffer copyToMe = new StringBuffer(); int i = 0; char c = copyFromMe.charAt(i); do { copyToMe.append(c); c = copyFromMe.charAt(++i); } while (c != 'g'); System.out.println(copyToMe); } } 结果: Copy this strin

  27. For 语句 一般格式: for (initialization; termination; increment) { statement(s) }

  28. For 语句示例 public class ForDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } System.out.println(); } } 运行结果: 32 87 3 589 12 1076 2000 8 622 127

  29. If … else 语句 一般格式: if (expression) { statement(s1) } else{ statement(s2) }

  30. If … else 语句示例 public class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } } 运行结果: Grade = C

  31. Switch语句 switch (expr1){ case expr2: … ; break; case expr3: … ; break; … } 注意:expr1的值必须是与int兼容的类型,byte, short,char而浮点型(float, double)或long 不可以。

  32. Switch 与 if 语句 int month = 8; if (month == 1) { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } . . . // and so on If 语句可以使用一个值的范围或条件进行判断, 而switch只能根据单个整型值进行判断, 并且这个值必须唯一。

  33. Break语句 Break语句结束了switch 语句。程序流控制转到 switch 语句块后的第一条语句。没有break语句,程序控制将执行随后的各个 case 语句。

  34. Break语句示例 public class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break;

  35. case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; break; } System.out.println("Number of Days = " + numDays); } } 运行结果: Number of Days = 29

  36. Default 语句 在 switch 语句块的最后,对于没有匹配任何case语句条件的值,执行 default 语句。

  37. 特殊跳转语句  break [label] 从switch语句、循环语句中跳出。 continue[label] 跳过标号循环体的其余部分,不带label 跳过 最内层循环的剩余语句。 label: statement

  38. 特殊跳转语句举例 1Loop: while (true){ 2 for( … ){ 3 switch( ){ 4 case -1: 5 case ‘\n’: 6break loop ; 7 … 8 } 9 } 10 } 11 test: for( … ){ 12 … 13 while(… ){ 14 if( …){ 15 … 16continue test ; 17 } 18 } 19 } //跳出while去11行 // 跳到11行

  39. 关于程序控制的小经验 • 永远不要使用goto 或者标号,这都是旧的语法,虽然可能不会伤害可读性,但是仍然不是好的编码习惯; • 能够使用for 循环的时候,最好用for 循环,效率最高,代码最简单; • 多层嵌套的if…else 很难读,要注意格式和注释

More Related