C programming question..

somethingwitty

Golden Member
Aug 1, 2000
1,420
1
0
hey all, hope I can get the usual quick answer here :)

I need to break a for statement using an if statement, and I'm wondering if this will work...I'd compile it now, but the code's not yet close enough to completion to go through the hassle.

for (j = 0; j < 15; j++)
{
if (locations[J] == RINGOS_HAND)
{
printf (&quot;You already have something in your hand.\n&quot;);
break;
}
else if...
(my else if leads me to the same question-so heres the code for the else-if)
else if (locations[j] == locations[15])
{
locations[j] = RINGOS_HAND;
same thing here-i'd need to break both the else if and the for statements...
}
else...

basically, I want to check each value in the array, and break the ENTIRE FOR STATEMENT if it does-but I'm doubting whethere the break I have will do it (i think it might only break the if), a break after it would (i think) be meaningless, and a break before the else if would definitely end the for statement, which, at that point, I would NOT want to do...any ideas?

one note: values in the array can repeat-so, for example, locations[0] can = locations [10] = Name of place...thats why i need to break the for statement

Thanks
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
I believe it will work...

If I remember correctly, break will only &quot;break&quot; loops (eg. while, for, etc... ) and not if statements...

The best way to find out for sure is try! :) insert some test printf statements and see what happens...
 

Turkey

Senior member
Jan 10, 2000
839
0
0
Looks like this could probably be better done with a while or do while loop.