Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Searching

Options
  • 19-05-2011 7:20pm
    #1
    Registered Users Posts: 3,500 ✭✭✭


    Hey

    Can some one explain this one for me. Its a sample program from a book il reading. I am searching an array of numbers for a certain number

    boolean found=false;
    int j=0;

    while(j<array.length&&!found){
    if(x==k)
    found = true



    Can someone tell me what the '!found" bit means? if the boolean found = false does '!found' = true? If yes then should 'found' in the loop not be false so the loop will keep goin until its true?

    thanks!


Comments

  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    ! is boolean logic for "not"
    So if found = true, then !found = (not)found = (not)true.

    In your case, found is set to false, so the check is for (not)false, which is true.

    The loop requires the boolean check to always be true for it to loop. So it checks that you haven't gone past the length of the array AND that you haven't found the result yet.

    When found is set to true, the (not)found part suddenly makes the boolean check fail, so it breaks out of the loop.


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    im goina have to be honest here and say i still dont get it!!

    if the boolean in the loop = true does that not mean the number has been found?


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Well, that's not a full program there, it's just a basic representation of some algorithm.

    I guess the full program would have an input and a defined array. The loop goes through each value and checks the input against each value of the array (if x == k ). If it is found, it sets "found" to true, and exits the loop. If it is never found, the loop exits with found still set as false, because you have reached the end of the array.
    J would be a number that sets the current position in the array, and gets incremented which each iteration of the loop, so that if it goes on long enough, J will be larger than the size of the array, meaning the logic check for the continuation of the loop fails.

    You would then check the found value after the loop to check if you found the number.


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    Ya your spot on with the way the program works. I understand everything in it just cant seem to get my head around why the boolean is true in the loop.

    il motor on and see does it click after trying a few more examples.


    Thanks for the help Giblet!


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    int [] myArray = new int [] { 1, 2, 3, 4 };
    int valueToSearchFor = 3;
    bool found = false;
    int j = 0;
    
    while(j < myArray.length && !found)
    {
        if(myArray[j] == valueToSearchFor)
        {
            found = true;
        }
        j = j + 1;
    }
    

    I have an array of 1,2,3,4
    I am searching for 3.
    I have a flag which is set to true if the value is found.
    Starting an position 0, I will check each value in the array until either I reach the end without success, or I find the value successfully. This is done using a loop so I don't have to do it manually.

    I check the position 0, and set my flag to true if I found it.
    This fails, so I increment the position (j). If I am still not past the end, and my flag is false, I try again (the loop).

    This continues until I do find my value. So I then set the found flag to true, which informs the loop on the next pass that I have finished with my checks.


  • Advertisement
  • Registered Users Posts: 3,500 ✭✭✭Drexel


    Giblet wrote: »
    int [] myArray = new int [] { 1, 2, 3 };
    int valueToSearchFor = 3;
    int j = 0;
    bool found = false;
    while(j < myArray.length && !found)
    {
        if(myArray[j] == valueToSearchFor)
        {
            found = true;
        }
        j = j + 1;
    }
    




    Ya thats exactly the code. I have it workin and all but my problem is i just dont get why 'found = true' in the loop.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    found is only set to true in the loop when x==k.


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    stevenmu wrote: »
    found is only set to true in the loop when x==k.

    So why the !

    is it just set to true so the loop can run??

    Sorry if im being a bit stupid here!! haha


  • Registered Users Posts: 428 ✭✭Joneser


    Think of the '!' as checking that the boolean negative. Therefore, it is always checking that found is set to false. Then once the boolean gets set to true, it breaks the loop, because the boolean is no longer negative.


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    Joneser wrote: »
    Think of the '!' as checking that the boolean negative. Therefore, it is always checking that found is set to false. Then once the boolean gets set to true, it breaks the loop, because the boolean is no longer negative.


    thanks joneser!

    I think im finally starting to understand it. Did a few more examples and did one or two on my own and its starting to sink in.



    Thanks for the help everyone!


  • Advertisement
Advertisement