Reputation: 50
This is my code:
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
break; //break out of while loop also
}
}
}
How should i do this please, sorry if my english is good,i still learning.
Upvotes: 0
Views: 168
Reputation: 1318
You can use goto
:
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
goto EndOfWhile; //break out of while loop also
}
}
}
EndOfWhile: (continue your code here)
Upvotes: 0
Reputation: 9093
My favorite approach to this sort of situation is to move the code into a separate routine and simply return from it when I need to break. Two loops is as much complexity as I like to include in a single routine anyway.
Upvotes: 0
Reputation: 14044
So as I understand you wanted to break 2 Loops on one condition. You can do the following
bool DirtyBool = true;
while(DirtyBool)
{
for(int x = 0; x < 10; x++)
{
StringArray[x] = new string();
if(isDead)
{
DirtyBool = false;
break; //break out of while loop also
}
}
}
Upvotes: 1
Reputation: 11740
create a function inline, and call it. use return from within the lambda.
var mt = () => {
while(true){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
return
}
}
}
}
mt();
Upvotes: 1
Reputation: 16277
Try below:
bool bKeepRunning = true;
while(bKeepRunning){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
bKeepRunning = false;
break;
}
}
}
Upvotes: 0
Reputation: 9571
Change your while loop to a variable, then set that variable to false (your isDead
variable for instance)
while(!isDead){
for(int x = 0; x < 10; x++){
StringArray[x] = new string();
if(isDead){
break; //break out of while loop also
}
}
}
That way, your break
will get you out of the for
loop, then having the isDead
set to true will stop the while
loop executing.
Upvotes: 3
Reputation: 475
You could create a bool for example:
bool leaveLoop;
And if isDead is true then set leaveLoop to true, in the while loop then check if the leaveLoop is true to break from it.
Upvotes: 0