package com.itheima_06;
/*
* do...while循環(huán)的基本格式:
* do {
* 循環(huán)體語句;
* }while(判斷條件語句);
* 擴展格式:
* 初始化語句;
* do {
* 循環(huán)體語句;
* 控制條件語句;
* }while(判斷條件語句);
* 執(zhí)行流程:
* A:執(zhí)行初始化語句;
* B:執(zhí)行循環(huán)體語句;
* C:執(zhí)行控制條件語句;
* D:執(zhí)行判斷條件語句,看是true還是false
* 如果是true,回到B繼續(xù)
* 如果是false晒屎,就結(jié)束
*
* 練習(xí):
* 求和案例
* 統(tǒng)計水仙花個數(shù)
*/
public class DoWhileDemo {
public static void main(String[] args) {
//輸出10次 HelloWorld
/*
for(int x=1; x<=10; x++) {
System.out.println("HelloWorld");
}
*/
//do...while改寫
int x=1;
do {
System.out.println("HelloWorld");
x++;
}while(x<=10);
}
}
package com.itheima_06;
/*
* 三種循環(huán)的區(qū)別:
* A:do...while至少執(zhí)行一次循環(huán)體
* B:for,while循環(huán)先判斷條件是否成立灯帮,然后決定是否執(zhí)行循環(huán)體
*
* for和while的小區(qū)別:
* for循環(huán)的初始化變量苏携,在循環(huán)結(jié)束后鉴分,不可以被訪問。而while循環(huán)的初始化變量贬丛,是可以被繼續(xù)使用的。
* 如果初始化變量给涕,后面還要繼續(xù)訪問豺憔,就使用while,否則够庙,推薦使用for恭应。
*
* 循環(huán)的使用推薦:
* for -- while -- do...while
*/
public class DoWhileDemo2 {
public static void main(String[] args) {
/*
int x = 3;
while(x<3) {
System.out.println("我愛林青霞");
x++;
}
System.out.println("--------------");
int y = 3;
do {
System.out.println("我愛林青霞");
y++;
}while(y<3);
*/
for(int x=1; x<=10; x++){
System.out.println("愛生活,愛Java");
}
//這里的x無法繼續(xù)訪問
//System.out.println(x);
System.out.println("-----------------");
int y = 1;
while(y<=10) {
System.out.println("愛生活耘眨,愛Java");
y++;
}
System.out.println(y);
}
}