• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anyone check my understanding of this program please?  RSS feed

 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing out my understanding of the following program in sentenced English, how I'd try and explain how to do it in person. I've tried to be formal, and to use the correct terminologies, so if there is anything that's not phrased properly please point it out otherwise I might plod along without realising

OK here's the program (It's from the Oracle tutorials) :



Explanation :

Create String variable to be searched through (searchMe) and a String to search through searchMe for (subString).
After this a boolean variable is created which will indicate when the subString is found. It's default is changed as leaving it on true will create a false positive and make the program ineffective.

Then we need to find out the maximum amount of characters that can be searched through without the subString being present, this is done using the .length method of String, and subtracting the subString from searchMe. This answer is mapped to the int max

Next is the loop, a label is created for the loop that will work with the continue statement.
First a for loop is used which will iterate through searchMe until it reaches int max value.
Within the for loop variables are created, first one (int n) that is the value of the subString. This will be the amount of characters to be referenced each time. Next an int j which is equal to i, (as i is already being used within these balanced braces) this will be used to iterate through the main String. Finally an int that can be used to iterate through the subString (int k)

Then a while loop is made, and its declaration (declaration the right term....?) should enable it to run by the amount of charaters int he subString each time. This is done by using the int representing subString and reducing it by one each iteration, having while stop when the value reaches zero.
Nested in the while loop is an if statement, that will compare the character in searchMe to the subString, one at a time until the value of the subString is reached. At this point the while loop ends, the fo increaments the value of i (and in turn j) and the while loop is activated once more but from one character further into the searchMe String.

The first loop would check

'Loo'

second

'ook'

third

'ok '

fourth

'k f'

etc etc...


If the characters are not equal, continue returns back to the 'test' label (Why doesn't it break out of the loop as soon as one matching value is reached?)

Once the subString has been found, the boolean value is changed to true and the break statement puts us into the code following the 'test' loop.

Here we print out the results, using an if then else statement.





OK, so that's my understanding of it, I think it's close, but there are a couple of areas that I haven't quite got.

Does is make sense ?

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. The reason it doesn't simply break out of the inner loop is that it is intended to show labelled continue. You are not using an if‑then‑else to print the result, but a conditional operator, which has some similarity to an if‑then‑else.

Many of us, myself included, avoid labelled continue like the plague.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are correct. The reason it doesn't simply break out of the inner loop is that it is intended to show labelled continue. You are not using an if‑then‑else to print the result, but a conditional operator, which has some similarity to an if‑then‑else.

Many of us, myself included, avoid labelled continue like the plague.


Thanks Campbell, reassuring to have some one look at the understanding (not just code...)

ah ok, bit of a wrong term there (if then else..) cheers
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
Mike Philips wrote: . . . Why doesn't it break out of the loop as soon as one matching value is reached? . . .
I have already given the reason, but shouldn't that read non‑matching value?
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You're welcome
Mike Philips wrote: . . . Why doesn't it break out of the loop as soon as one matching value is reached? . . .
I have already given the reason, but shouldn't that read non‑matching value?


I thought that the if statement was continuing thing's if they weren't equal :



is that not activating continue so long as the character's don't match?

What I didn't know was why as soon as one matched the program didn't go on to foundIt = true; - does the if statement check all letters of the subString at once (rather than one at a time) ?

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The if statement checks one letter only. It is called up to thrice. It checks for letters which are not equal. If you get a non‑matching value, it calls continue on the outer loop.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The if statement checks one letter only. It is called up to thrice. It checks for letters which are not equal. If you get a non‑matching value, it calls continue on the outer loop.


OK so the program would see L, reference it against subString's s, see that it's not matching, go to the continue, and restart from the for loop, but with i now having a value of 1.

Meaning that when the program looked again it would reference o, check it against the second element of subString, and hit continue again...

Once i got to a value of 11 the program would see s, and instead of hitting continue (because the characters are no equal) the program would (I'm stretching a little here) stay on the while loop, yet the value of n would now be reduced to 2, and 1 added to both j and k.... because the program was now looking at the subString within the searchMe String this would continue, and n would reach the value zero before the if statement would find a non-match.

As the while loop has been "completed" (ended / finished?) the program goes straight to the next line of code out of the scope of the while loop - which sets foundIt to true, and breaks out of test....

Is that right? Or is there something that I'm missing there?

Cheers Campbell.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think so.

It shows how confusing labelled continue and labelled break are, too.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK great! Yes it was a bit of a spin
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You now know why I said many people avoid those constructs.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tell that to Oracle ;)
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wasn't Oracle who developed Java®; it was a team under James Gosling at Sun Microsystems Inc, and Oracle took Sun over about three years ago.
It wasn't Oracle who introduced those constructs; it was Brian Kernighan and Dennis Ritchie (who died last year) at Bell/ATT labs over 40 years ago, and they may have been extant in older languages; not sure about that. The older languages supported the goto keyword, which Java® retains, but never uses, and the labelled constructs were probably introduced as a less bad alternative to goto. The goto construct goes back at least to the 1960s, and probably to the very first high‑level language, FORTRAN from about 1956.
The labelled break and labelled continue constructs have been copied from C into C++ and Java®. If the tutorials claim to be comprehensive, then they have to show every language feature.

I thought C# would also support labelled break, but when I Googled, I found this.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It shows how confusing labelled continue and labelled break are, too.

Not to mention the use of compound expressions:
if (searchMe.charAt(j++) != substring.charAt(k++)) { ...
involves at least 5 operations, two of which (the increments) involve a read/write that you may not actually want.

The main problem with expressions like this is that if you ever reference j or k again - regardless of the outcome of the if statement - they will be 1 more than they were when you executed it.

Personally, I prefer for loops, especially when many variables are involved, viz:
Winston
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those compound expressions look like the sort of thing you see in the older C books.
When C came out, memory was scarce and expensive, so every keystroke cost however much to implement. That is why you get one‑letter identifiers, no space between successive tokens, and semicolons instead of loop bodies.
I do not have trouble with compounding ++ and --, but you ought to use the variable compounded as an index and nothing else.
 
Junilu Lacar
Sheriff
Posts: 11490
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Dennis Ritchie (who died last year)

Dennis passed sometime in October 2011, about the same time that Steve Jobs passed.

There are occasions when break and continue can come in handy but they should be used sparingly if at all and should be relegated to the bowels of your programming toolbox.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I do not have trouble with compounding ++ and --, but you ought to use the variable compounded as an index and nothing else.

I don't either (I grew up with C, and so got quite used to it); but if I'm doing it, I much prefer pre-incrementing (++i) to post-incrementing (i++) for the reason I explained above: if the variable/index is ever accessed afterwards, it contains the same value as was evaluated in the compound statement. Unfortunately, it often means setting the initial value to -1.

Winston
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: . . . Dennis passed sometime in October 2011, . . .
I didn't realise it was that long ago.
 
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!