在一個程序執行的過程中,各條語句的執行順序對程序的結果是有直接影響的。也就是說,程序的流程對運行結果有直接的影響。所以,我們必須清楚每條語句的執行流程。而且,很多時候我們要通過控制語句的執行順序來實現我們要完成的功能。
一、判斷語句
1 、if
if(關係表達式){
語句體;
}
public static void main(String[] args){
int a = 10;
int b = 20;
//變量使用if判斷
if (a == b){
System.out.println("a等於b");
}
System.out.println("結束");
}
2、if…else
- if語句第二種格式:
if(關係表達式) {
語句體1;
}else {
語句體2;
}
public static void main(String[] args){
int a = 1;
//變量使用if判斷
if(a % 2 == 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;
} else {
執行語句n+1;
}
public static void main(String[] args) {
int score = 100;
if(score>=90 && score<=100){
System.out.println("你的成績屬於優秀");
}else if(score>=60 && score<90){
System.out.println("你的成績屬於合格");
}else if(score>=0 && score<60){
System.out.println("你的成績屬於不合格");
}else {
System.out.println("成績數據有誤");
}
}
二、選擇語句
1、switch
- switch語句格式:
switch(表達式) {
case 常量值1:
語句體1;
break;
case 常量值2:
語句體2;
break;
...
default:
語句體n+1;
break;
}
public static void main(String[] args) {
int month = 2;
//switch語句實現選擇
switch(month) {
case 1:
System.out.println("冬季");
break;
case 2:
System.out.println("冬季");
break;
default:
System.out.println("你輸入的月份數字有誤");
break;
}
}
switch語句中,表達式的數據類型,可以是byte,short,int,char,enum(枚舉),JDK7後可以接收字符串。
2、case的穿透性
在switch語句中,如果case的後面不寫break,將出現穿透現象,也就是不會在判斷下一個case的值,直接向後運行,直到遇到break,或者整體switch結束。
public static void main(String[] args) {
int month = 2;
//switch語句實現選擇
switch(month) {
case 1:
System.out.println("冬季");
case 2:
System.out.println("冬季");
break;
default:
System.out.println("你輸入的月份數字有誤");
break;
}
}
三、循環語句
1、概述
循環語句可以在滿足循環條件的情況下,反覆執行某一段代碼,這段被重複執行的代碼被稱爲循環體語句,當反覆執行這個循環體時,需要在合適的時候把循環判斷條件修改爲false,從而結束循環,否則循環將一直執行下去,形成死循環。
2、for
- for循環語句格式:
for(初始化表達式①; 布爾表達式②; 步進表達式④){
循環體③
}
public static void main(String[] args) {
for(int x = 0; x < 10; i++) {
System.out.println("HelloWorld"+i);
}
}
3、while
- while循環語句格式:
初始化表達式①
while(布爾表達式②){
循環體③
步進表達式④
}
while循環計算1-100之間的和
public static void main(String[] args) {
//定義一個變量,記錄累加求和
int sum = 0;
//定義初始化表達式
int i = 1;
//使用while循環讓初始化表達式的值變化
while(i<=100){
sum += i ;
i++;
}
//打印求和的變量
System.out.println("1-100的和是:"+sum);
}
4、do…while
- do…while循環格式
初始化表達式①
do{
循環體③
步進表達式④
}while(布爾表達式②);
do…while循環的特點: 無條件執行一次循環體,即使我們將循環條件直接寫成false,也依然會循環一次。這樣的循環具有一定的風險性。
public static void main(String[] args){
do{
System.out.println("無條件執行一次");
}while(false);
}
5、for 和 while的區別
for
和 while
的小區別:
控制條件語句所控制的那個變量,在for循環結束後,就不能再被訪問到了,而while循環結束還可以繼續使用,如果你想繼續使用,就用while,否則推薦使用for。原因是for循環結束,該變量就從內存中消失,能夠提高內存的使用效率。
6、break和continue區別
break
- 使用場景:終止switch或者循環
- 在選擇結構switch語句中
- 在循環語句中
- 離開使用場景的存在是沒有意義的
continue
- 使用場景:結束本次循環,繼續下一次的循環
四、擴展知識點
1、死循環
- 死循環: 也就是循環中的條件永遠爲true,死循環是永不結束的循環。
public static void main(String[] args) {
while (true) {
System.out.println("HelloWorld");
}
}
2、嵌套循環
-
所謂嵌套循環,是指一個循環的循環體是另一個循環。比如for循環裏面還有一個for循環,就是嵌套循環。總共的循環次數=外循環次數*內循環次數
-
嵌套循環格式:
for(初始化表達式①; 循環條件②; 步進表達式⑦) {
for(初始化表達式③; 循環條件④; 步進表達式⑥) {
執行語句⑤;
}
}