// Check every number from 0 to 1000 (not including 1000)
for(int i = 0; i < 1000; i++)
{
var isMultipleOf3isDivisibleBy3 = i % 3 == 0;
var isMultipleOf5isDivisibleBy5 = i % 5 == 0;
//If it is divisible by 3 or it is divisible by 5
if(isMultipleOf3isDivisibleBy3 || isMultipleOf5isDivisibleBy5)
{
//then add it to the sum
sum += i;
}
}
You don't need to use the booleans. I simply added them to simplify the example. if(i % 3 == 0 || i % 5 == 0)
would be equally okay to use because it's still reasonably readable.
If
If the calculations become more complex, I suggest always using the booleans so you neatly break your algorithm down to small and manageable steps. It will do wonders for your code readability, and it does not impact performance (the compiler will optimize this in a release build).