break

Discussion in 'Java' started by Mike, Apr 10, 2007.

  1. Mike

    Mike Guest

    Hi:


    public class bbb
    {
    public static void main(String args[])
    {
    System.out.println(sum(100000));
    }
    public static int sum(int n)
    {
    if(n==0)
    break;
    else
    return sum(n-1)+n;
    }
    }

    After I compiled the above program, error message shows " break
    outside switch or loop".
    Why?
    Thank you in advance.
    Mike
     
    Mike, Apr 10, 2007
    #1
    1. Advertising

  2. Mike

    Tarkin Guest

    On Apr 10, 3:44 am, "Mike" <> wrote:
    > Hi:
    >
    > public class bbb
    > {
    > public static void main(String args[])
    > {
    > System.out.println(sum(100000));
    > }
    > public static int sum(int n)
    > {
    > if(n==0)
    > break;
    > else
    > return sum(n-1)+n;
    > }
    >
    > }
    >
    > After I compiled the above program, error message shows " break
    > outside switch or loop".
    > Why?
    > Thank you in advance.
    > Mike


    Because the keyword [ break ] is only used
    with looping constructs like
    switch (..) { ...} , while (..) {...},
    do {...} while (..) and for (...) {...} .

    [switch-case is a single-iteration loop, as
    is do {...} while(false) ]

    AFAIK in Java, if (..) ... or if (..) {...}
    does not count as a loop, thus you cannot use
    break.

    I believe you can code your construct in one
    of two ways:

    if (n != 0) ...

    or

    if (n == 0) {}
    else {...}

    The latter allows for slightly 'easier'
    code addition later.

    HTH,
    Tarkin
     
    Tarkin, Apr 10, 2007
    #2
    1. Advertising

  3. Mike wrote:
    > Hi:
    >
    >
    > public class bbb
    > {
    > public static void main(String args[])
    > {
    > System.out.println(sum(100000));
    > }
    > public static int sum(int n)
    > {
    > if(n==0)
    > break;
    > else
    > return sum(n-1)+n;
    > }
    > }
    >
    > After I compiled the above program, error message shows " break
    > outside switch or loop".
    > Why?


    Because you have a break that is not in a switch or loop :)

    By definition "A break statement with no label attempts to transfer
    control to the innermost enclosing switch, while, do, or for statement
    of the immediately enclosing method or initializer block; this
    statement, which is called the break target, then immediately completes
    normally." That makes no sense at all if there is no enclosing switch,
    while, do, or for statement in the method.

    I don't know what you are trying to do, but my best guess from the look
    of the code is that you want to return immediately from sum. You have to
    return something, because sum returns int, and from the context it looks
    as though 0 is the most reasonable thing:

    if(n==0)
    return 0;
    else
    return sum(n-1)+n;

    Patricia
     
    Patricia Shanahan, Apr 10, 2007
    #3
  4. Mike

    Mike Guest

    On Apr 10, 12:49 pm, Patricia Shanahan <> wrote:
    > Mike wrote:
    > > Hi:

    >
    > > public class bbb
    > > {
    > > public static void main(String args[])
    > > {
    > > System.out.println(sum(100000));
    > > }
    > > public static int sum(int n)
    > > {
    > > if(n==0)
    > > break;
    > > else
    > > return sum(n-1)+n;
    > > }
    > > }

    >
    > > After I compiled the above program, error message shows " break
    > > outside switch or loop".
    > > Why?

    >
    > Because you have a break that is not in a switch or loop :)
    >
    > By definition "A break statement with no label attempts to transfer
    > control to the innermost enclosing switch, while, do, or for statement
    > of the immediately enclosing method or initializer block; this
    > statement, which is called the break target, then immediately completes
    > normally." That makes no sense at all if there is no enclosing switch,
    > while, do, or for statement in the method.
    >
    > I don't know what you are trying to do, but my best guess from the look
    > of the code is that you want to return immediately from sum. You have to
    > return something, because sum returns int, and from the context it looks
    > as though 0 is the most reasonable thing:
    >
    > if(n==0)
    > return 0;
    > else
    > return sum(n-1)+n;
    >
    > Patricia- Hide quoted text -
    >
    > - Show quoted text -


    Thank you very much.
    Yes, I want to do a test study of sum by recursive.
    I heard that it runs quite slowly when one use recursive.

    Then why do people use recursive? On what situtation?
    Is there an example that one must use it?

    thank you

    Mike
     
    Mike, Apr 10, 2007
    #4
  5. Mike

    Daniel Pitts Guest

    On Apr 9, 10:21 pm, "Mike" <> wrote:
    > On Apr 10, 12:49 pm, Patricia Shanahan <> wrote:
    >
    >
    >
    > > Mike wrote:
    > > > Hi:

    >
    > > > public class bbb
    > > > {
    > > > public static void main(String args[])
    > > > {
    > > > System.out.println(sum(100000));
    > > > }
    > > > public static int sum(int n)
    > > > {
    > > > if(n==0)
    > > > break;
    > > > else
    > > > return sum(n-1)+n;
    > > > }
    > > > }

    >
    > > > After I compiled the above program, error message shows " break
    > > > outside switch or loop".
    > > > Why?

    >
    > > Because you have a break that is not in a switch or loop :)

    >
    > > By definition "A break statement with no label attempts to transfer
    > > control to the innermost enclosing switch, while, do, or for statement
    > > of the immediately enclosing method or initializer block; this
    > > statement, which is called the break target, then immediately completes
    > > normally." That makes no sense at all if there is no enclosing switch,
    > > while, do, or for statement in the method.

    >
    > > I don't know what you are trying to do, but my best guess from the look
    > > of the code is that you want to return immediately from sum. You have to
    > > return something, because sum returns int, and from the context it looks
    > > as though 0 is the most reasonable thing:

    >
    > > if(n==0)
    > > return 0;
    > > else
    > > return sum(n-1)+n;

    >
    > > Patricia- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Thank you very much.
    > Yes, I want to do a test study of sum by recursive.
    > I heard that it runs quite slowly when one use recursive.
    >
    > Then why do people use recursive? On what situtation?
    > Is there an example that one must use it?
    >
    > thank you
    >
    > Mike


    There are no situations where you MUST use recursion, however, there
    are many circumstances in which is simplifies the implementation of
    your algorithm.

    for example:
    public static <E extends Comparable<E>> void quickSort(List<E> list) {
    if (list.isEmpty() || list.size() == 1) {
    return;
    }
    final int partitionPoint = partition(list);
    quickSort(list.subList(0, partitionPoint));
    quickSort(list.subList(partitionPoint, list.length());
    }

    It is possible to replace this implementation to use an explicit
    stack, however, that would add an extra layer of complication...
    Internally, you're using the call stack so you don't need to be
    explicit about it.
     
    Daniel Pitts, Apr 10, 2007
    #5
  6. Mike

    Alex Hunsley Guest

    Mike wrote:
    > Hi:
    >
    >
    > public class bbb
    > {
    > public static void main(String args[])
    > {
    > System.out.println(sum(100000));
    > }
    > public static int sum(int n)
    > {
    > if(n==0)
    > break;
    > else
    > return sum(n-1)+n;
    > }
    > }


    public static int sum(int n)
    {
    return n*(n+1)/2;
    }

    :)
     
    Alex Hunsley, Apr 12, 2007
    #6
  7. Mike

    Guest

    On Apr 10, 7:56 am, "Daniel Pitts" <>
    wrote:
    > On Apr 9, 10:21 pm, "Mike" <> wrote:
    > There are no situations where you MUST use recursion, however, there
    > are many circumstances in which is simplifies the implementation of
    > your algorithm.


    And there are good reasons to avoid recursion in production code.
    Unless you have complete control over the data it opens opportunities
    for DoS attacks. Without additional guarding input data can eat up all
    your memory, slows down your system and finally crash the
    application.

    When you use an explicit stack you can easily put a limit to the stack
    size and enforce it.

    When you use the return stack you can control recursion depth by using
    an additional recursion counter somewhere. But that is often
    forgotten. Most algorithm textbooks don't demonstrate the problem, so
    algorithms blindly copied from textbooks are a classic source of these
    kind of bugs. And the code with a counter gets ugly. You either have
    some external "global" variable and can forget about multithreading,
    or you have to pass the counter down as argument.
     
    , Apr 12, 2007
    #7
  8. Mike

    Daniel Pitts Guest

    On Apr 11, 11:13 pm, "" <>
    wrote:
    > On Apr 10, 7:56 am, "Daniel Pitts" <>
    > wrote:
    >
    > > On Apr 9, 10:21 pm, "Mike" <> wrote:
    > > There are no situations where you MUST use recursion, however, there
    > > are many circumstances in which is simplifies the implementation of
    > > your algorithm.

    >
    > And there are good reasons to avoid recursion in production code.
    > Unless you have complete control over the data it opens opportunities
    > for DoS attacks. Without additional guarding input data can eat up all
    > your memory, slows down your system and finally crash the
    > application.

    Actually, threads have limited stack size, so you're more likely to
    just get a StackOverflowError.
    >
    > When you use an explicit stack you can easily put a limit to the stack
    > size and enforce it.
    >
    > When you use the return stack you can control recursion depth by using
    > an additional recursion counter somewhere. But that is often
    > forgotten. Most algorithm textbooks don't demonstrate the problem, so
    > algorithms blindly copied from textbooks are a classic source of these
    > kind of bugs. And the code with a counter gets ugly. You either have
    > some external "global" variable and can forget about multithreading,
    > or you have to pass the counter down as argument.


    In either case, you should always validate your input, regardless of
    your implementation choice. If possible, you should validate your
    input before you even get into the meat of the algorithm, so that the
    program fails faster, rather than 30 minutes into a process getting an
    exception.
     
    Daniel Pitts, Apr 12, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Coyne  Gaijin42

    Word wrap line break code and algorithm for c#

    Jason Coyne Gaijin42, Apr 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    24,269
    Jason Coyne Gaijin42
    Apr 8, 2004
  2. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,163
    Big and Blue
    Jan 30, 2005
  3. Jaco

    Client Side Break Mode

    Jaco, Jul 17, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    331
  4. Andrew
    Replies:
    3
    Views:
    388
    Andrew
    Aug 22, 2003
  5. Replies:
    12
    Views:
    988
Loading...

Share This Page