1. Hi there, Guest

    Only registered users can really experience what DLP has to offer. Many forums are only accessible if you have an account. Why don't you register?
    Dismiss Notice

Beginning in Programming

Discussion in 'Tech Support' started by Agent One, May 20, 2018.

  1. Lord Ravenclaw

    Lord Ravenclaw DLP Overlord Admin

    Joined:
    Apr 2, 2005
    Messages:
    4,290
    Location:
    Denver, CO
    Like anything (including Knuth's oft repeated and misunderstand statement about premature optimization), everything in moderation. The hate around GOTOs really stems from non-local GOTOs (and Dijkstra's scathing dislike of them) where it became really really hard to reason about the flow of your program.

    Knuth has really good article, "Structured Programming with go to Statements" (pdf) that talks about the whole situation.

    As an example of something made considerably easier with goto (often disguised as "break <label>", right from the Java documentation:

    Code:
    class ContinueWithLabelDemo {
        public static void main(String[] args) {
    
            String searchMe = "Look for a substring in me";
            String substring = "sub";
            boolean foundIt = false;
    
            int max = searchMe.length() -
                      substring.length();
    
        test:
            for (int i = 0; i <= max; i++) {
                int n = substring.length();
                int j = i;
                int k = 0;
                while (n-- != 0) {
                    if (searchMe.charAt(j++) != substring.charAt(k++)) {
                        continue test;
                    }
                }
                foundIt = true;
                    break test;
            }
            System.out.println(foundIt ? "Found it" : "Didn't find it");
        }
    }
    These are gotos, but have their purpose.
     
  2. EsperJones

    EsperJones Headmaster

    Joined:
    Mar 3, 2006
    Messages:
    1,072
    Location:
    Seattle, WA
    Note: break/continue do not require labels.

    Code:
    for (int i = 0; i < 10; i++) {
      if (i == 3) break;
    }
    
    It can become useful if you have multiple nested loops and you want to break out of the entire construct (2D arrays are a great example) but in my years of professional Java development I have never seen the "break label" version in any production code.
     
  3. Lord Ravenclaw

    Lord Ravenclaw DLP Overlord Admin

    Joined:
    Apr 2, 2005
    Messages:
    4,290
    Location:
    Denver, CO
    It's rare you can't structure it in another way. If you've ever used Guava, there's a number of places it happens. I mostly see it in algorithm-heavy code -- in Guava, it's often because of microbenchmarks found something was a bit faster that way.
     
  4. Jarizok

    Jarizok Professor

    Joined:
    Sep 22, 2015
    Messages:
    420
    Gender:
    Male
    Location:
    Netherlands
    I have succesfully passed all three tests of the first six weeks (basic programming, SQL & databases and object oriented programming). This means that I'll be allowed to continue on to the next phase, which will largely consist of group projects.

    The amount of stuff I've learned in 5 weeks (we had one week off) is kinda crazy, but I'm still loving it. Don't love DOAs as much though. Still kinda don't really understand what they're really for, even if I can use 'm.
     
Loading...