java 学习笔记(三)Flow of Control

                    Charpter3    Flow of Control

3.1 branching mechanism

3.1.1 if-else 

使用:if 和 else中的内容都是一个boolean的表达式或者值。如果if后为true,则执行else 之前的内容,也叫就是if 区域内的内容,如果if后为false,则执行else块后的内容。

NB:(1) if 和else后都有括号,其中内容都为boolean的表达式

         (2) if-else中可以进行嵌套,就是if(if-else)--- else 或者 if --- else(if-else)

         (3)if 条件句可以单独使用,不需要else ; 但是else语句不可以单独使用而不使用if

3.1.2 if - else if - else if - 。。。 - else

与if - else原理相同,只是if-else只有两个选择,但是如果有多个选择的时候,就可以使用下面的语句。但是注意最后一个else没有boolean 语句。该用法是,只要有一个if 后面的语句判断完是true,就结束全部的语句。如果前面所有的if或者else if 都没有true,那么就进入else中,执行其中的语句。

          

3.1.3 switch - case

                    

(1)switch后的语句叫controlling expression ;switch后的内容controlling expression,一般是char int short byte string;

(2)case后有一个常数和一个冒号;case后的叫case labels;对其的要求是必须是同种type;通过比较程序执行时输入的controllling expression值和哪个case label相同,选择执行哪个case后的语句,然后通过break语句退出整个结构。如果在case labels后的语句没有break,那么会继续执行下个case中语句。比如:

                

                     

(3)如果所有的case labels都不对,那么就会进入到最后的default中,执行其后的语句。当然该default不是必须的当没有default后,如果前面的care labels都不对,那么什么都不会执行。一般情况下,可以把error写在该部分。

其实switch-case语句和if-else if - else语句的工作原理是相同的。

3.2 boolean expression

3.2.1 比较符号

      

 

3.2.2 compareTo 

compareTo函数:用来比较两个string。按字典顺序比较两个字符串,该比较基于字符串中各个字符的Unicode值

s1.compareTo(s2) : 当s1 在 s2 之前的时候,返回 negative number ;相等返回0;否则返回负数

 

3.2.3 运算符号


(1)and  --- &&;

使用: boolean1 expression && boolean2 expression

判定方法:同真为真,一假全假

(2) or --- ||

使用: boolean1 expression || boolean2 expression

判定方法:同真才真,一假就假

(3)not --- !

使用: !boolean expression

判定方法:本来是真,后来为假;本来为假,后来为真

注意:

a) short-circuit evaluation or lazy evaluation.现象

在使用的时候,比如&&(boolean1 expression && boolean2 expression )如果第一个表达式判定为false后,不会进行第二个表达式的判断。以为结果肯定是false。同理,boolean1 expression || boolean2 expression )的时候,如果判断第一个表达式是true,那么第二个表达式也不需要判断,因为结果一定是true。

如果只是判断上面还好,但是如果涉及计算,比如boolean b=((number > 0) &&(number ++ > 2) )当number是1的时候,b是true,那么之后的number的结果也仍然是1,不会被结算。

b)&& 和 & 、|| 和 | 的区别 参考:https://blog.csdn.net/qq_36098284/article/details/80709301

c)short-circuit的好处,避免不必要的计算和出错。比如:

if ( (kids != 0) && ((pieces/kids) >= 2) ) System.out.println("Each child may have two pieces!");

当kids 为0的时候,不会进行(pieces/kids) >= 2 的计算,那么就不会有run-time error,这么0坐分母的情况就不会出现,也不会报错。

                

 

3.3 LOOP

3.3.1 while

(1)语法:

                                             

 

(2)判断:首先判断boolean值,true的话,执行statement(循环体),如果为false,则不进入while中,执行之后的部分。

 

3.3.2 do-while

(1)语法:

                                        

(2)判断:首先,执行一次循环体,也就是do-while中的循环体,然后判断while后的boolean是否为true,如果为true则继续执行循环体,如果为false,则结束循环,执行之后的内容。

NB:do-while和while的区别:主要在当boolean为false的时候,是否执行循环体,do-while至少执行一次,但是while不执行。

 

3.3.3 for

(1)语法:for (Initialization; Boolean_Expression; Update)

intitialization:初始化,用于变量的初始化;

boolean expression:用于判断循环是否继续

update:对变量的值进行处理

NB:for循环中可以嵌套for循环;

 

3.3.4 break & continue

两个关键词都只能用在循环中,break也可以用再switch-case中,但是不可以用再if-else中

break:结束循环;

continue:结束此次循环

 

3.3.5 exit statement

break只能结束循环,但是exit语句可以结束整个程序。只要该函数被调用,program就会结束

语法:System.exit(0); 一般使用0,当然也可以使用其他别的数字

 

3.4 debugging

3.4.1 loop bugs

(1)off-by-one

因为while或者for中的boolean expression设计有误,可能导致loop body无限次的被执行

NB:有的时候,如果boolean expression 使用浮点数的比较(比如 == 或者 !=)的时候,就会出错,因此一般不使用浮点数来比较大小。

3.4.2 tracing variables

跟踪变量的每时每刻的值,就可以发现bug所在

3.4.3 assertion check

断言:就是一个语法:assert Boolean_Expression;

使用:

If you run your program in the proper way, the assertion check behaves as follows:

If the Boolean_Expression evaluates to  true , nothing happens, but if the Boolean_Expression evaluates to  false , the program ends and outputs an error message saying that an assertion failed.

例子:

首先这是一个正常的程序,用注解的形式描述n 和 sum 的值

                                       

下面是使用了断言的代码:

                                      

 

3.5  random number generation

3.5.1 the Random object

首先导入:import java.util.Random;

使用:Random randomGenerator = new Random();

          int r = randomGenerator.nextInt(); (To generate a random integer in the range of all possible integers)

          int r = randomGenerator.nextInt(n); (To generate a random integer in the range from 0 to n-1, use)

          double r = randomGenerator.nextDouble();(To generate a random  double )

If you want a random number in a different range, then you can scale the number by adding an offset. For example, to generate a random number that is 4, 5, or 6, use int r = randomGenerator.nextInt(3) + 4;

 

3.5.2 The Math.random() Method

   Java also includes a method to generate random doubles without requiring the user to create an instance of the  Random class. 

     In fact, when this method is called for the first time, Java internally creates an instance of the  Random class and
invokes the  nextDouble() method.

The method  Math.random() returns a random double that is greater than or equal to 0.0 but less than 1.0. 

For example, if you need an  int in the range from 1 to 6, the following code could be used:
                     int num = (int)(Math.random() * 6) + 1;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值