简体   繁体   中英

Can you use a for loop inside the condition of an if-else statement?

Can you use a for loop inside the condition of an if-else statement? For example, something like this...

if(
        for(q = 0; q < 10; q++){
            values[q]>=values[q+1];
        }          
            )

            {

         done = 0;
    }

This is loading an error I can't seem to place. I want the if statement to check to see if the int[] I called values is in order from greatest to least, and if it is, set int variable done equal to 0.

I only just started taking a programming class and I bet this is a very dumb mistake, but I've been trying to figure this out for a while and some help would be absolutely fantastic.

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;
}

A for loop can exist inside of an if block

if (true) {
    for (int i = 0; i < 5; i++) {
        System.out.println("Hello, World!");
    }
}

But a for loop can not be the condition of the if block

if( for (int i = 0; i < 5; i++) { } ) { }

A for loop is not a boolean. Every if condition requires a boolean.

No . If requires a boolean test. For loop doesn't return a boolean value. You can instead separate the check into another function.

if( isArrayOrdered(values) ){
    done = 0;
}

// assuming values is an array of `int` values
private boolean isArrayOrdered( int[] values){
   for(int q = 1; q < values.length; q++){ 

      if( values[q] > values[q-1] ){
         return false;
      }
   }

   return true;
}

No, you can't. The if condition must evaluate to some boolean value, which doesn't happen with this for loop.

It can only be in the if statement body, like

if(someCondition)
    for(int i = 0;i < 10;i++)...

To achieve your goal, a code like this one might help:

boolean ordered = true;

for(int i = 0;i < array.length - 1;i++) {
    if(array[i] > array[i+1]) {
        ordered = false;
        break;
    }
}

if(ordered) // your if body here

The expression inside the "if" condition should evaluate to a boolean or Boolean otherwise the code will not compile.

Check that Java specs page here

I think you probably want the alternate nesting, and start with the assumption that done = 0 (that is, that the list is correctly ordered), unless you prove in the loop that it is not true.

public class Play{
    public static void main(String[] args) {
        done = 0;
        for(q = 1; q < 10; q++){
            if (values[q-1]<values[q]) {
                  done = 1;
                  break;
             }   

        }          


    }
}

You need to break you logic down into steps.

first of all, a method

boolean isInOrder (int [] values) {

    for(int q = 0; q < values.length - 1; q++){

        if (values[q] > values[q+1]) {
          return false; 
        }
   }
   return true;
}

// now lets call this method

  if (isInOrder (values)) {
     System.out.println ("In Order");
  }
  else {
     System.out.println ("Not In Order");
  }

将“for 循环”语句放在 if 语句“(”和“)”之间是不可能的。但是如果你在 if 语句“{”和“}”之间写“for 循环”是可能的。谢谢

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM