How can I fix the problem I'm having with break in my code?

  • Thread starter Thread starter VirObitus
  • Start date Start date
  • Tags Tags
    Break
Click For Summary

Discussion Overview

The discussion revolves around a coding problem related to the use of the 'break' statement within a loop that is intended to compare integer traits against minimum and maximum values. Participants explore potential issues in the code, suggest corrections, and discuss alternative approaches to avoid using 'break'. The scope includes technical explanations and debugging strategies.

Discussion Character

  • Technical explanation
  • Debugging
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the behavior of the 'break' statement and its impact on the loop's execution.
  • Another participant points out a semicolon at the end of the for-loop declaration, suggesting it causes the loop to terminate prematurely.
  • A participant acknowledges the semicolon mistake but notes that the core problem persists, as the code still always selects the first choice.
  • Concerns are raised about variable naming consistency, with a suggestion that misspellings could lead to errors, although one participant insists the variables are correctly declared in the full source.
  • One participant shares a revised version of the code that still uses 'break' and seeks alternatives to avoid it, mentioning an infinite loop issue with a proposed modification.
  • Another participant suggests using a debugger to trace the code execution for troubleshooting.
  • Questions arise about the initialization of 'traitCount' and its purpose in the context of the code.
  • Participants discuss the implications of reading from a file and how it relates to the loop's operation, clarifying that 'traitCount' is used to determine when to stop processing.
  • Suggestions are made to replace the for loop with a while loop that incorporates the success condition to avoid using 'break'.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the code and explore alternatives to 'break', but multiple competing views remain regarding the best approach to achieve this. The discussion does not reach a consensus on a definitive solution.

Contextual Notes

Limitations include potential misunderstandings about variable names and the specific behavior of the loop constructs. The discussion also highlights the complexity of debugging and the need for clarity in code structure.

VirObitus
Messages
19
Reaction score
0
Hi again,

I *think* I am having a problem with break, as I can't figure out why I am having the problem I am having. This code is meant to compare 24 integer 'traits' against 48 minimum and maximum values, and if all 24 fit within their respective min and max value, to exit the loop. If the response (saythis) fails even one of the min/max tests, it should pick a new saythis. Right now it does the same thing every time - throws out exactly 1 saythis, and chooses the second one.

I've also been told to avoid using break, as it is bad coding style (or something), so if there is another way to accomplish this, I would love to see it.

Code:
int success = -1;
int traitCount = 24;
int ResponseCount = 120;
int TraitMatrix[120][24]; // if this array were graphed, all the odd numbers would be min, and all the even ones would be max
int a = 0;
int b = 1;

     while (success < 1)
     {              
         saythis = rand()%responseCount;     //creates a random number between 0 and responseCount and assigns it to saythis

         printf("first saythis is: %d\n\n", saythis);  //debug - see each random number as it is selected
                     
         for (i = 0; i < traitCount; i++);
         {
             if (success == 0)
             {
                break;
             }
             testMin = traitMatrix[saythis][a];  //sets the minimum possible value of the trait
             testMax = traitMatrix[saythis][b]; //sets the maximum possible value of the trait
                    
             if (traits[i] >= testMin && traits[i] <= testMax) //tests the NPC's actual trait against the min and max value
             {
                a = a+2;
                b = b+2;
                success = 1;
             } 
             else
             {
                 success = 0;
             }
         }
     }
 
Last edited:
Technology news on Phys.org
Why do you have a semicolon at the end of the for-loop line? That doesn't seem right.

Code:
for (i = 0; i < traitCount; i++);
{
   ...
}

That semicolon terminates the for-block; the following block is executed only once, after the empty for-loop terminates.

If you fix that, you can get rid of the 'break' by combing your break test with the conditional in the for loop:

Code:
for (i = 0; (i < traitCount) && (success != 0); i++)
{
   testMin = ...
 
Last edited:
Aha, thank you for pointing that out... stupid mistake. The success test in the for loop is a great idea, I didn't even think of that, thanks.

However, the problem still remains... except now it just always selects the first choice.
 
Last edited:
What's the full source file? You've declared all of the variables you are using, right? Your arrays have meaningful numbers stored in them, right?

There's a couple more probable errors: you misspell several variables. You've declared 'ResponseCount' but use 'responseCount', and ditto with 'TraitMatrix'. Hope this helps.
 
Last edited:
Nah, the problem isn't the variables, that would cause a compile error (that was just me not paying attention when i posted them) The full source is about 500 lines, so I would rather not post the entire thing. I've troubleshot(sp?) the rest of the program, and it compiles and run fine, and the arrays are declared and assigned correctly, the problem is with testing the min/max array against the traits, so it has to be in this bit of code I've posted...
 
Last edited:
Ok, I had a hell of a time troubleshooting this thing, and if anyone is interested, this is how I finally got it to work. However, it still uses break, which I am told is 'bad programming', so if anyone can show me a way to avoid it I would be grateful. I tried " for (i = 0; i < traitCount || success != 0; i++) " but it causes an infinite loop. Maybe I am doing that wrong? Anyway, here is the code.

Code:
success = 2;

     while (success != 1)
     {
         a = 0;
         b = 1;
         saythis = rand()%responseCount;                                           //creates a random number between 0 and loopCount2 and assigns it saythis
         printf("first saythis is: %d\n\n", saythis);                         //debug***  see each random number as its selected -
                     
         for (i = 0; i < traitCount; i++)
         {
             
             //printf("success is: %d\n\n", success);
             testMin = traitMatrix[saythis][a];                                    //sets the minimum possible value of the trait
             testMax = traitMatrix[saythis][b];                                    //sets the maximum possible value of the trait
                                                 
             //printf("testMin is: %d\n", testMin);
            //printf("testMax is: %d\n", testMax);
             
             if (traits[i] >= testMin && traits[i] <= testMax)             //tests the NPC's actual trait against the min and max value
             {
                a = a+2;
                b = b+2;
                success = 1;
             }
             else
             {
                 success = 0;
             }
             
             if (success == 0)
             {
                 break;
             }
         }
     }

Edit: As a side note, the reason my OP code was not working (besides the semicolon mistake) was that success == 0 was tested first, thus causing an infinite loop if success ever did == 0. Live and learn.
 
Can't you debug it with a debugger to see exactly what it is doing?
 
Where is traitcount initialized? And to what?
 
As I said above, the last code I posted is working correctly, no need to debug it (unless your talking about the for loop?). Just curious if there is a way to do that without using break. traitCount is set by reading a file, and is set to the number of lines in the file.
 
  • #10
Good!

In place of your 'break', you could write..

i = traitCount;
 
  • #11
Whoops, I said something stupid. Yeah, that looks as if it should work
 
  • #12
VirObitus said:
As I said above, the last code I posted is working correctly, no need to debug it (unless your talking about the for loop?). Just curious if there is a way to do that without using break. traitCount is set by reading a file, and is set to the number of lines in the file.

So:
- you're reading from a file
- you're using a loop to perform some function(s) on each line
- you don't know how many lines there are in the files, at least until you reach the end
- once you reach the end you want to break out of the loop

This is a textbook case for a while (!myfile.eof){} loop.
 
  • #13
Not really Dave. I've already finished reading the file by the time this function is called, its just testing the results of the file that it read against another array. traitCount just holds the number of entries in the file, so that the program knows when to stop. It doesn't manipulate the file at all, the file just tells it values to check against other values.
 
  • #14
If you want to really avoid break, then change the for loop into while loop with the success condition. Or if you want to really piss off purists, you can set i to traitcount right after success=1. that would teach them a lesson.ah someone else suggested that already.
 
Last edited:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
18
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K