Get every 100th value in a loop

Is there a way to make this cleaner and not use tempvalue like I did here?


UPDATE, the code had a logical error and did not show what I was doing. This is what I do:

var loopTempValue = noOfPackets / 100; for(i=0; i < noOfPackets; i++) { \\DoStuff if (i == loopTempValue) { loopTempValue = loopTempValue + (noOfPackets / 100); UploadBackGroundWorker.ReportProgress(pross); } } 

UPDATE Final

This is how it is fixed after feedback, thanks guys.

 if (i % (noOfPackets / 100) == 0 && i != 0) { UploadBackGroundWorker.ReportProgress(pross); } 
+7
source share
3 answers
 if (i % 100 == 0 && i != 0) { //YOUR CODE } 

The module is great for such checks.

More information about the module - http://www.vias.org/cppcourse/chap04_01.html

UPDATE: I added && i != 0 for case 0, which is true.

If you want to use tempvalue instead of hardcoding 100, this will be the solution:

 if (i % tempvalue == 0 && i != 0) { //YOUR CODE } 
+19
source

You want to say that you want the condition to fire 100 times during the cycle, i.e. every 36th iteration? (In your source code, you double the tempvalue each time, so it will only run seven times during the loop.)

You can use the modulo operator to verify this. This will trigger a condition in the last of each set of 36 iterations:

 for(i=0; i < 3600; i++) { \\DoStuff if(i % 36 == 35) { \\DoStuff } } 
+5
source
 if( (i+1 % (tempvalue+1) == 0) { //DoStuff //tempvaule = tempvalue + tempvalue; } 
+1
source

All Articles