Several ways of writing loop in java

Several ways of writing loop in java

while

Grammar
while (conditional expression) { execute statement; }

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

do……while

Syntax:
do{ execution statement }while (conditional expression);

Note: do……while has an extra semicolon than while
. The difference between do while and while is that do while will be executed once regardless of whether the condition is established or not.

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

for

Syntax:
for (initial value expression; conditional expression; value-added expression) { execution statement; }

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

for each

Syntax:
for (variable x: traversal object) { execute statement; }

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

Guess you like

Origin blog.csdn.net/qq_36976201/article/details/112254477
Recommended