d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Don' Tunderstand How This Works
Prev12
Add Reply New Topic New Poll
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 15 2016 01:57am
Quote (carteblanche @ Mar 14 2016 06:08pm)
i dont actually know much about coding. i just google the questions and copy/paste the responses and links. azrad is way better.


hey, i'm flattered, but i doubt it. Here is how a write a program:

1: figure out each step that needs to be preformed to complete the program.
2: Find a library that performs each of these steps.
3: write a program that glues those together.

Prob the same thing you are doing.
Member
Posts: 5,354
Joined: Dec 15 2008
Gold: 66.63
Mar 21 2016 08:41am
Did you get this figured out or are you still confused?

The labeled break only breaks THAT loop, so if i == 3 it would break loop three but execute loops 1 and 2 since loop 3 is contained inside them.
If you break loop 1, it is outside loop 2 and 3 so loop 2 and 3 are not executed.
Try this:
change i < 4 to i < 5 and add the statement if(i==4) break;
this should 'break' the for loop completely and only print the "after for." statement.

Any outer loop you end will end any inner loop as well. Make sense?
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Mar 26 2016 12:00am
I've never seen this before.

I get how it works, but anyone have a use case where they've used it or would?

I can't really think of when I would want to use this although it looks powerful/efficent...

Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Mar 26 2016 06:19am
Code
public static int stopCounter=0, noStopCounter=0;

public static void compareLineToStopWords(String line, String[] stopWords) {
//split the line up into words
String[] words = line.split("[\\W&&[^']]+|[\\d]+");
//use sentinel to break from the search when a match is found
Boolean sentinel = false;
for (int i = 0; i < words.length && !sentinel; i++) {
int min = 0, max = stopWords.length - 1;
while (max >= min && !sentinel) {
int mid = (max + min) / 2;
//if a match is found quit searching and increment the stopCounter
if (stopWords[mid].equalsIgnoreCase(words[i])) {
stopCounter++;
sentinel = true;
} else if (stopWords[mid].compareToIgnoreCase(words[i]) > 0) {
max = mid - 1;
} else {
min = mid + 1;
}
}
//if no match is found by the last word print the line and increment the noStopCounter
if (i == (words.length - 1) && max < min) {
noStopCounter++;
System.out.println("-" + line);
}
}
}

here you can see a method that searches an array for each word in a line and breaks out of the search entirely as soon as it finds a match and if it doesnt find a match it does some other stuff
i used a sentinel to show how you would have to do it without a labeled break

Code
//use binary search method to compare each word in the line against stopWords
public static void compareLineToStopWords(String line, String[] stopWords) {
//split the line up into words
String[] words = line.split("[\\W&&[^']]+|[\\d]+");
//use labeled break instead of sentinel
test: {
for (int i = 0; i < words.length; i++) {
int min = 0, max = stopWords.length - 1;
while (max >= min) {
int mid = (max + min) / 2;
//if a match is found quit searching and increment the stopCounter
if (stopWords[mid].equalsIgnoreCase(words[i])) {
stopCounter++;
break test;
} else if (stopWords[mid].compareToIgnoreCase(words[i]) > 0) {
max = mid - 1;
} else {
min = mid + 1;
}
}
}
//if no match is found by the last word print the line and increment the noStopCounter
noStopCounter++;
System.out.println("-" + line);
}
}

as you can see with the break you can jump out of the nested loop entirely
it simplifies the conditions in the loops because you no longer need the sentinel
also you don't need the nostop counter if statement because you would have broken out if the loop entirely if you found a word before the last word

regular breaks are unreliable and should be avoided but a labeled break allows more control over what you're doing

This post was edited by Ideophobe on Mar 26 2016 06:47am
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Mar 26 2016 09:31am
Quote (Ideophobe @ Mar 26 2016 07:19am)


here you can see a method that searches an array for each word in a line and breaks out of the search entirely as soon as it finds a match and if it doesnt find a match it does some other stuff
i used a sentinel to show how you would have to do it without a labeled break

as you can see with the break you can jump out of the nested loop entirely
it simplifies the conditions in the loops because you no longer need the sentinel
also you don't need the nostop counter if statement because you would have broken out if the loop entirely if you found a word before the last word

regular breaks are unreliable and should be avoided but a labeled break allows more control over what you're doing




Thank you very much. This help'd a lot.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 26 2016 11:20am
Quote (Noobtard @ Mar 26 2016 02:00am)
I've never seen this before.

I get how it works, but anyone have a use case where they've used it or would?

I can't really think of when I would want to use this although it looks powerful/efficent...


There's a similar example in the article "'GOTO Considered Harmful' Considered Harmful"

Quote
At least I can attempt to reopen the discussion by showing, a clearcut instance where GOTOs significantly reduce program complexity. I posed the following problem to a group of expert computer programmers: “Let X be an N x N malrix of integers. Write a program that will print the number of the first all-zero row of X, if any.”

Three of the group regularly used GOTOs in-their work. They produced seven-line programs nearly identical to this:

Code
for i :=1 to n
do begin
for j :=1 to n do
if x[i,j]<>O
then goto reject;
writeln ('The firstall-zero row is', i);
break;
reject: end;


The other ten programmers normally avoided GOTOs. Eight of them produced 13 or 14-line programs using a flag to indicate when an all-zero row was found. (The other two programs were either incorrect or far more complex.) The following is typical of the programs produced:
Code
i :=1;
repeat
j :=1;
allzero:=true;
while(j<=n) and allzero
do begin
if x[i,j] <> 0
then allzero := false;
j :=j+1;
end;
i :=i+l;
until (i>n) or allzero;
if i<=n
then writeln('The firstall-zero row is', i-1);


he reduced it to 9 lines. you can dig up the article to see it if you're interested. the point is it can add clarity.

This post was edited by carteblanche on Mar 26 2016 11:22am
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Mar 27 2016 12:31am
Quote (carteblanche @ Mar 26 2016 11:20am)
There's a similar example in the article "'GOTO Considered Harmful' Considered Harmful"



he reduced it to 9 lines. you can dig up the article to see it if you're interested. the point is it can add clarity.


i don't see any value to goto there
nor do i understand whatever that language is
Code
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (x[I][j] == 0) {
if (j == n - 1) {
System.out.printf("%d is the first line number with all 0s", i);
i = n;
}
} else j=n;
}
}

7 lines without gotos or breaks




This post was edited by Ideophobe on Mar 27 2016 12:53am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 27 2016 01:16am
Quote (Ideophobe @ Mar 27 2016 02:31am)
i don't see any value to goto there
nor do i understand whatever that language is
Code
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (x[I][j] == 0) {
if (j == n - 1) {
System.out.printf("%d is the first line number with all 0s", i);
i = n;
}
} else j=n;
}
}

7 lines without gotos or breaks


http://imgs.xkcd.com/comics/goto.png


That's fine. Nobody said you had to use it. If noobtard finds your code more intuitive and easy to follow, he might never look into it again.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll