Can you use a for loop inside the condition of an if-else statement?
You should work out your condition first (ie is your array in order), and then feed that in to your if
statement. Like so...
boolean isOrdered = true;
for(q = 0; q < 10; q++){
if (values[q]>=values[q+1]){
// in order
}
else {
// not in order
isOrdered = false;
break; // we have found a false, so we can quit out of the for loop
}
}
if (isOrdered){
// do something if the array is in order;
}
You can do it if you refactor the logic into a method that returns a boolean:
if (isInOrder(values)) {
//
}
private static boolean isInOrder(int[] array) {
for (int i = 0; i < array.length - 1; i++)
if (array[i] > array[i+1])
return false;
return true;
}