In today’s lessons, we learned about what happens when you need two conditions.
I learned code to count from 1 to 20 and then to add an if/else statement so that each number divisible by 3 is changed to “Fizz”
Remember that semicolons are terminators and basically they only go on the end of statements inside curly mustache brackets { and }
Also, a lesson in the unwritten code of programming conduct, i is typically used when you want to use a variable to count things. In this case we want to use i because the FizzBuzz exercise calls for us to count from 1 – 20.
We learned how to set conditionals in the last lesson so here we go
for (a=1; a<=20; a++)
As a reminder, the first conditional (remember each conditional is seperated by a terminator, the god damn semicolon) means our counter starts at 1, then a<=20 means that whatever happens WHEN our statement is true, it will ONLY happen when a is less than 20 or equal to 20. After that, a++ means that each time our statement is true, we add 1 to a which we started at 1.
Because we need 3 things to happen
Fizz when number is divisible by 3
Buzz when number is divisible by 5
FizzBuzz when number is divisible by 15
We need some way to set up 3 conditions after we state our for statement.
Because program runs code in the order it’s written, we have to make sure that we put the FizzBuzz when numbers are divisible by 15 first.
This is because if we don’t, our code will print out Fizz or Buzz whenever anything is divisible by 15. Why? Because 15 is divisible by both 3 and 5.
We learned that 2 conditions call for if/else. When we need a third condition, we put it in between if and else calling it else if. So our code would call for
if (random condition)
{ (what happens during if if statement is true);}
else if (random condition)
{ (what happens during else if statement is true);}
else
{ (what happens if neither if or the else if statement is true);}
