Where and why we use ":" this one?pls tell me.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Where and why we use ":" this one?pls tell me..

java

28th Apr 2018, 11:46 AM
kumar
3 Answers
+ 3
Ah okay... That notation is called an "enhanced for loop". It requires less writing and less lines than the regular loop and it is used to traverse and get elements of an array... Basically you have: for (variableName : array) //do something with the variable So it takes each element of the array and puts it in the variable Look at the code below. The two loops do the same thing but the enhanced loop makes things simpler https://code.sololearn.com/cOvUvAuVnZxZ/?ref=app
28th Apr 2018, 1:27 PM
cyk
cyk - avatar
+ 2
Could you be a little more specific? The clearer your question, the better we can help
28th Apr 2018, 12:43 PM
cyk
cyk - avatar
0
public class Program { public static void main(String[] args) { int[ ] primes = {2, 3, 5, 7}; for (int t: primes) { /*just like here , why they use ":"? */ System.out.println(t);
28th Apr 2018, 1:19 PM
kumar