《JAVA编程思想》学习备忘(第135页:Controlling Execution)

Controlling Execution
 
Like a sentient creature,a program must manipulate its world and make choices during execution.In Java you make choices with execution control statements. 
像一个有感知的生物体,一个程序在其运行是必须要掌控它的世界并做出选择。Java中你用运行控制语句做出选择。
true and false
在上章中所述的所有条件运算符,均可用来产生一个条件语句。且表达式返回true或者false 。
注意:Java不允许将一个数字当作boolean一样来使用,这一点不同与C语言(0为假,非0为真)。但可转化为boolean值来使用:if(a != 0)。
 
if-else
if-else语句是控制程序流最基础的方法。else是可选项,这样你可以用两种形式使用if:
if(Boolean-expression)
   statement
或者
if(boolean-expression)
  statement
else
  statement
 
Iteration
循环被while,do-while 和 for 控制。
举个简单例子:
public class WhileTest{
    static boolean condition(){
        boolean result = Math.random()<0.99;
        System.out.print(result + ", ");
        return result;
    }
    public static void main(String [] args){
        while(condition())
            System.out.println("Inside 'while'");
        System.out.println("Exited 'while'");
    }
}
 
do-while
do-while循环形式是:
do
  statement
while(Boolean-expression);
 
for
for循环形式是:
for(initialization; Boolean-expression;step)
    statement
注意:在Java中,你可以在任何你需要变量的地方声明变量。
 
The comma operator
逗号运算符中Java中的唯一用处是,在for循环中的控制表达式中,用于两个初始表达式和步进选项表达式之间。
使用逗号运算符,你可在for语句中定义多项变量,但它们必须是同一类型:
public class CommaOperator{
    public static void main(String[] args){
        for(int i = 1,j = i+10;i < 5; i++, j = i+2)
            System.out.println("i = " + i + " j = " + j);
    }
}
 
Foreach syntax
Java SE5 introduces a new and more succinct for syntax,for use with arrays and containers(you'll learn more about these in the Arrays and Contariners in Depth chapter).This is often called the foreach syntax,and it means that you don't have to create an int to count through a sequence of items-the foreach produces each item for you,automatically.
For example,suppose you have an array of float and you'd like to select each element in that array:
| import java.util.*;
public class ForEachFloat{
    public static void main(String[] args){
        Random rand = new Random(47);
        float f[] = new float[10];
        for(int i = 0; i < 10; i++)
          f[i] = rand.nextFloat[10];
        for(float x : f)
          System.out.println(x);
    }
}
float为数据类型,f为数组名,x表示数组元素值。
 
创建一个静态的range方法:
package staticRange;
public class Range {
    public static int[] range(int x){
        int[] array = new int[x];
        for(int i = 0; i < x; i++)
            array[i] = i;
        return array;
    }
    public static int[] range(int x,int y){
        int[] array = new int[y-x];
        for(int i = x,j=0; i < y; i++,j++)
            array[j] = i;
        return array;
    }
    public static int[] range(int x,int y,int z){
        int[] array;
        if((y-x)%z==0)
            array = new int[(y-x)/z];
        else
            array = new int[(y-x)/z+1];
        for(int i = x,j=0; i < y; i=i+z,j++)
            array[j] = i;
        return array;
    }
}

 

使用range方法的例子:

import static staticRange.Range.range;
import static staticPrint.Print.print;
public class ShowRange {

       public static void main(String[] args){

       for(int i : range(10))
           print(i + " ");
       print("/n");
       for(int i : range(5, 10))
           print(i + " ");
       print("/n");
       for(int i : range(5,29,3))
           print(i + " ");
       }
}

运行程序观察输出结果。

 

return
关键字return有两个目的:它指出方法将返回什么样的值,它用来退出当前方法。
 
break and continue
break用于不执行剩余语句直接跳出循环;continue用于不执行剩余语句进行下一轮的循环。 
import static staticRange.Range.range;
public class BreakAndContinue {
    public static void main(String[] args){
        for(int i = 0; i < 100; i++){
            if(i==74) break; //Out of for loop
            if(i % 9 != 0) continue; //Next iteration
            System.out.print(i + " ");
        }
        System.out.println();
        for(int i : range(100)){
            if(i == 74) break; //Out of for loop
            if(i % 9 != 0) continue;  //Next iteration
            System.out.print(i + " ");
        }
        System.out.println();
        int i = 0;
        //An "infinite loop";
        while(true){
            i++;
            int j = i*27;
            if(j == 1269) break; //Out of loop
            if(i % 10 != 0) continue; //Top of loop
            System.out.print(i + " ");
        }
    }
}
 
The infamous "goto"
The goto keyword has been present in programming languages from the beginning.Indeed,goto was the genesis of program control in assembly language:"If condition A,then jump here;otherwise,jump there."
尽管goto在Java中是一个保留字,它不在这门语言中使用;Java没有goto.
(其它内容略)
 
switch
The switch is sometimes called a selection statement.The switch statement selects from among pieces of code base on the value of an integral expression.Its general form is:
switch(integral-selector){
    case integral-value1 : statement;break;
    case integral-value2 : statement;break;
    case integral-value3 : statement;break;
    case integral-value4 : statement;break;
    case integral-value5 : statement;break;
    // ...
    default: statement;
}
break是可选项,如果少了break,在执行完选择点的语句后,将顺序执行下一条语句。选择序列可递增排列也可递减排列。掌握这些特性,加以灵活应用,将会有异想不到的奇效。
注意:条件语句要求是一个整型值。(下一章,你将会看到Java SE5 新的enum特性,这将使switch的条件语句的严格限制有所松动。)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值