日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

循环结构, while, do……while

發布時間:2024/9/27 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 循环结构, while, do……while 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

循環結構

  • while循環
  • do……while循環
  • for 循環
  • 在Java5引入一種主要用于數組增強型for循環

while循環

  • while是最基本的循環,結構為:
while(布爾表達式){//循環內容 }
  • 只要布爾表達式為true,就會一直循環下去
  • 我們大多數情況會有循環停下來,需要一個表達式失效的方式來結束循環
  • 少部分需要循環一直執行,比如服務器的請求響應監聽等
  • 循環條件一直我true就會變成死循環,在正常業務編程要避免死循環。會影響程序性能或造成程序卡死崩潰
package com.boss.struct;public class WhileDemo02 {public static void main(String[] args) {//死循環while (true){//等待客戶端連接//定時檢查//。。。。。。。。}} }
  • 思考:計算1+2+3+……+100=?
package com.boss.struct;public class WhileDemo03 {public static void main(String[] args) {//計算1+2+3+……+100=?int i=0;int t=0;while (i<=100){t=t+i;i++;}System.out.println(t);} }
  • 輸出1-100
package com.boss.struct;public class WhileDemo01 {public static void main(String[] args) {//輸出1-100int i=0;while (i<100){i++;System.out.println(i);}} }

do……while循環

  • 對于while語句,如果不滿足條件,就不能進入循環。但是有的時候我們需要即使不滿足條件也至少執行一次。
  • do……while循環和while循環相似,不同的是,do……while循環至少執行一次
  • 語法
do{//代碼語句 }while(布爾表達式);
  • while和do……while的區別:

  • while先判斷后執行,do……while先執行再判斷!

  • do……while總是保證循環體至少會執行一次!這是他們的主要區別。

    package com.boss.struct;public class DowhileDemo02 {public static void main(String[] args) {int a=0;while (a<0){System.out.println(a);a++;}System.out.println("_____________________________________");do {System.out.println(a);a++;}while (a<0);} } D:\開發工具\java\bin\java.exe "-javaagent:D:\開發工具\IDEAIU\IntelliJ IDEA 2020.1\lib\idea_rt.jar=1093:D:\開發工具\IDEAIU\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\java學習\代碼\out\production\代碼 com.boss.struct.DowhileDemo02 _____________________________________ 0進程已結束,退出代碼 0

代碼

package com.boss.struct;public class DowhileDemo01 {public static void main(String[] args) {//計算1+2+3+……+100=?int i=0;int t=0;do{t=t+i;i++;}while (i<=100);System.out.println(t);} }

for循環

總結

以上是生活随笔為你收集整理的循环结构, while, do……while的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。