File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes A flow control issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A flow control issue" Watch "A flow control issue" New topic

A flow control issue

Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Hi all,

I'll give a general description of the issue I am faced with:

I have a for... loop, inside which there is an if... statement. Now, the else part of this if statement cannot be nested within the for...loop (because I don't want the else... part to loop). Outside the for...loop, there is a statement I want to execute, but it should not execute if what is contained within the if...statement inside the for loop executes (it is basically the else..part of the if...statement within the for...loop, but I cannot put it inside the for...loop because I want it to execute only once).

How do I control the flow of my program to achieve this?

Basically, I want to do a GOTO like implementation (I know how to do this with GOTO). How do I implement it in Java/JSP?

Thanks a ton.
[ January 16, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63862

There is nothing JSP-specific to this question; Java in a JSP is just like Java anywhere else. I've moved it off to a more appropriate forum.
[ January 16, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
some of the source of your problem would be useful to us figuring out what your dilemma is,

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46368
How do you implement it in Java with "goto?" Easy: you don't.

It is a little difficult to understand what you want, but the notion of an "if" inside a loop and an "else" outside a loop seems to be impossible to implement.

The idea of structured processing is to get rid of "goto" and its surrogates, which include "break" "continue" and multiple "return." That is why the JavaRanch code style (here-see 3.1) tells us not to use them.

Suggest you try this sort of thing:
  • declare a boolean local variable and initialise it to "false".
  • Apply some test in the loop and then apply its value to this variable. That can be inside or outside the "if."
  • After the loop, set up another if with !myBoolean as its condition.
  • That is what I would try. Other people might try something different
    Mark Newton
    Ranch Hand

    Joined: Jan 31, 2006
    Posts: 129
    What about something like...

    So - you're running through the loop, doing your 'if' test each time. If, at any time, you enter the 'if' section of code (your condition is true), you set the 'doneIf' flag, to say so. Then, once you've finished the loop, you look at the flag, to see whether your condition ever evaluated to true. If not, you do your 'else' code.

    [EDIT: Sorry, posted at the same time as Campbell, I think we agree though ]
    [ January 16, 2008: Message edited by: David Payne ]
    I agree. Here's the link:
    subject: A flow control issue
    It's not a secret anymore!