A common task you might be asked to do and learning for loops you started a for loop that prints out something called fizz buzz. And if you program four You've probably heard of it. If not what you want to do is you want to print out the numbers to in the con. And for every loops search for digital marketing company in Hyderabad for multiple of three you want to print out the word fizz instead of the actual number every multiple of five you want to print out the word buzz and for every multiple of both three and five you want to print the word buzz instead of the number.
Pros & Cons:
So we have the knowledge we need at this point to be able to do that. And if we want to do that it's pretty straightforward. We're just going to build it up slowly so that before and we'll do a lead here. Let my calls. We're going to start with one so we'll say it equals. And then you can for digital media agency what time do you want to break out of the loop. So when I or in other words loop until I am no longer less than or equal to and you want to be increment I buy one for every loop plus equals. Also written as a plus.
So initially just to kind of test it out let's console log out. I like so clear this will run it. So there we go we have printed out the numbers too. That's exactly what we want. You know what I'm going to get rid of invoking this function right now. So there we go. Now if we can figure out how to do this so it's pretty digital marketing company in Hyderabad straightforward really we just need to kind of do a couple of checks. So first of all if I remember the modulo operator I present three equals zero. And you forgot this is basically saying if the number is perfectly divisible by three if the number divided by three results in zero remainder this becomes true. This expression is true.
So if that's the case in this particular loop through a log something else instead. The console log is so. Now if we test so it's not going to have quite the implementation we want and if we are going to school up it will be easier to see. So here we have number three print those fears. And also just continues on and prints out the number anyway.
So we want to throw an else statement in there. So we'll see if this else console out the number so that should fix that issue we'll clear this or run it again. Scrolling up I can see that it did solve that issue. Right so is or is and we want this five now and every other multiple of five to print out the word buzz. And that's pretty straightforward and put this on its own line.
Purpose for Loops:
So we'll do it then we'll do it. Else if I. Percent present five equals zero. If it's perfectly divisible by five we're going to print out buzz instead let's try that out. OK. It appears to be working correctly. We've got our buzzing and digital marketing company in Hyderabad We get our fears and it just kind of continues down as many times as we need in this case it goes on for loops. Finally, let's do the multiple of three and five.
So we could do that up at the very top because we want to kind of catch that before any other action is applied. And what I mean by that is if we do it down here LCF and this is the couple we do for digital marketing company in Hyderabad this we could do I present three equals zero. And I present five equals zero console log fizz buzz that this won't have quite the implementation we need because we're not quite catching it in time right. So here we've already applied the logic and then it doesn't meet the if conditions. There's no fizz buzz as far as I can see anywhere here. So if we move this to the top though. Right and that's a large part in times of figuring out how the for loop works is where in your loop you need to put your logic you know if we cut this out.
Going to move it up here. And this one will be our initial if statement and then this one will now become LCF.
OK so let's try that out here appears to be working correctly. So multiple of three and five.
Looks like the lowest common denominator is near-EOP fizz buzz. And this should also apply.
Let's see and. Yes. So it does appear to be working and we can actually clean up the code a little bit if we realize that hey the lowest common denominator of and is.
So we can actually just clean this up a bit. So instead of doing this and this way we just check if it is preset equals zero. Then we will print out physios for digital marketing company in Hyderabad for loops in python.
Finally, the last thing we can do if it doesn't satisfy any of those other conditions is we'll just cancel out the number clear we run. And it does appear to be correct. We'll check here. Fifteen Yes. There you go for loops in python from digital marketing company in Hyderabad. So you've successfully written a for loop to print out this but for us. And it just needed we just needed some understanding of how the loop works how we can kind of check with some IF ELSE IF statements as well.