

This sets up the conditional logic we need for this game: if those conditions are true, we will execute some code written inside the IF statement. The first step in that replacement process is checking each number and seeing if it’s divisible by 3, or 5, or both: myNumber = 0

That’s a good start, but now we need to replace some of those numbers, specifically the ones that are divisible by 3, or 5, or both. If we run this code as is, on our screen, we’ll get a list of all the numbers from 1 to 100.

That means we need to output each one : myNumber = 0 Now we’re going through each number, but the assignment says we need to actually show each number. This will continue counting up until we reach 100. WHILE myNumber is less than or equal to 100 In this loop, we’ll start the first number, 0, then add 1 to get to the next, like so, all the way until we’ve reached 100: myNumber = 0 Or if we know how long we need to drive for, we could say, keep driving for 10 miles. If we’re driving down a street, a loop construct might be thought of as, while my car has gas, keep driving. The loop is a way that we can do something again and again, for a defined number of times or as long as we need to. In order to know if we should write “Fizz” or “Buzz”, we have to go through every number in that range.
How to write c code within ruby how to#
Then we have to think about how to actually accomplish this with code. I decided to write a script that looked at numbers 0 to 100 and apply the rules of the game to that range. Besides being important to learn if it ever turns up in an interview, it’s also a really good way to learn some basic programming concepts, such as the all important loop and conditional logic, which are a huge part of programming.įirst, we pick a range of numbers to try this with. Finally if the number is divisible by both 3 and 5, output "FizzBuzz". The idea is to list a range of numbers, and if the number is divisible by 3 output "Fizz", or if the number is divisible by 5 output "Buzz". FizzBuzz is a simple game, often used in interview questions.
