• Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie Question about the use of flow control algorithms  RSS feed

 
Nasir Bin Otis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java programing and algorithms I was going through the the java tutorial and was quite proud of myself as I was surviving chapt. 3 then I came across this demo and began to lose my footing
Can you please help me by explaining these few lines of code in the ContinueWithLabelDemo? correct me if i am wrong thanks . Oh yeah the (?) means if my note is incorrect, then please explain or clarify else comment if you wish.

class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me"; //String literal declaration instantiated (Y/N)?
String substring = "sub"; //String literal declaration instantiated (Y/N)?
boolean foundIt = false; //Boolean Primitive data type instantiated

int max = searchMe.length() - substring.length(); // This evaluates to a numerical integer value of 23 (Y/N)?
test:
for (int i = 0; i <= max; i++) { // This means: at this time integer i's numerical value is 0 and it will remain so until the next iteration of this loop then the value for i will be 1 and so on (Y/N)?
int n = subString.length(); // This means that integer n evaluates numerically to 3. (Y/N)?
int j = i; //This means integer J evaluates to the numerical value of 0 and will not evaluate to 1 until the next iteration of this loop. (Y/N)?
int k = 0;
while (n-- != 0) { //This means that while integer n-- (meaning 3=3-1) evaluates to not equal to 0 (Y/N)?
if (searchMe.charAt(j++) != substring.charAt(k++)) { // Meaning search for particular substring of text to match the it's parent as long as the test returns in-equal increment the variable J & K by 1(Y/N)?
continue test; // Continue the test if the previous statement evaluates to true(Y/N)? Can you reword the statement "skips the current iteration of the outer loop marked with the given label to describe what is happening here"?

}
}
foundIt = true;
break test; // Discontinue the test ?? How does this effect the outer loop?

}

System.out.println(foundIt ? "Found it" : // If then else construct (Y/N)?
"Didn't find it");


}


}

OUTPUT: Found it

TIA

-Nasir
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put your code between the [code] [/code] tags. I am having problems reading it.
 
Kevin McMahon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, normally, we wouldn't reinvent the wheel, so the code could probably be rewritten as



But... as this is an algorithm tutorial I guess the purpose is to analyse various ways methods like "contains" work.
Therefore:

//String literal declaration instantiated (Y/N)? : Yes.
The max variable is there to stop you checking characters at the end of the searchMe string when there are not enough characters left to check for it to be possible to find a match. In other words, if the substring is 6 characters long, and you only have 5 characters of the searchMe string left to check, there's no point in checking them - it's never going to produce a match.

The loop:

i refers to the number of starting positions in searchMe to start checking for a match. (Stopping at max as described above)
n is the number of characters which must be matched in sequence for foundIt to be true (in otherwords, the length of the substring we are looking for)

j maintains a reference to the current character we are examining in the searchMe string. Initially, this is driven by the outer loop (i) value, but say we start to match characters, j will increase within the loop as we check the next character, then the next and so on.

k is the position in the substring we are checking for. This will always start at 0 for every loop, as we always need to start looking for the first character of the substring.

the while(n-- != 0) basically means "while we still have characters of the sustring left to match" or "until we run out of substring characters to check for)

We then compare the current character in the searchMe string with current character in the substring. If they don't match, we can start our for loop again, moving on to the next "start position" or character in our searchMe string.
If they do match, the while loop continues. Bear in mind that n has been reduced by one, so now we have 1 less character left to match. k and k have increased, meaning we are now checking the next character in searchMe (j) with the next character in the substring (k)

If we manage to match the full length of the substring(n) times, the while loop will complete and foundIt will be set to true. The break statement is nested in the outer for loop, so therefore when a match is found, we break out of that loop.



To look at and learn the way this works, I would suggest naming the variables differently:

i = outerLoopSearchMePosition
n = numberOfCharactersStillToMatch
j = currentSearchMePosition
k = currentSubStringPosition

Then put some System.out.println trace statements in so you can see exactly what is being done.
Also, if you are using Eclipse or some IDE that allows you to debug, put a breakpoint at the start of the main method and then step through the code. You can watch the values of the variables that way as you step through the code statement by statement. That would probably help.

Hope that helps a little?
 
Nasir Bin Otis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Nasir Bin Otis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kevin!
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an example the Java Tutorial writers ought not to be proud of. Kevin has done a lot of work answering it (thank you). Your long comments, which I have broken up, made it hard to read even with the code tags. You will find a different opinion about continue here under 3.1. Labelled continue is even more difficult to understand than ordinary continue.

What the example does is to go through "Find a substring in me" letter by letter from start to finish. When it finds a letter which matches the beginning of "sub", it starts a new loop which looks for the second letter of "sub". It would have been better to search a String like "Searches this super string to see whether you find substring anywhere." because it has several "fault starts" with "s" and "su".
The idea of the "continue test;" is that the loop stops half-way through, then goes back to the test: label which is before the outer loop, so it restarts the outer loop where it left off. The same page in the Java Tutorial compares that behaviour with break labelled break and ordinary continue. Can you understand that? If not, maybe that is an argument for forgetting all about break (except after switch . . . case) and continue.

******************************************************************************************************

Another problem is while (n-- != 0) . . . Did you understand that bit? If you precede that with int n = 3;, can you work out how many times the loop will run?
And have you understood the . . . ? . . . : . . . bit?

If you have the slightest problem with either of those points, please start a new thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!