Loops and If Statements¶
As you can probably guess from now, yes you can place loops inside if statements and if statements inside loops.
An if statement inside a loop would (in most computer programming languages) take the generic form of:
or using While loops:
Here is one of the most well-known examples of the exercises that you might be given as the opening question in a junior data scientist job interview.
The task is: Go through all the whole numbers up until 100. Print ‘fizz’ for every number that’s divisible by 3, print ‘buzz’ for every number divisible by 5, and print ‘fizzbuzz’ for every number divisible by 3 and by 5! If the number is not divisible either by 3 or 5, print a dash (‘-‘)!
In a flow chart it looks like:
The following code snippet is the solution to the above problem: