Files
javaSE251223/day03/src/com/inmind/Demo06.java

45 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind;
/*
三种循环的区别
使用场景for数组遍历字符串遍历集合遍历、while(io流)、do-while(当循环体必须执行一次的时候)
区别:
1.当循环条件都不满足时只有do-while一定会执行一次循环体语句
2.for循环的循环变量是定义在for语句内部的所以超出for循环以外的话就不能使用就像没定义过一样
3.while和do-while的循环变量可以一直使用
*/
public class Demo06 {
public static void main(String[] args)
{
//for
//打印1~10的值
for(int i = 1;i <= 10;i++){
System.out.println(i);
}
int i = 100;//证明for循环中的i与当前的i不在同一范围内
System.out.println(i);
System.out.println("-----------------------");
//while
//打印1~10的值
int j = 1;
while(j<=10){
System.out.println(j);
j++;
}//这里的大括号就表示结束了
System.out.println(j);
System.out.println("-----------------------");
//do-while
//打印1~10的值
int k = 1;
do{
System.out.println(k);
k++;
}while(k<=10);//错误: 需要';'{}表示结束,而()不行,必须要加;表示结束
System.out.println(k);
}
}