Wednesday, October 26, 2016

Bà i 8 Cấu trúc lặp while do while for trong Java!

Bà i 8 Cấu trúc lặp while do while for trong Java!


Bài 8: C?u trúc l?p while, do while, for trong Java!

D?ng 1: while(…)

while(?i?u_ki?n_l?p){
kh?i_l?nh;
}

Xét ?i?u ki?n tr??c, ?úng r?i m?i th?c hi?n kh?i l?nh. Vd:?
PHP:
public class AndroidVn {

    public static 
void main(String[] args) {
        
int i;
        
0;
        while (
10) {
            
System.out.print(" "+i++);
        }
        
System.out.println("...i = "+i);

        
15;
        while (
10) {
            
System.out.print(" "+i++);
        }
        
System.out.println("...i = "+i);
    }
}

D?ng 2: do{…}while;

do{
kh?i_l?nh;

}while(?i?u_ki?n);


Th?c hi?n kh?i l?nh tr??c, r?i xét ?i?u ki?n, n?u sai thì không th?c hi?n n?a. Nh? v?y, ngay c? ?i?u ki?n sai t? l?n ??u, t? kh?i l?nh luôn ???c th?c hi?n ít nh?t 1 l?n. Vd:?
PHP:
public class AndroidVn {

    public static 
void main(String[] args) {
        
int i 0;
        do {
            
System.out.print(" " i++);
        } while (
10);
        
System.out.println("...i = " i);

        
15;
        do {
            
System.out.print(" " i++);
        } while (
10);
        
System.out.println("...i = " i);
    }
}

D?ng 3: for(…)

for(kh?i_t?o_bi?n_??m;?k_l?p;t?ng_bi?n){
<kh?i_l?nh>;

}

Vd:?
PHP:
public class AndroidVn {

    public static 
void main(String[] args) {
        
int i;
        for (
010i++) {
            
System.out.print(" " i);
        }
        
System.out.println(" ..i = " i);
    }
}
?? hi?u h?n các c?u trúc, các b?n tham kh?o thêm
Video c?a Blog StudyAndShare






?
Ngu?n : Android.vn

Available link for download