Sabtu, 23 Oktober 2010

JavaScript For Loop Explained JavaScript Untuk Loop Dijelaskan

There are four important aspects of a JavaScript for loop : Ada empat aspek penting dari JavaScript untuk loop:
  1. The counter variable is something that is created and usually used only in the for loop to count how many times the for loop has looped . Variabel counter adalah sesuatu yang diciptakan dan biasanya digunakan hanya dalam for loop untuk menghitung berapa kali untuk loop telah dilingkarkan. i is the normal label for this counter variable and what we will be using. i adalah label normal untuk variabel counter dan apa yang akan kita gunakan.
  2. The conditional statement. Pernyataan bersyarat. It is what decides whether the for loop continues executing or not. Ini adalah apa yang memutuskan apakah untuk loop terus melaksanakan atau tidak. This check usually includes the counter variable in some way. Pemeriksaan ini biasanya mencakup variabel counter dalam beberapa cara.
  3. The counter variable is incremented after every loop in the increment section of the for loop . Variabel counter bertambah setelah setiap loop di bagian peningkatan untuk loop.
  4. The code that is executed for each loop through the for loop . Kode yang dieksekusi untuk setiap loop melalui untuk loop.
This may seem strange, but 1-3 all occur on the same line of code. Hal ini mungkin aneh, tapi 1-3 semua terjadi pada baris yang sama kode. This is because the for loop is such a standardized programming practice that the designers felt they might as well save some space and clutter when creating the for loop . Hal ini karena untuk loop adalah suatu praktek pemrograman standar bahwa desainer merasa bahwa mereka mungkin juga menghemat ruang dan kekacauan saat membuat untuk loop.




JavaScript For Loop Example JavaScript Untuk Contoh Loop

This example will show you how to create a simple for loop that prints out the value of our counter until the counter reaches 5. Contoh ini akan menunjukkan cara membuat sederhana untuk loop yang mencetak nilai kontra kita sampai counter mencapai 5. Pay special close attention to the three different items that are on the first line of the for loop code. Bayar perhatian khusus pada tiga item yang berbeda yang terdapat pada baris pertama dari kode loop. These are the important for loop parts 1-3 that we talked about earlier. Ini adalah penting untuk bagian loop 1-3 yang kita bicarakan sebelumnya.

JavaScript Code: JavaScript Kode:
<script type="text/javascript"> <script type="text/javascript">
<!-- <! -
var linebreak = "<br />"; var LINEBREAK "/> <br" =;
document.write("For loop code is beginning"); document.write ("Untuk kode loop awal");
document.write(linebreak); document.write (LINEBREAK);

for(i = 0; i < 5; i++) { untuk (i = 0; + i +; i <5) {
         document.write("Counter i = " + i); document.write ("Counter i =" + i);
        document.write(linebreak); document.write (LINEBREAK);
} }

document.write("For loop code is finished!"); document.write ("Untuk kode loop selesai!");
</script> </ Script>

Display: Tampilan:
For loop code is beginning
Counter i = 0
Counter i = 1
Counter i = 2
Counter i = 3
Counter i = 4
For loop code is finished!
The counter variable name i may seem a little strange, but it has been used for years now! Nama variabel i counter mungkin terlihat agak aneh, tetapi telah digunakan selama bertahun-tahun sekarang! No matter the language, i is the default name for a loop counter. Tidak peduli bahasa, i adalah nama standar untuk loop counter. Other common variable names are j, k, x, y and z . Lain nama variabel umum adalah j, k, x, y dan z.
In this example, our counter was initially set to 0 with "i = 0;" and then the conditional statement "i < 5;" was executed. Dalam contoh ini, counter kami pada awalnya diatur ke 0 dengan "i = 0;" dan kemudian pernyataan kondisional "i <5;" dieksekusi. Our counter was indeed smaller than 5, so the for loop 's code was executed. counter kami memang lebih kecil dari 5, sehingga untuk s kode 'loop dieksekusi.
After the loop's code is executed then the increment "i++" happens, making the counter i equal to 1. Setelah loop's kode dijalankan maka selisih "i + +" terjadi, membuat i counter sama dengan 1. The for loop then checked that i was less than 5, which it was, causing the for loop's code to be executed again. Untuk loop kemudian diperiksa bahwa saya kurang dari 5, yang itu, menyebabkan untuk itu kode loop akan dieksekusi lagi.
This looping happened a couple more times until i was equal to 5, which is not less than 5, causing the for loop 's code to stop executing. perulangan ini terjadi lebih beberapa kali sampai saya adalah sama dengan 5, yang tidak kurang dari 5, menyebabkan untuk s kode 'loop untuk menghentikan mengeksekusi.

source: google.com,  



1 komentar:

Anonim mengatakan...

mestinya bukan contoh java script, tetapi java biasa untuk aplikasi console krn ada bbrpa syntax yg beda :)

Posting Komentar