if statement inside while loop

Error message suggests you have a } too many.

And yes, you can also write it like that.