switch结构:

6.2 switch结构:  


变量可以是:variable can be:(mark: tried, can not be long)
char/int/short/byte(java1.7 就可以用String来switch了)

 

public class Test{
  public static void main(String[] args){
    double d1=0.0, d2=0.0;
    d1 = 11;
    d2 = 22;
    char o = '+';
    switch(o){
       case '+':
           System.out.println(d1+d2);
           break;
       case '-':
           System.out.println(d1-d2);
           break;
       case '*':
           System.out.println(d1*d2);
           break;
       case '/':
           System.out.println(d1/d2);
    }
  }
}

result is:

33.0

 




6.3 循环



6.3.1 while 循环

public class Test{
  public static void main(String[] args){
    int lim = 5;
    int sum = 0;
    int i = 1;
    while(i<=lim){
        sum =sum + i;
        i++;
    }
    System.out.println("sum = " + sum);

    int j = 1;
    while(j<=4){
        System.out.println("j=" + j);
        j++;
    }
    System.out.println(j);
  }
}

 

result:

sum = 15
j=1
j=2
j=3
j=4
5



6.3.2 do-while循环

package com;
public class Test{
      public static void main(String[] args){
        int lim = 0;
        int sum = 0;
        int i = 1;
        do{
            sum =sum + i;
            i++;
        } while(i<=lim);
        System.out.println("sum = " + sum);

      }
    }
结果:

sum = 1





6.3.3 for循环



break语句:从switch分支或循环中跳转出来,执行其后继语句。

continue语句:终止当前这一轮循环,继续下一轮。


(demo, also add in single step debug.)
public class Test{
  public static void main(String[] args){
    int lim = 4;
    int sum = 0;

    for(int i=1; i<=lim; i++){
        sum += i;
    }
    System.out.println("sum = " + sum);
  }
}

the result is:

sum = 10

public class Test{
  public static void main(String[] args){

    for(int i=20; i>0; i -= 2){
        System.out.println("The number is " + i);
    }

    for(int i=20; i<100; i *= 2){
        System.out.println("The number is " + i);
    }
/*
for(int i=10; i>0; i++){
System.out.println("无限循环");
}*/
  }
}

 

the result is:

The number is 20
The number is 18
The number is 16
The number is 14
The number is 12
The number is 10
The number is 8
The number is 6
The number is 4
The number is 2
The number is 20
The number is 40
The number is 80

 




public class Test{
    public static void main(String[] args){
        int limit = 4;
        long fac;

        for (int i=1;i<=limit;i++){
            fac = 1;
            for (int j=2; j<=i;j++){
                fac = fac*j;
            }
            System.out.println(i + "!" + " is " + fac);
        }
    }
}

the result is:

1! is 1
2! is 2
3! is 6
4! is 24

 



public class Continue{
    public static void main(String[] args){
        for(int i=1;i<=10;i++){
            if(i==5 || i==8){
                continue;
            }
            System.out.println("i=" + i);
        }
    }
}

result is:

i=1
i=2
i=3
i=4
i=6
i=7
i=9
i=10



public class Test{
    public static void main(String[] args){
        for(int i=1;i<=10;i++){
            if(i==5 || i==8){
                break;
            }
            System.out.println("i=" + i);
        }
    }
}
结果:

i=1
i=2
i=3
i=4