wingyip
wingyip

Reputation: 3536

More succinct code example

Is there a more succinct way of writing this?

articleCount += 1;
if (articleCount > 4)
{
    // Reset counter to 1
    articleCount = 1;
}

Upvotes: 0

Views: 111

Answers (2)

Glorfindel
Glorfindel

Reputation: 22631

For those who like mathematical puzzles unreadable code, the following would work as well:

articleCount = (articleCount % 4) + 1;

Upvotes: 6

Roy Dictus
Roy Dictus

Reputation: 33139

Yes:

if (++articleCount > 4) articleCount = 1;

Upvotes: 6

Related Questions