java中循环的几种写法

java中循环的几种写法

while

语法
while(条件表达式){
执行语句;
}

int a=1;
while(a<100){
    
    
	a++;
}

do……while

语法:
do{
执行语句
}while(条件表达式);

注意:do……while比while多了一个分号哦
do while与while的区别在于do while无论条件是否成立都会先执行一次

int a=1;
do{
    
    
	a++;
}while(a<100);

for

语法:
for(初值表达式;条件表达式;增值表达式){
执行语句;
}

for(int i=0;i<100;i++){
    
    
	System.out.println(i);
}

for each

语法:
for(变量x:遍历对象){
执行语句;
}

int arr[]={
    
    1,2,3,4,5}
for(int x:arr){
    
    
	System.out.println(x);
}

猜你喜欢

转载自blog.csdn.net/qq_36976201/article/details/112254477
今日推荐