1. 程式人生 > 其它 >Java學習筆記5-流程控制

Java學習筆記5-流程控制

技術標籤:java

Java學習筆記5-流程控制

1、簡介

控制流程(也稱為流程控制)意指在程式執行時,個別的指令(或是陳述、子程式)執行或求值的順序。即:程式執行時,對程式碼執行的控制。

2、分類

  • 複合語句
  • 順序語句
  • 條件語句
  • 迴圈語句

3、實現

3.1、複合語句

Java語言的複合語句是以整個塊區為單位的語句,又稱塊語句。複合語句由“{”開始,閉括號“}”結束。如:類或方法定義時的類體方法體

複合語句為區域性變數建立了一個作用域,該作用域為程式的一部分,在該作用域中某個變數被建立並能夠被使用,如果在某個變數的作用域外使用該變數,則會發生錯誤。並且複合語句中可以巢狀複合語句。
3.2、順序語句

按照程式碼的寫作順序,從上到下依次執行

public class Hello {

	public static void main(String[] args) {
		String s = "hello world,你好,世界!";
		String x = "1+2=";
		String str1 = "Hello         ";
		String str2 = "World";
		System.out.println(s);
		System.out.println(x + (1 + 2));
		System.
out.println(str1); System.out.println(str2); } }

在這裡插入圖片描述

3.3、條件語句
(1)if
語法:if(條件){語句 }

public class Contiue {
	public static void main(String[] args) {
	
		int i = 10;
		int m = 0;
		if (i < m) {
			System.out.print("m大於i!!");
		}
		if (m < i) {
			System.out.print("i大於m!!");
		}
} }

(2)if…else…
語法:if(表示式){
若干語句
} else{若干語句
}

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

		int a = 11;
		if (a > 0) {
			System.out.println("a的為正整數");
		}else {
			System.out.println("a的為不為正整數");
		}
		
	}
}

(3)if…else if …else if…
語法:if(表示式1){
語句序列1
}else if(表示式2){
語句序列2}
,,,
else if(表示式n){
語句序列n
}

public class Contiue1 {

	public static void main(String[] args) {

		int x = 59;
		if (x > 60) {
			System.out.print("你考試及格了");
		} else if (x > 90) {
			System.out.print("你真棒!!");
		} else {
			System.out.print("繼續努力!!");
		}
	}
}

(4)switch多分支語句
語法:switch(表示式)
{ case 常量1:
語句塊1
[break;]

case 常量n:
語句塊n
[break;]
default:
語句塊 n+1;
[break;]
}

public class Contiue4 {

	public static void main(String[] args) {

		String a = "天晴天陰下雪下雨";
		switch ("天") {
		case "天晴":
			System.out.println("今天天氣不錯!心情也舒暢了不少");
			break;
		case "天陰":
			System.out.println("今天天陰了!心情還好吧");
			break;
		case "下雪":
			System.out.println("今天下雪了!心也有點冷");
			break;
		case "下雨":
			System.out.println("今天下雨了!心情不好啊");
			break;
		default:
			System.out.println("心情糟糕透了");
			break;
		}
		
	}
}

3.4、迴圈語句
(1)while迴圈語句
語法:while(條件表示式)
{執行語句
}

//1+2+3+....+10=
public class Contiue2 {

	public static void main(String[] args) {

		int i = 10;
		int m = 0;
		while (i > 0) {
			m = m + i;
			i--;
		}
		System.out.print("m=" + m);
		
	}
}

(2)do…while 迴圈語句(“{}”中程式至少被執行一次)
語法:do{
執行語句
}
while(條件表示式)

public class Contiue2 {

	public static void main(String[] args) {

		int x = 60;
		do {
			System.out.println("x=" + x);
			x--;
		} while (x == 40);
		
	}
}

(3)for語句
語法:for(表示式1;表示式2;表示式3)
{
語句序列
}

public class Contiue2 {

	public static void main(String[] args) {

		// for迴圈
		int sum = 0;
		for (int i = 1; i <= 100; i += 2) {
			sum = sum + i;
		}
		System.out.print("輸出1到100之間奇數之和:" + sum + "\n");
		
	}
}

(4)foreach 語句(for語句的特殊簡化版本,不是關鍵字,常用於遍歷陣列)
語法:for(元素變數x:遍歷物件 obj){
引用x的語句
}

public class Contiue2 {

	public static void main(String[] args) {

		// foreach迴圈
		int a[] = { 1, 2, 3, 4, 45, 5, 6, 6, 7 };
		System.out.println("一維陣列元素為:");
		for (int x : a) {
			// foreach中x指引用的變數,a要迴圈的陣列,最後輸出x
			System.out.println(x);
		}
		
	}
}

(5)迴圈控制break和continue

  • break語句

    作用:跳出當前迴圈體,中斷當前迴圈。

  • continue語句

    作用:跳過本次迴圈繼續執行下次迴圈。

public class Contiue3 {

	public static void main(String[] args) {

		// break
		int a = 1;
		while (a > 0) {
			a = a + 2;
			System.out.println(a);
			if (a == 79) {
				break;
			}
		}
		System.out.println("---end----" + "\n");


		// continue
		for (int a1 = 2; a1 < 100; a1 += 2) {
			if (a1 % 5 == 0) {
				continue;
			}
			System.out.println(a1);
		}
		System.out.println("----over-----");
	
	}
}