提綱:
1、循環結構
2、while循環
3、do-while循環
4、for循環
5、break語句
6、continue語句
7、循環嵌套
8、作業
一、循環結構1.1 概念
條件滿足,某些代碼會被反覆多次的執行。條件不成立了,循環結束。0-n次。
1.2 為什麼使用循環
開發中可能會把某些代碼需要執行多次,如果使用CV大法,CV戰士,治標不治本。會出現以下問題
1、代碼過於臃腫!
2、代碼閱讀性極差!
3、代碼維護性極差!
循環的組成部分
1、初始化部分:對循環變量進行初始賦值。
2、循環條件部分:判斷循環變量是否滿足循環條件。
3、循環體部分:要循環執行的具體的代碼。
4、更新循環變量部分:修改循環變量的值。二、循環語句2.1、while循環
while循環的語法結果:
while( 循環條件判斷 ){
//循環體 //(循環中變量的變化)}/*執行流程: 首先判斷while之後的小括號裡的循環條件的值:boolean--->true,false 如果是true,表示循環條件成立,那麼執行{}裡的內容,然後再來判斷條件 如果是false,表示循環條件不成立,那麼循環結束*//*注意事項: 1、學會循環過程推理,避免死循環 2、如果出現死循環,ctrl+c 終止程序*///while循環的特點:先判斷條件,再執行代碼。
while循環的流程圖:
示例代碼:
class Test2While {
public static void main(String[] args)
{
/* while( boolean類型的表達式 ){ } 示例: while( 活著 ){ 心臟跳一下 } */
//使用while循環,來列印100遍的helloworld
//初始化一個變量,表示列印helloworld的次數 int i = 1; //1,2,3....100
while( i
System.out.println("Hello World!");
//i++; ++i;
}
System.out.println(i);
}}
課堂練習:
public class Test3While {
public static void main(String[] args)
{
//課堂練習:使用while循環,列印10-1數字。
int i = 10;
while(i > 0){
System.out.println(i);
i--;
}
System.out.println("mian..over...");
/* System.out.println(10); System.out.println(9); System.out.println(8); System.out.println(7); ... System.out.println(1); */
}}
2.2、do-while循環
do-while循環的語法結構:
do{
//循環體 //(循環變量的變化)}while( 循環條件 );/*執行流程: 首先執行do後{}之間的內容,然後再判斷while裡的循環條件。 如果條件為true,循環就繼續執行。 如果條件為false,循環終止!*/
do-while的執行流程圖
示例代碼:
public class Test5DoWhile {
public static void main(String[] args)
{
/* do{ //循環體 //(循環變量變化) }while( 循環的條件 ); */
//使用do-while循環,來列印1-10這個數字
int i = 1;
do{
//循環體 System.out.println(i);
//(循環變量變化) i++;
}while( i
/* System.out.println(1); System.out.println(2); System.out.println(3); //... System.out.println(10); */
System.out.println("i--->" + i);
}}
對比while和do-while循環
while循環,先判斷循環的條件,然後根據條件執行裡面的循環體。一句話:先判斷,再執行。do-while循環,先執行一遍循環體,然後再來判斷條件。一句話:先執行,再判斷。
2.3、for循環
for循環的語法結構:
for(表達式1:循環變量的初始化 ; 表達式2:循環的條件 ; 表達式3:循環變量的變化 ){
//循環體;}/*執行流程:首先執行表達式1:只執行1次。慣用於初始化循環變量。然後執行表達式2:循環的判斷條件:boolean-->true,false如果為true,執行循環體;然後再執行表達式3:變量的變化然後再判斷條件是否成立,如果成立,就繼續否則條件不成立,就結束整個循環*//*for循環的優勢 1、for循環的語法結構很清晰。 2、for循環,很方便的推算出循環的次數。*/
for執行流程
示例代碼:
class Test7For {
public static void main(String[] args)
{
/* for(表達式1:循環變量的初始化 ; 表達式2:循環的條件 ; 表達式3:循環變量的變化 ){ //循環體; } */
//列印10遍"喝粥"
for(int i = 1 ; i
//循環體; System.out.println("喝粥..." + i);//i:1,2,3...10 }
//System.out.println(i);//此處不能列印i,因為i在for循環裡定義的,超出了作用域。
System.out.println("Hello World!");
}}
課堂練習:
public class Test8For {
public static void main(String[] args)
{
//課堂練習:使用for循環,求1-10的和 /* 分析過程: sum = 0; sum += 1;//+1 sum += 2;//+1 +2 sum += 3;//+1 +2 +3 ... sum += 10;//+1 +2 +3....+10 */
int sum = 0;//定義變量sum,用於表示1-10的和,這個結果,初始值是0 for(int i = 1; i
sum += i;//i:1,2,3...10 }
System.out.println(sum);
}}
for循環的特殊形式:了解性的內容
1、表達式2如果省略,表示循環永真。
循環條件默認是true--->成立2、表達式3:本來是跟在循環體後面執行的。
但是不是很建議3、如果表達式1、3都省略,只剩表達式2-->相當於
while(循環條件){
}4、如果表達式1,2,3都省略:for(;;){}--->相當於
while(true){
}
示例代碼:
public class Test9For {
public static void main(String[] args)
{
/* 標準的for循環 for(表達式1;表達式2;表達式3;){ 循環體; } for循環特殊形式:了解 1、表達式2如果省略,表示循環永真。 循環條件默認是true--->成立 2、表達式3:本來是跟在循環體後面執行的。 但是不是很建議 3、如果表達式1、3都省略,只剩表達式2-->相當於 while(循環條件){ } 4、如果表達式1,2,3都省略:for(;;){}--->相當於 while(true){ } */
for(int i = 1;i
System.out.println(i);
}
}}
2.4、幾種循環的比較
1、對於同一個問題,三種循環可以互相替代。
2、循環次數確定的情況下,優先選擇for循環,循環次數不固定的建議while,do-while循環。
3、要防止無限循環--->死循環
三、循環的流程控制語句3.1、break語句
break關鍵字
break:詞義:打破,打碎,破壞
用法一:switch-case語句中,break用於防止switch穿透。
用法二:循環語句:while,do-while,for。強制結束了循環語句,無論循環條件是否滿足。
示例代碼:
class Test10Break {
public static void main(String[] args)
{
/* 循環停止:循環條件不成立,循環就停止了。 break語句:控制循環結構。 break的用法:詞義:打破,打碎, 用法一:switch-case語句中。用於結束case分支,結束switch語句。 用法二:針對於循環語句,一個循環中,一旦執行了break語句,那麼該循環就徹底的結束了 無論循環條件是否滿足。 */
for(int i= 1;i
if( i == 5){
break;//強制的結束循環。 }
System.out.println(i);
}
System.out.println("main..over...");
}}
3.2、continue語句
continue關鍵字
continue:詞義:繼續
只能在循環中使用,專門用於控制循環。
用法:結束當前這次循環,循環下次會繼續執行。
注意點:continue的大坑,
在while,do-while循環中使用,注意continue關鍵字和循環變量的變化的位置。防止出現死循環這種情況。
for循環中就沒有這種顧慮。
示例代碼:
public class Test2Continue {
public static void main(String[] args)
{
/* continue:結束了當前這次循環。循環下次繼續。 */
for(int i = 1;i
if( i == 5){
continue;//詞義:繼續,結束當前這次循環,下次繼續。 }
System.out.println(i);
}
System.out.println("main...over...");
int j = 0;
while(j < 10){
if( j == 5){
continue;
}
j++;
System.out.println("j-->"+j);
}
System.out.println("main...over........");
}}
四、循環嵌套
什麼是多重循環?
多種循環,指的就是循環的嵌套。
特點:外層循環執行1次。內存循環要完整的執行一遍。
戒驕戒躁:代碼量。
示例代碼:
public class Test3Loop {
public static void main(String[] args)
{
for(int i = 1;i
for(int j = 1;j
}
System.out.println();
}
}}/***************************/
課堂練習:列印乘法表
class Loop2 {
public static void main(String[] args)
{
/* 列印9*9的乘法表 1*1=1 2*1=2 2*2 =4 3*1=3 3*2=6 3*3=9 ... 9*1=9 9*2=18 9*3=27 9*4=36...9*9=81 分析: 列印 i * j i = 1 j = 1 i = 2 j = 1,2 i = 3 j = 1,2,3 i = 4 j = 1,2,3,4 .. i = 9 j = 1,2,3,4,5,6,7,8,9 */
for(int i = 1;i < 10;i++){
for(int j = 1;j
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}}
五、循環使用總結
1、學會找出代碼中運行的規律,完成對循環的代碼實現。
數組,集合,算法。。。
2、時時刻刻記住:循環中的坑
死循環,while,do-while
continue關鍵字,while,do-while
3、學會循環變量的值推理過程:
循環變量的初始值,結束值,循環次數,變量怎麼變化(變量的步長)
4、學會使用循環嵌套
一點一點的分析,推理
每一層循環,每一個變量都是有意義上的。。
擴展幾種列印方式
System.out.println();
1.println();//列印後換行:print+line
2.print();//就列印,不換行
3.printf(" 佔位符 \n",類型的變量);//格式化列印:print+format
%d,整數佔位符
%s,字符串佔位符
%f,浮點佔位符
%.2f,%.3f
%c,字符佔位符
示例代碼:
class Test4Print {
public static void main(String[] args)
{
/* 列印:System.out.xxxx() 1、println();print + line ,列印後換行 2、print();只是列印,不換行 3、擴展內容: printf();print+format ,格式化列印 */
System.out.println("Hello World!");//print + line ,列印後換行 System.out.println("hahahahah");
System.out.println();//只是換行 System.out.println("王二狗");
System.out.print("李小花");
System.out.print("白骨精");
System.out.println();
String name = "小鑽風";
int age = 100;
double score = 88.7;
System.out.printf("姓名:%s,年齡:%d 歲,分數:%.2f\n",name,age,score);//佔位符,使用一個符號,佔著這個地兒 System.out.println("main..over..");
}}
六、作業
1、"百錢買百雞"是我國古代的著名數學題。 題目描述:公雞5文錢1隻,母雞3文錢1隻,小雞3隻1文錢,100文錢剛好買100隻雞,怎麼買?
2、求水仙花數。所謂水仙花數,是指一個三位數, 每個位上數字的立方和(百位數字的立方+十位數字的立方+個位數字的立方), 剛好是這個數字本身。 比如:153 1^3+5^3+3^3 ,剛好是153。所以153就是一個水仙花數。
3、列印2-100內所有的素數。 (素數,也叫質數,就是只能被1和本身整除的數,比如3,7,11,13等)
4、列印菱形