TAILIEUCHUNG - Ivor Horton’s Beginning Java 2, JDK 5 Edition phần 2

Giá trị của i được tăng lên bằng cách sử dụng các nhà điều hành tăng postfix. Nếu bạn sử dụng hình thức tiền tố, bạn sẽ có được kết quả sai. Lưu ý rằng dấu chấm phẩy sau khi các điều kiện trong khi hiện diện trong mỗi phiên bản của vòng lặp. Đây là một phần của báo cáo vòng lặp, do đó bạn không phải quên để đặt nó. | Loops and Logic do sum i Add the current value of i to sum while i limit Of course you can and should still put the braces in. I advise that you always use braces around the body of a loop even when it is only a single statement. There are often several ways of writing the code to produce a given result and this is true here you could also move the incrementing of the variable i back inside the loop and write it as follows do sum i Add the current value of i to sum while i limit The value of i is now incremented using the postfix increment operator. If you were to use the prefix form you would get the wrong result. Note that the semicolon after the while condition is present in each version of the loop. This is part of the loop statement so you must not forget to put it in. The primary reason for using this loop over the while loop would be if you want to be sure that the loop code always executes at least once. Nested Loops You can nest loops of any kind one inside another to any depth. Let s look at an example where you can use nested loops. A factorial of an integer n is the product of all the integers from 1 to n. It is written as n . It may seem a little strange if you haven t come across it before but the factorial of an integer is very useful for calculating combinations and permutations of things. For example n is the number of ways you can arrange n different things in sequence so a deck of cards can be arranged in 52 different sequences. Let s try calculating some factorial values. Try It Out Calculating Factorials This example will calculate the factorial of every integer from 1 up to a given limit. Enter the following code public class Factorial public static void main String args long limit 20L Calculate factorials of integers up to this value long factorial 1L A factorial will be stored in this variable Loop from 1 to the value of limit for long i 1L i limit i factorial 1L Initialize factorial for long factor 2 factor i factor factorial factor .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.