js中while死循环语句_Java系列教程day06——循环语句
生活随笔
收集整理的這篇文章主要介紹了
js中while死循环语句_Java系列教程day06——循环语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
day06——循環語句
提綱:
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 <= 100 ){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 <= 10 );/*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 <= 10 ; 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;//+1sum += 2;//+1 +2sum += 3;//+1 +2 +3...sum += 10;//+1 +2 +3....+10*/int sum = 0;//定義變量sum,用于表示1-10的和,這個結果,初始值是0for(int i = 1; i<= 10; 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 <= 10 ;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 <= 10;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<= 10;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 <= 5;i++){//5次,i:1,2,3,4,5,控制行數的 ?for(int j = 1;j<= 5;j++){//控制*的個數System.out.print("*");} ?System.out.println();}} } /* ***** ***** ***** ***** ***** */ ?課堂練習:打印乘法表
class Loop2 {public static void main(String[] args) {/*打印9*9的乘法表1*1=12*1=2 2*2 =43*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 = 1j = 1i = 2j = 1,2i = 3j = 1,2,3i = 4j = 1,2,3,4..i = 9j = 1,2,3,4,5,6,7,8,9 ? ?*/ ?for(int i = 1;i < 10;i++){for(int j = 1;j<=i;j++){System.out.print(j+"*"+i+"="+i*j+"t");}System.out.println();}} } ?五、循環使用總結
1、學會找出代碼中運行的規律,完成對循環的代碼實現。數組,集合,算法。。。 2、時時刻刻記住:循環中的坑死循環,while,do-whilecontinue關鍵字,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 歲,分數:%.2fn",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、打印菱形
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的js中while死循环语句_Java系列教程day06——循环语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卡尺测量的最小范围_不知道这四大基本原则
- 下一篇: 充钱包是什么交易