Author |
Message |
iluvchairs112
|
Posted: Wed Apr 04, 2007 7:19 pm Post subject: if statements and loop |
|
|
this is probably really basic, but I can't seem to figure it out (and I'm kinda new to Java anyway)
so, if I have a couple of if-statements and I want them to repeat. like if one becomes untrue then go on to the next one. if that is untrue then go on to the next one. then if the third one is untrue, go back to the top. i've tried experimenting with it, but can't figure it out. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Geostigma
|
Posted: Wed Apr 04, 2007 7:44 pm Post subject: RE:if statements and loop |
|
|
I don't know how java works but you need a else or elsif style command. |
|
|
|
|
 |
rdrake

|
Posted: Wed Apr 04, 2007 8:14 pm Post subject: Re: if statements and loop |
|
|
iluvchairs112 @ Wed Apr 04, 2007 7:19 pm wrote: this is probably really basic, but I can't seem to figure it out (and I'm kinda new to Java anyway)
so, if I have a couple of if-statements and I want them to repeat. like if one becomes untrue then go on to the next one. if that is untrue then go on to the next one. then if the third one is untrue, go back to the top. i've tried experimenting with it, but can't figure it out. Take a look at the while and do while loops.
While:
While the condition is true, the while loop happily repeats itself.
Java: | while (condition) {
// My code to execute...
} |
Do While:
While the condition is true, the do while loop also happily repeats itself. Difference here is the code block is executed at least once, even if the condition is false to begin with.
Java: | do {
// My code to execute...
} while (condition); |
Note: I'm honestly not sure if that last semi-colon should be there or not. Get rid of it if the compiler throws a fit over it. |
|
|
|
|
 |
ericfourfour
|
Posted: Wed Apr 04, 2007 8:33 pm Post subject: RE:if statements and loop |
|
|
Or if you want some simple infinite loops:
Java: | while (true) {
}
do {
} while (true)
for (;;) {
} |
|
|
|
|
|
 |
iluvchairs112
|
Posted: Thu Apr 05, 2007 5:46 am Post subject: RE:if statements and loop |
|
|
is there something like a while else statement? i know there is if else and i know there is while ... |
|
|
|
|
 |
richcash
|
Posted: Thu Apr 05, 2007 9:19 am Post subject: Re: if statements and loop |
|
|
Can't you do this?
Java: | while (cond) {
//do whatever
if (! cond & someOtherCond) {
//do whatever - this is like an elsif
}
if (! cond) {
//do whatever - this is like an else
}
} |
where cond and someOtherCond are obviously boolean expressions |
|
|
|
|
 |
klopyrev
|
Posted: Thu Apr 05, 2007 1:40 pm Post subject: Re: if statements and loop |
|
|
I don't get what you are trying to do. Explain more clearly.
You are either trying to do this:
code: |
while(true)
{
while(A)
{
}
while(B)
{
}
while(C)
{
}
}
|
OR
code: |
while(true)
{
if(A)
{
}
else if(B)
{
}
else if(C)
{
}
}
|
But then again, I don't really understand what you are trying to do.
KL |
|
|
|
|
 |
FileFantasy
|
Posted: Fri Apr 06, 2007 9:22 pm Post subject: Re: if statements and loop |
|
|
Don't really know what you mean, but from what you described, this is what I got:
code: |
boolean allUntrue = true;
while (allUntrue)
{
if (/*first statement is true*/)
{
//do something
allUntrue = false;
}
else if (/*second statement is true*/)
{
//do something
allUntrue = false;
}
else if (/*third statement is true*/)
{
//do something
allUntrue = false;
}
}
|
However, I don't know how the statements will become true if they're not true the first time around. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
ericfourfour
|
Posted: Fri Apr 06, 2007 11:48 pm Post subject: RE:if statements and loop |
|
|
Using the allUntrue variable is pretty tedius. A simple break; will end the loop. |
|
|
|
|
 |
FileFantasy
|
Posted: Sun Apr 08, 2007 7:30 pm Post subject: Re: if statements and loop |
|
|
That's true, and this is what ericfourfour means:
code: |
while (true)
{
if (/*first statement is true*/)
{
//do something
break;
}
else if (/*second statement is true*/)
{
//do something
break;
}
else if (/*third statement is true*/)
{
//do something
break;
}
}
|
|
|
|
|
|
 |
|