How To Use For In Java

Tanggal : 15 Feb 2023, Category : Pemprograman Website, Create By : admin

In Java, the for loop has a slightly different syntax than in JavaScript. The for loop in Java can be used with an enhanced version called the "for-each" loop, which is designed to iterate over arrays and collections. Here's how to use the for and for-each loops in Java:

Using a for loop:

for (int i = 0; i < 10; i++) {
System.out.println(i);
}

In this code, the for loop runs 10 times, with the variable i taking on the values 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 in each iteration. The loop body prints the value of i to the console in each iteration.

Using a for-each loop:

int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}

In this code, the for-each loop runs for each element of the numbers array. The loop body prints the value of each element to the console in each iteration.

The syntax for the for-each loop is as follows:

for (datatype variable : collection) {
// Loop body
}

In this syntax, datatype is the type of the elements in the collection, variable is the variable that will hold each element of the collection in each iteration, and collection is the array or collection being iterated over.

The for-each loop is a more concise and readable way to iterate over arrays and collections in Java, and is especially useful when you don't need to keep track of the index of each element.


Keyword:
  • How To Use For In Java

Artikel Lain

Menampilkan Nama Hari Di Php

Date : 31 Aug 2018, Category : Tutorial, Create By : admin

Selamat datang kembali di website gudang ilmu aplikasi, setalah kmaren kita membahas mengenai membuat format tanggal indonesia dengan php, kali ini gudang ilmu aplikasi akan membahas kelanjutan dari materi sebelumnya .....

Cara Login Ke User Root Menggunakan User Biasa Linux Debian

Date : 01 Aug 2019, Category : Server, Create By : admin

Selamat datang di wesbite gudang ilmu aplikasi, kali ini kita akan membasah mengenai bagaimana cara masuk ke user root menggunakan user biasa, seperti kita ketahui user dalam operating system linux .....

Looping Array Javascript

Date : 28 Dec 2018, Category : JavaScript, Create By : admin

Looping adalah sebuah metode perulangan dimana bisanya perulangan tersebut akan menghasilakan sebuah data berurutan, misalkan looping angka numerik 1 sampai dengan 10 maka looping akan mencetak 1 2 3 4 .....