Java - for, while do...while
Java
- while
- do…while
- for
Java5 for
while
while
while( ) {
//
}
true
Test.java
public class Test {
public static void main(String[] args) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
dowhile
while
dowhile while dowhile
do {
//
}while();
true false
Test.java
public class Test {
public static void main(String[] args){
int x = 10;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
for
while do...while Java for
for
for(; ; ) {
//
}
for
- truefalse
Test.java
public class Test {
public static void main(String[] args) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
Java for
Java5 for
Java for :
for( : )
{
//
}
Test.java
public class Test {
public static void main(String[] args){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
10,20,30,40,50, James,Larry,Tom,Lacy,
break
break switch
break
break
break;
Test.java
public class Test {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
// x 30
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
10 20
continue
continue
for continue
while dowhile
continue
continue;
Test.java
public class Test {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
10 20 40 50
tianqixin
429***[email protected]
for
for(int i=1; i<=3; i++){ for(int n=1; n<=3; n++){ // System.out.println("i = " + i + ", n = " + n); } }for i, 1 i 3, i 1
for n, 1 n 3, n 1
tianqixin
429***[email protected]
Anna
lis***[email protected]
for
public class Test { public static void main(String args[]) { for(int i=1;i<=9;i++){ for(int j=1;j<=i;j++){ System.out.print(j+"*"+i+"="+i*j+" "); } System.out.println(); } } }for i, 1 i 9, i 1
for j, 1 j i, j 1
Anna
lis***[email protected]
.hello
986***[email protected]
for
for
public class Test { public static void main(String[] args) { // int i,j,q; // for( i = 1;i < 6; i ++) // { // for( j = 5; j > i; j--) // { System.out.print(" "); } // for( q = 1;q < i*2; q++) //13579 { System.out.print("*"); } System.out.println(); // } // for(i = 1; i < 5; i++) // { // for(j = 1;j <= i; j++) { // System.out.print(" "); } // for( q = i*2; q < 9 ; q++) { //7531 System.out.print("*"); } System.out.println(); // } } }.hello
986***[email protected]
632***[email protected]
Java
0-99 10 ---- :
public class Test { public static void main(String []args) { for(int i = 0; i < 10; i++){ for(int j = 0; j < 10; j++){ System.out.print("" + i + j +" "); } System.out.println("\n -------------------------------------- \n"); } System.out.println(""); } }0-29:
29
public class Test { public static void main(String []args) { for(int i = 0; i < 10; i++){ for(int j = 0; j < 10; j++){ if(i * 10 + j > 29){ break; } System.out.print("" + i + j +" "); } System.out.println("\n -------------------------------------- \n"); } System.out.println(""); } }0-29, break
public class Test { public static void main(String []args) { lable: for(int i = 0; i < 10; i++){ for(int j = 0; j < 10; j++){ if(i * 10 + j > 29){ break lable; } System.out.print("" + i + j +" "); } System.out.println("\n -------------------------------------- \n"); } System.out.println(""); } }3 lable
7 break lable lable
632***[email protected]
190***[email protected]
10
public class tset { public static void main(String []args){ // int sum1=1; for(int i=9;i>=1;i--){ sum1=(sum1+1)*2; } System.out.println("sum1="+sum1); // int sum2=1; for (int i=1;i<=9;i++){ sum2=(sum2+1)*2; } System.out.println("sum2="+sum2); } }190***[email protected]