samedi 4 novembre 2017

Boolean use in an if statement

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!

Aucun commentaire:

Enregistrer un commentaire