Foreach Loop in Java:-
It is modified form of for loop which is basically used in Array and Collection Object.
foreach loop is used to display the elements of array.
for(datatype o:arrayname)
{
System.out.println(o);
}
int arr[] = {12,23,34,11,56};
for(int x:arr)
{
System.out.println(x);
}
int arr[][] = {{12,2},{11,2}}
for(int a[]:arr)
{
for(int b:a)
{
System.out.print(b);
}
System.out.println();
}
Program Explanation of For-Each Loop in JAVA:-
class ObjectArr
{
public static void main(String args[])
{
Object arr[] = {"C",12.34,true,'a'};
for(Object o:arr)
{
System.out.println(o);
}
}
}
It is modified form of for loop which is basically used in Array and Collection Object.
foreach loop is used to display the elements of array.
for(datatype o:arrayname)
{
System.out.println(o);
}
int arr[] = {12,23,34,11,56};
for(int x:arr)
{
System.out.println(x);
}
int arr[][] = {{12,2},{11,2}}
for(int a[]:arr)
{
for(int b:a)
{
System.out.print(b);
}
System.out.println();
}
Program Explanation of For-Each Loop in JAVA:-
class ObjectArr
{
public static void main(String args[])
{
Object arr[] = {"C",12.34,true,'a'};
for(Object o:arr)
{
System.out.println(o);
}
}
}
Post a Comment
POST Answer of Questions and ASK to Doubt