Buzzfizz
FizzBuzz Challenge - Explaination
for (int i= 1; i<101; i++)
{
if
((i % 3 == 0) && (i % 5 == 0))
Console.WriteLine($"{i} -fizzbuzz");
else if
(i % 3 == 0)//When the current value is divisible by 3, print the term Fizz next to the number.
Console.WriteLine($"{i} -fizz");
else if
(i % 5 == 0)
Console.WriteLine($"{i} -buzz");
else
Console.WriteLine($"{i}");
}
for (int i= 1; i<101; i++)
This sets up the range of numbers to be evaluated.
((i % 3 == 0) && (i % 5 == 0))
This initial statement contains two conditional statements - the first checks if the integer is divisible by 3, the second wether the same integer is divisible by 5.
If both conditions are met Console.WriteLine($"{i} -fizzbuzz"); writes the output integer {i} concatenated with the text fizzbuzz.
The precedence is important - we have to check these two conditions together first - if we try to check the two individual conditions seperately beforehand the results will mask numbers where both apply
&& joins the two conditions, and wrapping the whole in parentheses( (i % 3 == 0) && (i % 5 == 0) ) ensures that both are checked simultaneously.
We can then progress to check the conditions seperately using "else if" statements.
Finally the "else" statement returns just the integers that are not divisible by either 3 or 5 with no text suffixed.