Loops and If Statements
As you can probably guess by 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:
FOR counter in range(n)
IF (Boolean expression) THEN
Statements to be performed
ENDIF
END
or using While loops:
WHILE counter1 <= n :
IF (Boolean expression) THEN
Statements to be performed
ENDIF
…
counter1 = counter1 + 1
END
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 (1 to 100). Print ‘Fizz’ for every number that’s divisible by 3, print ‘Buzz’ for every number divisible by 5, and print ‘Fizz-Buzz’ for every number divisible by 3 and by 5! If the number is not divisible either by 3 or 5, print out the number.
Top-Down Design for Loops and If Statements

Flowchart for Loops and If Statements

Pseudocode for Loops and If Statements
FOR counter1 in range(10)
FOR counter2 in range(10)
SHOW Odometer {counter1}{counter2}
END
END
Code for the Loops and If Statements
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program is the Fizz Buzz program
6
7#include <stdio.h>
8
9int main() {
10 // this function is the Fizz Buzz program
11 for (int counter = 1; counter <= 100; counter++) {
12 if (counter % 3 == 0 && counter % 5 == 0) {
13 printf("Fizz-Buzz\n");
14 } else if (counter % 3 == 0) {
15 printf("Fizz\n");
16 } else if (counter % 5 == 0) {
17 printf("Buzz\n");
18 } else {
19 printf("%d\n", counter);
20 }
21 }
22
23 printf("\nDone.\n");
24 return 0;
25}
1// Copyright (c) 2020 St. Mother Teresa HS All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program is the Fizz Buzz program
6
7#include <iostream>
8
9int main() {
10 // this function is the Fizz Buzz program
11 for (int counter = 1; counter <= 100; counter++) {
12 if (counter % 3 == 0 && counter % 5 == 0) {
13 std::cout << "Fizz-Buzz" << std::endl;
14 } else if (counter % 3 == 0) {
15 std::cout << "Fizz" << std::endl;
16 } else if (counter % 5 == 0) {
17 std::cout << "Buzz" << std::endl;
18 } else {
19 std::cout << counter << std::endl;
20 }
21 }
22
23 std::cout << "\nDone." << std::endl;
24 return 0;
25}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program is the Fizz Buzz program
4*/
5
6using System;
7
8/*
9 * The Program class
10*/
11class Program {
12 static void Main() {
13 // this function is the Fizz Buzz program
14 for (int counter = 1; counter <= 100; counter++) {
15 if (counter % 3 == 0 && counter % 5 == 0) {
16 Console.WriteLine("Fizz-Buzz");
17 } else if (counter % 3 == 0) {
18 Console.WriteLine("Fizz");
19 } else if (counter % 5 == 0) {
20 Console.WriteLine("Buzz");
21 } else {
22 Console.WriteLine(counter);
23 }
24 }
25
26 Console.WriteLine("\nDone.");
27 }
28}
1/**
2 * Created by: Mr. Coxall
3 * Created on: Sep 2020
4 * This program is the Fizz Buzz program
5 */
6
7package main
8
9import (
10 "fmt"
11)
12
13func main() {
14 // this function is the Fizz Buzz program
15
16 // process & output
17 for counter := 1; counter <= 100; counter++ {
18 switch {
19 case counter%3 == 0 && counter%5 == 0:
20 fmt.Println("Fizz-Buzz")
21 case counter%3 == 0:
22 fmt.Println("Fizz")
23 case counter%5 == 0:
24 fmt.Println("Buzz")
25 default:
26 fmt.Println(counter)
27 }
28 }
29 fmt.Println("\nDone.")
30}
1/*
2 * This program is the Fizz Buzz program
3 *
4 * @author Mr Coxall
5 * @version 1.0
6 * @since 2020-09-01
7 */
8
9import java.util.Scanner;
10
11public class Main {
12 /**
13 * This function handles all the inputs and outputs.
14 *
15 * @param args
16 */
17 public static void main(String[] args) {
18 // this function is the Fizz Buzz program
19
20 // process & output
21 for (int counter = 1; counter <= 100; counter++) {
22 if (counter % 3 == 0 && counter % 5 == 0) {
23 System.out.println("Fizz-Buzz");
24 } else if (counter % 3 == 0) {
25 System.out.println("Fizz");
26 } else if (counter % 5 == 0) {
27 System.out.println("Buzz");
28 } else {
29 System.out.println(counter);
30 }
31 }
32 System.out.println("\nDone.");
33 }
34}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program is the Fizz Buzz program
4 */
5
6// process & output
7for (let counter = 1; counter <= 100; counter++) {
8 if (counter % 3 === 0 && counter % 5 === 0) {
9 console.log("Fizz-Buzz");
10 } else if (counter % 3 === 0) {
11 console.log("Fizz");
12 } else if (counter % 5 === 0) {
13 console.log("Buzz");
14 } else {
15 console.log(counter);
16 }
17}
18
19console.log("\nDone.");
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module is the Fizz Buzz program
6"""
7
8import time
9
10
11def main() -> None:
12 """The main() function is the Fizz Buzz program, returns None."""
13
14 # process & output
15 for counter in range(1, 101):
16 if counter % 3 == 0 and counter % 5 == 0:
17 print("Fizz-Buzz")
18 elif counter % 3 == 0:
19 print("Fizz")
20 elif counter % 5 == 0:
21 print("Buzz")
22 else:
23 print(counter)
24 time.sleep(0.25)
25
26 print("\nDone.")
27
28
29if __name__ == "__main__":
30 main()
Example Output
