Jun 28, 2009 3:06:48 PM Sent from my BlackBerry® wireless device -----Original Message----- From: miga <[email protected]>
Date: Sun, 28 Jun 2009 11:34:00 To: Free Java Programming Online Training Course By Sang Shin<[email protected]> Subject: [java programming] Re: Lesson 1034 - MyOwnWhileLoop On Jun 28, 7:41 pm, Gibbs <[email protected]> wrote: > On Jun 27, 8:39 pm, miga <[email protected]> wrote: > > > On Jun 27, 10:45 pm, Dan <[email protected]> wrote: > > > > while (i<names.length) > > > {i++; > > > Move the line above > > > > if (names[i].equals(searchName)){ > > > foundName = true; > > > break; > > > } > > just here > > > } > > OK--I finished this lesson last night, and initially had the i++ > directly below the "break" (Inside the following }). I realized this > wouldn't work, > and then placed it correctly, but I still haven't entirely wrapped my > mind > around it. Am I misunderstanding exactly what "break;" does? Also, > am I > correct in that the i++ is still falling under the while condition in > this > placement? Not sure if I'm making myself clear, but any help would be > appreciated. The correct code is: int i = 0; while (i < names.length) { if (names[i].equals(searchName)) { foundName = true; break; } i++; } So, first you initialize the counter before the while in order to have a counter, as contrary to a for loop there is no automatic counter initialization inside a while loop. Then you traverse the array of names by the indexes. Any array begins with an index of 0 and terminates with an index of n-1 if n is the size of the array. Hence your while loop goes from 0 included to names.length excluded (that's exactly how it is defined inside the while (...) line. Then once you get at a particular index you inspect whether or not the particular name corresponding to this index equals the given name (the one given by the JOptionPane above the while loop. If it equals it, you put a predefined flag to true - to be able to test it once you will be outside of the while loop - and you go out of the loop - since you have found a match hence your job is done with the search. If not, you are not done with the search so you must get at the next name in the array, to do it, you just increment i and as it is the last line of the while loop it goes automatically to the first line to test whether or not you are past the end of the array, if not it does again the search for the given name as above, if it is it goes out of the loop automatically. So that, in the end, as you define before the while loop the flag to false, if you have found a match inside the loop, you've got a true, if not you've got a false. Hope it is clear now. Just a matter of thinking how you will dichotomize it manually with your brain - if I dare say so -, and then transcript it in Java. --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
