Java三種流程控制語句

三種流程控制語句

順序結構

  1. 從上到下依次執行
package Test3;

public class OrderDemo {
    public static void main(String[] args) {
        System.out.println(1);
        System.out.println(2);
        System.out.println(3);
    }
}

順序輸出:1 2 3

選擇結構

  1. if語句第一種
package Test3;
/*
 * if語句第一種:
 *     if(關係表達式){
 *        語句體;    
 *     }
 * 
 * 執行流程:
 *    1.計算關係表達式的值,看結果是true還是flase;
 *    2.若是true則執行語句體;
 *    3.若是flase則不執行語句體
 * 
 */
public class IfDemo1 {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;

        if(a>b){
            System.out.println("hello");
        }

        System.out.println("...........");

        if(a==b){
            System.out.println("hello");
        }
   }
}
  1. if語句第二種
package Test3;
/*
 * if語句第二種:
 *     if(關係表達式){
 *        語句體1; 
 *     }else{
 *        語句體2;
 *     }
 *     
 * 執行流程:
 *    1.計算關係表達式的值,看結果是true還是flase;
 *    2.若是true則執行語句體1;
 *    3.若是flase則執行語句體2    
 * 
 */
public class IfDemo2 {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;

        if(a==b){
            System.out.println("hello");
        }else{
            System.out.println("你好");
        }

        System.out.println("............");

        if(a>b){
            System.out.println("hello");
        }else{
            System.out.println("你好");
        }
   }
}
  1. if語句第三種
package Test3;
/*
 * if語句第一種:
 *     if(關係表達式){
 *        語句體;    
 *     }
 * 
 * 執行流程:
 *    1.計算關係表達式的值,看結果是true還是flase;
 *    2.若是true則執行語句體;
 *    3.若是flase則不執行語句體
 * 
 */
public class IfDemo1 {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;

        if(a>b){
            System.out.println("hello");
        }

        System.out.println("...........");

        if(a==b){
            System.out.println("hello");
        }
   }
}
  1. switch語句
package Test3;

import java.util.Scanner;

/*switch語句格式:
 *  switch(表達式){
 *      case 值1:
 *              語句體1;
 *              break;
 *      case 值2:
 *              語句體1;
 *              break;
 *      ......
 *      default:
 *              語句體n+1;
 *              break;
 *  }
 * 
 * 注:表達式只可以是 byte,short,int,char;jdk5以後可以是枚舉,jdk7以後可以是字符串;
 * 
 * 執行流程:
 *    1.計算表達式的值;
 *    2.將得到的值依次與case後的值進行匹配,一旦匹配成功則執行相應的語句體,遇到break則結束;
 *    3.若是都不匹配,則執行語句體n+1,遇到break則結束;
 */
public class SwitchDemo {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("只能輸入1,2,3:");
        int a = sc.nextInt();


        switch(a){

        case 1:
            System.out.println("你");
            break;

        case 2:
            System.out.println("好");
            break;

        case 3:
            System.out.println("呀");
            break;

        default:
            System.out.println("輸入錯誤,請重新輸入");
            break;
        }
    }
}

循環結構

  1. for循環
package Test3;
/*
 * for循環語句的格式:
 *   for(初始化語句;判斷條件語句;控制條件語句){
 *      循環體語句;
 *   }
 * 
 * 執行流程:
 *   1.執行初始化語句;
 *   2.執行判斷條件語句;
 *   3.執行循環體語句;
 *   4.執行控制條件語句;
 *   5.回到2在開始執行,直至不符合控制條件語句.
 * 
 */
public class ForDemo {
    public static void main(String[] args) {
        int sum = 0;

        for(int i=1; i<=10;i++){
            sum+=i;
        }
        System.out.println("sum:"+sum);
    }
}
  1. while循環
package Test3;
/*
 * while循環語句的格式:
 *      while(判斷條件語句){
 *          循環體語句;
 *          控制條件語句;
 *      }
 * 執行流程:
 *   1.執行判斷條件語句;
 *   2.執行循環體語句;
 *   3.執行控制條件語句;
 *   4.回到1在開始執行,直至不符合控制條件語句.
 * 
 * 
 * 
 * 
 */
public class WhileDemo {
    public static void main(String[] args) {
        int sum = 0;
        int i = 1;
        while(i<=10){
            sum+=i;
            i++;
        }
        System.out.println("sum:"+sum);
    }
}
  1. do…while循環
package Test3;
/*
 * do...while循環語句的格式:
 *      do{
 *          循環體語句;
 *          控制條件語句;
 *      }while(判斷條件語句);
 * 
 * 執行流程:
 *   1.執行循環體語句;
 *   2.執行控制條件語句;
 *   3.執行判斷條件語句;
 *   4.回到1在開始執行,直至不符合控制條件語句.
 * 
 */
public class DoWhileDemo {
    public static void main(String[] args) {
        int sum = 0;
        int i = 1;
        do{
            sum+=i;
            i++;
        }while(i<=10);
        System.out.println("sum:"+sum);
    }
}

三種循環體的區別

  1. do…while循環至少可以執行一次循環體,for和while循環只有在判斷條件成立纔可以執行循環體。

  2. for循環結束後初始化的變量不能繼續使用,而while循環結束後初始化的變量可以繼續使用

package Test3;
/*
 * for循環與while循環的區別
 * 
 */
public class CompareDemo {
    public static void main(String[] args) {

        for(int x=1;x<=5;++x){
            System.out.println("hello");
        }
    //  System.out.println("x:"+x);

        System.out.println(".................");

        int y = 1;
        while(y<=5){
            System.out.println("world");
            y++;
        }
        System.out.println("y:"+y);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章