Step 6

Creating the spamWord function


Next, add the code inside the “WordSpammer” class as shown to create the function called “spamWord.”

This is a function that takes two parameters a String and an int. Remember parameters are things the function needs to do its job.

Here, this function needs to know what String/word it needs to spam. It also needs to know how many times to spam that word, which is why it needs these two parameters.

Lastly, we have a variable called “x”, you’ll see how this is used next.

The while loop


Next, add the code inside the “spamWord” function as shown. This is a new programming tool called the while loop.

At first glance, it looks very similar to the if statement and that’s because it is!

Just like the if statement there is a question/condition that gets checked. Again, if it is True the code inside the while loop runs, if it is False it gets skipped.

However, with a while loop, once the code is ran, it goes back to the top of the loop and asks the question again. The same thing happens, if it’s True it runs, if not, it gets skipped.

With a while loop this will keep happening until the condition is False. In this example, the loop will stop once “x” becomes equal to “timesToSpam”, meaning that this loop will print out our String the number of times we told it to!

Also, on line 11 we see “x++.” This is the same as writing “x = x + 1.” This is just a shortuct that programmers use, but we will be seeing this more often from now on.