Happy to Announce Our New Discussion Forum | Join Now

JAVA For-Else Loop

Can Anyone Explain Me How the Code Will RUN ???

public class Nested
{
public static void main(String[] args)
{
int[][] myComplexArray = {{5,2,1,3},{3,9,8,9},{5,7,12,7}}; // 2 dimensional arrey
for(int[] mySimpleArray : myComplexArray)
{
for(int i=0; i<mySimpleArray.length; i++)
{
System.out.print(mySimpleArray[i]+"\t");
}
System.out.println();
}
}
}

The code does not compile due to the fact that it has an incomplete “for” statement. The second “for” loop should be something like this:

for (int i = 0, i < mySimpleArray.length; i++) and it will print this:

5 2 1 3
3 9 8 9
5 7 12 7

So your code does not compile. Use Eclipse in order to investigate simple problems like this.

Compilation error, Check line 8 in screenshot. Please recheck the code once.

After updating the line 8 with correct code, i got following output

JAVA For-Else Loop code is given below:

for x in range(2, n):
if n % x == 0:
print n, ‘equals’, x, ‘*’, n/x
break
else:
# loop fell through without finding a factor
print n, ‘is a prime number’


In Java I need to write more code to achieve the same behavior:

finishedForLoop = true;
for (int x : rangeListOfIntegers){
if (n % x == 0)
{
//syso: Some printing here
finishedForLoop = false
break;
}
}
if (finishedForLoop == true){
//syso: Some printing here
}

For more to learn JAVA basic to Advance click: https://www.cetpainfotech.com/technology/java-training