0
Where and why we use ":" this one?pls tell me..
java
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
+ 2
Could you be a little more specific? The clearer your question, the better we can help
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);