Else Statement in While Loops

Can anyone tell me of an else statement is compatible with a while loop in openHab? In Python, it would execute when the while condition became false, ensuring it always runs when the loop exits. Sometimes along the lines of:

while(sun.state == ON){
    someRandomCode()
}else{
    sendCommand(light, ON)
       

Thanks.

I’m not sure if you can use while with else statement, but you could make an infinite while loop (while 1=1 for example), and use break when if condition is satisfied.

Beat regards,
Davor

I think I figured it out.

try{
    while(sun.state == ON){
        someRandomCode()
    }
}
finally{
    sendCommand(light, ON)
}

Isn’t this the same as:

while(sun.state == ON) {
    someRandomCode()
}
sendCommand(light, ON)

try and finally are used in exception handling, which you are not trying to do.

Ah, you are correct. I had some other buggy things going on that made me think it was behaving differently.