
-----------------------------------
iluvchairs112
Wed Apr 04, 2007 7:19 pm

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.

-----------------------------------
Geostigma
Wed Apr 04, 2007 7:44 pm

RE:if statements and loop
-----------------------------------
I don't know how java works but you need a else or elsif style command.

-----------------------------------
rdrake
Wed Apr 04, 2007 8:14 pm

Re: 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.Take a look at the while and do while loops.

While:

While the condition is true, the while loop happily repeats itself.
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.
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
Wed Apr 04, 2007 8:33 pm

RE:if statements and loop
-----------------------------------
Or if you want some simple infinite loops:

while (true) {
}

do {

} while (true)

for (;;) {

}

-----------------------------------
iluvchairs112
Thu Apr 05, 2007 5:46 am

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
Thu Apr 05, 2007 9:19 am

Re: if statements and loop
-----------------------------------
Can't you do this?
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
Thu Apr 05, 2007 1:40 pm

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:

while(true)
{
     while(A)
     {
     }

     while(B)
     {
     }

     while(C)
     {
     }
}


OR


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
Fri Apr 06, 2007 9:22 pm

Re: if statements and loop
-----------------------------------
Don't really know what you mean, but from what you described, this is what I got:


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.

-----------------------------------
ericfourfour
Fri Apr 06, 2007 11:48 pm

RE:if statements and loop
-----------------------------------
Using the allUntrue variable is pretty tedius. A simple break; will end the loop.

-----------------------------------
FileFantasy
Sun Apr 08, 2007 7:30 pm

Re: if statements and loop
-----------------------------------
That's true, and this is what ericfourfour means:


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; 
    } 
}

