Results 1 to 6 of 6
  1. #1
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Control flow operators

    Hello all. I was wondering if there are any more control flow operators than just the 'break' command. Also, is there any way to specify a 'if at any time..'? For example, I have a short that should never go above a certain limit (8 to be exactly). Would the best way to do this be to use an if statement every time I change the short?

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Can you please show a snippet of your code. Just the relevant parts.

    Thanks
    --user0--

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well i guess in theory you could throw an exception and have a try.. catch block handle the flow from there. that is usually for unexpected or exceptional situations.

    Another idea, is to investigate "aspect oriented programming" concepts, where unlike the one that comes with springframework, which only allows you to attach "advice" onto methods in your bean, if you use aspectj compiler (a special javac designed to work annotations in java source to produce regular byte code .class files that run in regular java runtimes) it will allow you to attach advice to members of an object. So in this sense you could have an aspect for "on variable hitting value >= 8" to have it invoke something.

    But for most general loop control conditions, for or while loop with a break is probably the easiest and most effective way to go.

    uh yea,

    so
    Java Code:
    while (someVar < 8) {
      //do stuff
    }
    is also valid

  4. #4
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Ok, travishein answered the question for me. Thanks.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    There is also continue and return. However, most of the time you should be able to write code where they are not needed.

    You could make setting the value of the variable and testing with an if statement in a method. Slightly more efficient than having the same if statement throughout your code.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Also, don't forget that you can use labels with continue and break to control where the code returns to. Helpful for nested loops!

Similar Threads

  1. How to generate Data Flow Diagram(Work Flow Diagram)
    By stsivaraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-23-2010, 12:21 AM
  2. where exactly bit shift operators are useful?
    By sandeepsai39 in forum New To Java
    Replies: 6
    Last Post: 09-08-2010, 12:58 PM
  3. New to operators, need some help...
    By Stryker4526 in forum New To Java
    Replies: 4
    Last Post: 09-26-2009, 02:29 AM
  4. control app width based on certain control
    By thebillybobjr in forum SWT / JFace
    Replies: 0
    Last Post: 05-15-2008, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •