0

In the following code I want to execute the if condition inside the outer for loop several times, but in the following code it only executes once and executes the steps after it. binaryHashResult is an array and innerNode is a List. I want to check if all values in array exist in list.

for (int j = 0; j < binaryHashResult.length; j++) 
    if (innerNode[binaryHashResult[j]] == 1.0)
        for (int h = 0; h < m.children.size(); h++) {
            BloomFilterIndex.BFINode<Integer> s=m.children.get(h);
            searchencryptedNode(binaryHashResult, e,  k, s);
        }  
dave
  • 11,641
  • 5
  • 47
  • 65
  • 2
    How do you know it is only executed once? Did you use a debugger? – Michael Butscher Nov 25 '18 at 01:18
  • 2
    @W.Ambrozic a `for` loop does not _require_ braces, although it's generally a good practice to do so. – dave Nov 25 '18 at 01:24
  • Please show a [mcve]... If it only execute once, then are you ever changing the values in the array? What are the values? – OneCricketeer Nov 25 '18 at 01:27
  • Might want to check out some debugging tips too https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – OneCricketeer Nov 25 '18 at 01:29
  • 1
    @W.Ambrozic Not quite. It's not one _line_ but one _statement_. The next statement is an `if` and it (and its contained `for` loop) form the single statement of the outer `for` loop. So the five lines following the initial `for` form its body. – dave Nov 25 '18 at 01:30
  • @user9860564 it's possible your floating point comparison `== 1.0` is failing to perform as you might expect. Check out this [question](https://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java). – dave Nov 25 '18 at 01:34
  • iam using debugging so i noticed that it execude the first (i) when it satisfy ,it complete the next statements. but i want to check all (i) before complete the next steps. – user9860564 Nov 25 '18 at 01:38

1 Answers1

0

Always wrap blocks of code with {} regardless if the block has only one line.

for (int j = 0; j < binaryHashResult.length; j++) 
    if (innerNode[binaryHashResult[j]] == 1.0) {
        for (int h = 0; h < m.children.size(); h++) {
            BloomFilterIndex.BFINode<Integer> s=m.children.get(h);
            searchencryptedNode(binaryHashResult, e,  k, s);
        } // closes inner for loop
   } // closes if
} // closes outer for loop
fpezzini
  • 774
  • 1
  • 8
  • 17