Following examples and threads of previous inquiries I find myself asking two primary things

1. Why when I place the following code -

Break;//break the string

at the end of my while statement that reads

while(!name.toLowerCase().equals("stop")); //begin while control statement
end = true; //end if condition is true, counter initiated to -1

do I receive the error message that break is outside of switch or loop? Where else might I place this so it would be within the loop?

Secondarily, I am being told that the following statement is an empty statement. Can someone explain what this signifies?

while (end == false);//condition is false, proceed

the while loop finishes at the first semi-colon, unless you put your statements between brackets.

so

while ( condition );
loopThis;

loopThis; is outside the while, because you've placed a semi-colon right after the while condition

the same with

while (condition );
{
loopThis;
}

loopThis; is still outside the while, since there is a semi-colon before the brackets.

while ( condition )
loopThis;
loopThisToo;

here, loopThis; is in the while, loopThisToo; not, since there is a semi-colon before that.

while ( condition ){
loopThis;
loopThisToo;
}

here both are in the while loop, since they're encapsulated in brackets that follow immediate to the while condition

Secondarily, I am being told that the following statement is an empty statement. Can someone explain what this signifies?

while (end == false);//condition is false, proceed

as I explained earlier, the while ends at the first semicolon, so whether or not end is false or not, the loop is ended and what ever comes after that statement will be executed, since the compiler does not consider it to be conditional.

next, if you want to compare a boolean value to 'true' or 'false', you can write the condition shorter:

while ( end = true)

is identical to

while ( end )

and

while ( end = false)

is identical to

while ( !end )

Edited 6 Years Ago by stultuske: answer to second question

You aren't declaring your while loop properly because of the ';'
For examples of how to correctly use while loops, use google, but for any type of loop, you need to put brackets around what you want to happen. Quick example for you

//acceptable
while(someVariable == false){
 //do stuff here!!!
}

Oh, and it should be 'break' not 'Break'.

Edited 6 Years Ago by BestJewSinceJC: n/a

This article has been dead for over six months. Start a new discussion instead.