day03-循环流程--三种循环的区别
This commit is contained in:
45
day03/src/com/inmind/dowhile05/Demo09.java
Normal file
45
day03/src/com/inmind/dowhile05/Demo09.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.dowhile05;
|
||||||
|
/*
|
||||||
|
三种循环的区别:
|
||||||
|
使用场景:
|
||||||
|
for:数组的遍历,集合的遍历,字符串的遍历
|
||||||
|
while:io流
|
||||||
|
do-while:当循环体必须执行一次的时候
|
||||||
|
|
||||||
|
不同之处:
|
||||||
|
当循环变量,直接不符合循环条件时,do-while一定会执行一次
|
||||||
|
for循环的循环变量,超出for的范围不能使用,就像没定义过一样
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//for循环
|
||||||
|
for(int i = 11;i<=10;i++){
|
||||||
|
System.out.println(i);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
System.out.println(i);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
System.out.println(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
//System.out.println(i);注意:此处的i超出了for循环范围,不能使用,没有定义过
|
||||||
|
System.out.println("-------------------------");
|
||||||
|
//while循环
|
||||||
|
int j = 11;
|
||||||
|
while(j<=10){
|
||||||
|
System.out.println(j);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
System.out.println(j);
|
||||||
|
|
||||||
|
System.out.println("-------------------------");
|
||||||
|
//do-while循环
|
||||||
|
int k = 11;//循环变量初始化①
|
||||||
|
do{
|
||||||
|
System.out.println(k);//循环体③
|
||||||
|
k++;//修改循环变量④
|
||||||
|
}while(k<=10);//循环条件②
|
||||||
|
System.out.println(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user