0
public static int search(int[] a, int target) { int i=0; boolean found = false; while((i<a.length) && ! found) { if (a[i] == target) { found = true; } else i++; } if (found) return i; else return -1; } 

I dont understand the if statement part. So how i am reading it in my head is found is set to false. If not found...so if not false (since found = false), do whatever. So basically im reading it as a double negative and seeing if (true) dow whatever. But it doesnt make sense. I know its not an inifite loop and it runs fine but I dont get the logic, it must not be a double negative. Thanks!

EDIT: So i get that we could just return i, much easier yes I agree. I just am having trouble with the logic of the boolean value being used in the loop with the not "!" symbol.

Basically if i wrote this i would say (ignoring everything else)

found = true //found is true to begin with while (!found) //while not true continue to next index //continue until ....actually i'm getting very confused now because to break the loop we would continue until found is false which logically is backwards

EDIT: Thank you everyone for your comments!! It all helped me understand it!

4
  • This could be written a lot simpler by just returning the index instead of found = true;. Then at the very end always return -1, because you'll only reach it if nothing was found. --- I don't blame you for not getting this, it's written slightly weirdly. Commented Nov 4, 2017 at 19:38
  • Take a pen and a piece of paper. And then just run this manually. Commented Nov 4, 2017 at 19:46
  • What @GhostCat said often makes it better understandable. What basically happens is that you set found to false, so you are executing the codeblock inside the while loop as long as !found – indeed a double negation (found was false, so not false is true). while only executes as long as the statement inside the parenthesis is true. Commented Nov 4, 2017 at 19:54
  • Ohhhh!!! i didnt know that the statement inside the while loop must be true. I thought it depends on what you want. So basically the point of "!" is to make this part of the loop true so the loop can run. Then when its found we set found to true, and now the "!" makes found false and we exit. So basically the part inside the loop i should NOT directly translate to english. Just worry about whether its true or false to keep the loop running or not? Commented Nov 4, 2017 at 20:00

10 Answers 10

2
public static int search(int[] a, int target) { for (int i = 0; i < a.length; i++){ if (a[i] == target) return a[i]; // or i if you want to get ingex of searched element } return -1; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Remember to return -1 at the end if nothing is found
Please don't put up code only answers. Prefer using explanatory text instead of embedded comments. Those are easily overlooked.
1

The ! operator inverts the following logical statement (logical not gate).

Because !false is true, it means it is not a double negative.

Comments

1

!found means the same thing that it means in English, i.e. "not found". The entire condition with i<a.length reads "while i is a valid index and [target is] not found", which is pretty close, given that you know that "not found" refers to target

You can simplify this loop to avoid Boolean variable:

while(i < a.length) { if (a[i] == target) { return i; } i++; } return -1; 

Comments

1

Do you think it is easier?

 public static int search(int[] a, int target) { for (int i = 0; i < a.length; i++) { if (a[i] == target) { return i; } } return -1; } 

3 Comments

And some digression: If you not must used while that you will use more simpler loop like for.The less code is better
Does this explain what he is asking for?
Problem is arising from bad code. When we simplified code then we avoid this problem.
1

Let me break it down into a few parts for you:

while((i<a.length) && ! found) 

So you start with false. As !false evaluates to true, you continue the loop until found is true which will make the whole condition false, causing you to break the loop.

i<a.length: while i<length returns true, continue the loop. You basically want found to be false and i<length for the loop to continue. If any of the condition isn't met, you break the loop (check out the &&).

 if (a[i] == target) found = true; else i++; 

This is simple: if the number is found, make the found boolean true. This will cause the next iteration condition to evaluate as false, causing the loop to break.

Comments

0

The code loops over the array of ints while found is false. During the loop it compares the current array value in a (a[i]) to the target, If the values are the same, it sets found to be true.

Thus after the loop it checks if found is true, and if it is it returns i, the index of the last comparison value.

Comments

0

At the start of the loop found is false, so the while loop will start. Once a value is found, found is set to true and the loop stops. I would rewrite the loop however:

public static int search( int[] a, int target) { int foundIdx = -1; for( int i = 0; i < a.length && foundIdx < 0; i++ ) { if( a[i] == target ) { foundIdx = i; break; } } return foundIdx; } 

Comments

0

So, I think your doubt is on the while statement, but I'll put some comments on all the code to try to explain what it does:

 public static int search(int[] a, int target) { int i=0; boolean found = false; // While i index is less than the array length AND item hasn't been found (negation of the found variable), keep iterating on the loop. If not (if any of this two conditions aren't met), continue. while((i<a.length) && ! found) { /* If the targeted item has been found, set found boolean to true, so this loop will stop before next iteration */ if (a[i] == target) { found = true; } // If targeted item is not this one, increase the index for next iteration else i++; } /* This condition only will be met if we exit the loop because we found the target. In that case, we will return the index in the array of that index. If we "finished" the array without finding it, we will return -1, meaning "it wasn't found". */ if (found) return i; else return -1; } 

Comments

0

Set the boolean found = false; because you have not found what you're looking for yet, then while (i<a.length) and ! found means that if you have not reached the end of the array and you have not found what you're looking for do

{ if (a[i] == target) { found = true; } else i++; } 

If you found it change the variable found to true which with the negation operator ! will be changed to false and the && will take you out of the while loop since one of the 2 conditions is not true any more. In other words if found is false then in the loop is true because you have ! (not) found it. And if found is true then in the loop is false because you have found it.

1 Comment

Perfect thank you! I didnt realize both things must be true to continue the loop, i thought if you want one thing true and one false do whatever. I guess both must be true and then ! helps to keep the loop true until found is true then ! makes it false and it exits. Thank you!!
0

What you think about using IntStream ?

public static int search(int[] a, int target) { final OptionalInt index = IntStream.range(0, a.length).filter(i -> a[i] == target).findFirst(); return index.isPresent() ? index.getAsInt() : -1; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.