In Python there is an efficient for .. else loop implementation described
here
Example code:
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
}
Is there any better implementation similar to Python for .. else loop in Java?
if (finishedForLoop == true)withif (finishedForLoop). No need to test fortrue, theifstatement does that implicitly :)