This site requires JavaScript, please enable it in your browser!
Greenfoot back
EIGNERT
EIGNERT wrote ...

2014/12/3

While...Else...?

EIGNERT EIGNERT

2014/12/3

#
Hey Guys, Is there such thing as a 'While-Else' statement? I want to have something in a while loop; however, if the conditions in the while loop aren't met, then I would like my program to do something else NOT in a loop. Any help with this would be much appreciated.
danpost danpost

2014/12/3

#
The 'while' loop will exit the moment the conditions are not met. So, any code following it would be your 'else' code (without having to actually say 'else'). However, I do not think that is quite what you meant. You could ask the condition in an 'if' statement; and if true process the 'while' statement and if not 'else' the 'if'.
1
2
3
4
5
6
7
8
if (conditions)
{
    while(conditions) { ... }
}
else
{
    ...
}
This way the 'else' code is only executed if the 'while' code is not. Another way is declaring a boolean and changing its value inside the 'while' statement; then, using it for a future 'if' block:
1
2
3
4
5
6
7
boolean whileExecuted = false;
while (conditions)
{
    ...
    whileExecuted = true;
}
if ( ! whileExecuted) { ... }
EIGNERT EIGNERT

2014/12/3

#
Hey, just tried that and is not working as I'd like it to. Is there are way to make my program go back to the point BEFORE the 'if statement' after the part within 'if' is completed? Like this:
1
2
3
4
5
6
7
8
9
10
RETURN
if(--CONDITION--)
{
    ACTION
    then return to where it says return
}
else
{
    ACTION
}
Thanks
danpost danpost

2014/12/3

#
You can enclose the code in a 'do' loop. But, you must have a way to 'break' out of it.
davmac davmac

2014/12/4

#
Is there are way to make my program go back to the point BEFORE the 'if statement' after the part within 'if' is completed? Like this:
What you have written is equivalent to:
1
2
3
4
5
while(--CONDITION--)
{
    ACTION
}
ACTION
... since at some point the 'else' branch must be executed. Your question in general seems like a possible example of the XY Problem. Instead of phrasing your question in terms of 'while' and 'if', how about just explain what it is that you ultimately want to accomplish?
EIGNERT EIGNERT

2014/12/4

#
Thanks for the replies. This is all working, however I just want it to do the thing within the 'while' loop ONCE, but still stay in the 'while' loop until the conditions aren't met. Within the 'while' loop I am printing something.
danpost danpost

2014/12/4

#
It sounds something like this (although it seems strange to do this -- like there might be an easier way to go about it):
1
2
3
4
5
6
7
8
9
10
11
12
boolean printExecuted = false;
while (conditions)
{
    if ( ! printExecuted)
    {
        ACTION (print)
        printExecuted = true;
    }
    // the conditions must be allowed to change somehow
    // or you will be stuck in the loop
}
ACTION2
davmac davmac

2014/12/4

#
This is all working, however I just want it to do the thing within the 'while' loop ONCE, but still stay in the 'while' loop until the conditions aren't met.
That doesn't make sense. You can't really 'stay in the while loop', without doing anything, until the condition becomes false - because the condition will never become false if you don't do anything whilst inside the loop. I suspect that what you really want is for an actor to do something once when a condition becomes true, and then do something else once the condition becomes false. You do not a 'while' loop or any other kind of loop for this. Typically you would code it something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// instance variables
boolean firstConditionMet = false;
boolean secondConditionMet = false;
 
public void act()
{
    if (! firstConditionMet) {
        if (condition1) {
            // ACTION1 here
            System.out.println("Action 1");
            firstConditionMet = true;
        }
    }
    else
    {
        if (! secondConditionMet) {
            if (condition2) {
                secondConditionMet = true;
                // ACTION2 here
                System.out.println("Action 2");
            }
        }
    }
}
For your case 'condition2' should just be the inverse of 'condition1'. I.e. condition2 = (! condition1).
danpost danpost

2014/12/5

#
davmac wrote...
That doesn't make sense. You can't really 'stay in the while loop', without doing anything, until the condition becomes false - because the condition will never become false if you don't do anything whilst inside the loop.
I was going to say something like that, but if the condition was time using the system clock, waiting is all that needs to be done. So, I intentionally avoided going there and qualified my post with lines 9 and 10.
danpost danpost

2014/12/5

#
It might be best if you show what you are attempting to do by posting the questionable code -- the more you give, the better understanding we would gain context-wise. It would give us a better idea of what might be needed and you may be able to move on more quickly.
You need to login to post a reply.