Reputation: 143
if(turn==2)
{
if(forward) /*then what to do if this if comes true for coming out of outer loop*/
if(columnHead>0)
{
columnHead--;
addr[columnHead] |=1<<(rowHead-1);
}
else
{
columnHead =7;
addr[columnHead] |=1<<(rowHead-1);
}
if(rowTail!=(rowHead-1))
{
addr[columnHead+columnSize] &=~(1<<rowTail);
columnSize++;
rowTail++;
}
else
{
addr[columnTail] &=~(1<<columnTail);
if(columnTail==0)columnTail=8;
columnTail--;
}
back=1;
}
I want to come out of outer if
loop if it satisfies condition if(forward)
Upvotes: 1
Views: 5476
Reputation: 34145
I guess just putting an else
in there will do what you want. Also, please put braces around big blocks - it makes the else
part unambiguous.
if(turn==2)
{
if(forward) {
if(columnHead>0)
{
columnHead--;
addr[columnHead] |=1<<(rowHead-1);
}
else
{
columnHead =7;
addr[columnHead] |=1<<(rowHead-1);
}
} else {
if(rowTail!=(rowHead-1))
{
addr[columnHead+columnSize] &=~(1<<rowTail);
columnSize++;
rowTail++;
}
else
{
addr[columnTail] &=~(1<<columnTail);
if(columnTail==0)columnTail=8;
columnTail--;
}
back=1;
}
}
Version after the author's comment:
if(turn==2 && !forward) {
if(rowTail!=(rowHead-1))
{
addr[columnHead+columnSize] &=~(1<<rowTail);
columnSize++;
rowTail++;
}
else
{
addr[columnTail] &=~(1<<columnTail);
if(columnTail==0)columnTail=8;
columnTail--;
}
back=1;
}
Upvotes: 1
Reputation: 19445
why go into the "if" in the first place.
change the outter if to:
if(turn==2 && forward) //not sure if your logic required `forward` or `!forward` you were ambiguous
{
//...
}
Upvotes: 0
Reputation: 2926
You can try giving the return 0;
or return something;
when you need to get out of the if
, before that u may want to put the entire code in a c syntax
function like-
(int)someFunction(int turn, int forward,
int columnHead, int rowHead, int rowTail, int columnTail)/*have all the arguments with proper types declaration*/{
//your code with return 0;
}.
Upvotes: 0
Reputation: 234655
A standard idiom is to use while(true){/*your code here*/ break; }
around the whole block and use break
statements as appropriate which will take you to the end of the while brace. Just remember to include the final break
or your program will loop.
Whatever you do, don't use a goto as they are considered very poor programming style.
Some folk, from the C days where while(true)
would issue a compile warning, use for(;;)
instead.
The odd thing is, thinking about this some more, you could use
do {
/*your code here, using break for premature exit*/
} while (false);
which doesn't need the final break
. Only I've never seen this in production code.
Upvotes: 2
Reputation: 3011
You should create function of this code, and return from the block where the condition becomes true.
Upvotes: 8
Reputation: 3015
You probably have to go the other direction, that means:
You don't want to check wether
if(forward)
is true, but instead you want to do if(!forward)
.
Though you'd need to check what type forward is. I guess it's an integer though.
Upvotes: 0