Làm thế nào để chúng ta sử dụng vòng lặp do while trong Java?

Câu lệnh

do {
     statement[s]
} while [expression];
4 liên tục thực hiện một khối câu lệnh trong khi một điều kiện cụ thể là
do {
     statement[s]
} while [expression];
5. Cú pháp của nó có thể được thể hiện như

while [expression] {
     statement[s]
}

Câu lệnh

do {
     statement[s]
} while [expression];
4 đánh giá biểu thức, biểu thức này phải trả về giá trị
do {
     statement[s]
} while [expression];
7. Nếu biểu thức đánh giá là
do {
     statement[s]
} while [expression];
5, thì câu lệnh
do {
     statement[s]
} while [expression];
4 sẽ thực thi [các] câu lệnh trong khối
do {
     statement[s]
} while [expression];
4. Câu lệnh
do {
     statement[s]
} while [expression];
4 tiếp tục kiểm tra biểu thức và thực thi khối của nó cho đến khi biểu thức ước tính thành
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
3. Sử dụng câu lệnh
do {
     statement[s]
} while [expression];
4 để in các giá trị từ 1 đến 10 có thể được thực hiện như trong chương trình
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
5 sau

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}

Bạn có thể triển khai vòng lặp vô hạn bằng cách sử dụng câu lệnh

do {
     statement[s]
} while [expression];
4 như sau

while [true]{
    // your code goes here
}

Ngôn ngữ lập trình Java cũng cung cấp một câu lệnh

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
7, có thể diễn đạt như sau

do {
     statement[s]
} while [expression];

Sự khác biệt giữa

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
7 và
do {
     statement[s]
} while [expression];
4 là
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
7 đánh giá biểu thức của nó ở dưới cùng của vòng lặp thay vì trên cùng. Do đó, các câu lệnh trong khối
while [true]{
    // your code goes here
}
1 luôn được thực hiện ít nhất một lần, như trong chương trình
while [true]{
    // your code goes here
}
2 sau đây

Trong hướng dẫn trước, chúng ta đã thảo luận về vòng lặp while. Trong hướng dẫn này, chúng ta sẽ thảo luận về vòng lặp do-while trong java. Vòng lặp do-while tương tự như vòng lặp while, tuy nhiên có sự khác biệt giữa chúng. Trong vòng lặp while, điều kiện được ước tính trước khi thực hiện phần thân của vòng lặp nhưng trong vòng lặp do-while, điều kiện được ước tính sau khi thực hiện phần thân của vòng lặp

Cú pháp của vòng lặp do-while

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
1

Vòng lặp do-while hoạt động như thế nào?

Đầu tiên, các câu lệnh bên trong vòng lặp thực thi và sau đó điều kiện được đánh giá, nếu điều kiện trả về đúng thì điều khiển sẽ được chuyển sang lệnh “do”, nếu không nó sẽ chuyển sang câu lệnh tiếp theo sau do-while

ví dụ về vòng lặp do-while

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
2

đầu ra

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
3

Thí dụ. Lặp lại mảng bằng vòng lặp do-while

Ở đây chúng tôi có một mảng số nguyên và chúng tôi đang lặp lại mảng và hiển thị từng phần tử bằng cách sử dụng vòng lặp do-while

Các vòng lặp trong Java được sử dụng khi chúng ta cần thực hiện lặp đi lặp lại một khối câu lệnh. Vòng lặp do-while trong Java là một vòng lặp điều khiển Thoát. Do đó, không giống như vòng lặp for hoặc while, do-while kiểm tra điều kiện sau khi thực hiện các câu lệnh của thân vòng lặp

cú pháp.  

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
4

Ghi chú. Test_expression cho vòng lặp do-while phải trả về giá trị boolean, nếu không chúng ta sẽ gặp lỗi thời gian biên dịch

Ứng dụng của do-while. Ứng dụng ví dụ của nó đang hiển thị một số loại menu cho người dùng.  

Ví dụ.  

Bạn đang triển khai một trò chơi trong đó bạn hiển thị một số tùy chọn cho người dùng, nhấn 1 để thực hiện việc này. , nhấn phím 2 để làm điều này. v.v. và nhấn 'Q' để thoát khỏi trò chơi. Vì vậy, ở đây bạn muốn hiển thị menu trò chơi cho người dùng ít nhất một lần, vì vậy bạn viết mã cho menu trò chơi bên trong vòng lặp do-while

Hình minh họa

Java




class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
5

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
6

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
7

 

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
8

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
50
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51

 

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
52
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
53

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
52
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
55
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
56
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
57
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
58

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
52
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
50

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51____252

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
54
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
55____256
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
57

 

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
59
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
50

 

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
61
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
62

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
61
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
64

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
61
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
66______367
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
68

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
61
while [true]{
    // your code goes here
}
50

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51____452

 

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51____454

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51
while [true]{
    // your code goes here
}
56

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51____458

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
50
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
51
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
56
class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
68

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
52
while [true]{
    // your code goes here
}
52

while [true]{
    // your code goes here
}
52

Đầu ra

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
5

Giải thích đầu ra

Trong đoạn mã trên, chúng tôi đã phát hiện ra rằng điều kiện được kiểm tra sau vì phần thân bên trong do sẽ được thực thi một lần mà không bị lỗi vì điều kiện được kiểm tra sau đó trở đi. Do đó, bất cứ khi nào chúng tôi muốn hiển thị menu và sau đó tiếp tục lệnh trên thiết bị đầu cuối, chúng tôi luôn sử dụng vòng lặp do-while

Các thành phần của vòng lặp do-while

A. biểu thức kiểm tra. Trong biểu thức này, chúng ta phải kiểm tra điều kiện. Nếu điều kiện có giá trị true thì chúng ta sẽ thực hiện phần thân của vòng lặp và đi đến cập nhật biểu thức. Nếu không, chúng ta sẽ thoát khỏi vòng lặp while. Ví dụ.  

class WhileDemo {
    public static void main[String[] args]{
        int count = 1;
        while [count < 11] {
            System.out.println["Count is: " + count];
            count++;
        }
    }
}
5

B. Cập nhật biểu thức. Sau khi thực hiện thân vòng lặp, biểu thức này tăng/giảm biến vòng lặp một giá trị nào đó. Ví dụ.   

Chủ Đề