Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualAllEightUp

Posted 29 April 2013 - 01:02 AM

Some practical everyday uses:

 

// I want to trigger something every 7 executions:

static int count = 0;

count = ++count % 7;

if( !count ) {do something}

 

// I want to cycle 0-6 repeatedly.

static int count = 0;

count = ++count % 7;

 

In the first case the 'if' relies on the fact that the only "true" condition is when something is non-zero.  In the second case a value modded by any higher value results in the same value, it goes to zero when modded with itself.  Basically a variable in both cases is going to step from 0 to mod value minus one, or 0-6 in this case and keep repeating.

 

I think I use the first case with the 'if' statement more often but I know I use the second case on occasion also.


#2AllEightUp

Posted 29 April 2013 - 01:00 AM

Some practical everyday uses:

 

// I want to trigger something every 7 executions:

static int count = 0;

count = ++count % 7;

if( count ) {do something}

 

// I want to cycle 0-6 repeatedly.

static int count = 0;

count = ++count % 7;

 

In the first case the 'if' relies on the fact that the only "true" condition is when something is non-zero.  In the second case a value modded by any higher value results in the same value, it goes to zero when modded with itself.  Basically a variable in both cases is going to step from 0 to mod value minus one, or 0-6 in this case and keep repeating.

 

I think I use the first case with the 'if' statement more often but I know I use the second case on occasion also.


#1AllEightUp

Posted 29 April 2013 - 12:13 AM

Some practical everyday uses:

 

// I want to trigger something every 7 executions:

static int count = 0;

count = ++count % 7;

if( count ) {do something}

 

// I want to cycle 0-6 repeatedly.

static int count = 0;

count = ++count % 7;

 

In the first case the 'if' relies on the fact that the only "true" condition is when something is zero.  In the second case a value modded by any higher value results in the same value, it goes to zero when modded with itself.  Basically a variable in both cases is going to step from 0 to mod value minus one, or 0-6 in this case and keep repeating.

 

I think I use the first case with the 'if' statement more often but I know I use the second case on occasion also.


PARTNERS