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

Top-Down Design for Nested Loops

Flowchart for Loops and If Statements

for Nested Loops flowchart

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright (c) 2020 Mr. Coxall All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program is the Fizz Buzz program

#include <stdio.h>

int main() {
    // this function is the Fizz Buzz program
    for (int counter = 1; counter <= 100; counter++) {
        if (counter % 3 == 0 && counter % 5 == 0) {
            printf("Fizz-Buzz\n");
        } else if (counter % 3 == 0) {
            printf("Fizz\n");
        } else if (counter % 5 == 0) {
            printf("Buzz\n");
        } else {
            printf("%d\n", counter);
        }
    }

    printf("\nDone.\n");
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright (c) 2020 St. Mother Teresa HS All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program is the Fizz Buzz program

#include <iostream>

int main() {
    // this function is the Fizz Buzz program
    for (int counter = 1; counter <= 100; counter++) {
        if (counter % 3 == 0 && counter % 5 == 0) {
            std::cout << "Fizz-Buzz" << std::endl;
        } else if (counter % 3 == 0) {
            std::cout << "Fizz" << std::endl;
        } else if (counter % 5 == 0) {
            std::cout << "Buzz" << std::endl;
        }  else {
            std::cout << counter << std::endl;
        }
    }

    std::cout << "\nDone." << std::endl;
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program is the Fizz Buzz program
*/

using System;

/*
 * The Program class
*/
class Program {
    static void Main() {
        // this function is the Fizz Buzz program
        for (int counter = 1; counter <= 100; counter++) {
            if (counter % 3 == 0 && counter % 5 == 0) {
                Console.WriteLine("Fizz-Buzz");
            } else if (counter % 3 == 0) {
                Console.WriteLine("Fizz");
            } else if (counter % 5 == 0) {
                Console.WriteLine("Buzz");
            } else {
                Console.WriteLine(counter);
            }
        }

        Console.WriteLine("\nDone.");
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program is the Fizz Buzz program
 */

package main

import (
	"fmt"
)

func main() {
	// this function is the Fizz Buzz program

	// process & output
	for counter := 1; counter <= 100; counter++ {
		switch {
		case counter%3 == 0 && counter%5 == 0:
			fmt.Println("Fizz-Buzz")
		case counter%3 == 0:
			fmt.Println("Fizz")
		case counter%5 == 0:
			fmt.Println("Buzz")
		default:
			fmt.Println(counter)
		}
	}
	fmt.Println("\nDone.")
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * This program is the Fizz Buzz program
 *
 * @author  Mr Coxall
 * @version 1.0
 * @since   2020-09-01
 */

import java.util.Scanner;

public class Main {
  /**
   * This function handles all the inputs and outputs.
   *
   * @param args
   */
  public static void main(String[] args) {
    // this function is the Fizz Buzz program

    // process & output
    for (int counter = 1; counter <= 100; counter++) {
      if (counter % 3 == 0 && counter % 5 == 0) {
        System.out.println("Fizz-Buzz");
      } else if (counter % 3 == 0) {
        System.out.println("Fizz");
      } else if (counter % 5 == 0) {
        System.out.println("Buzz");
      } else {
        System.out.println(counter);
      }
    }
    System.out.println("\nDone.");
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/* Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program is the Fizz Buzz program
 */

// process & output
for (let counter = 1; counter <= 100; counter++) {
  if (counter % 3 === 0 && counter % 5 === 0) {
    console.log("Fizz-Buzz");
  } else if (counter % 3 === 0) {
    console.log("Fizz");
  } else if (counter % 5 === 0) {
    console.log("Buzz");
  } else {
    console.log(counter);
  }
}

console.log("\nDone.");
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
"""
Created by: Mr. Coxall
Created on: Sep 2020
This module is the Fizz Buzz program
"""

import time


def main() -> None:
    """The main() function is the Fizz Buzz program, returns None."""

    # process & output
    for counter in range(1, 101):
        if counter % 3 == 0 and counter % 5 == 0:
            print("Fizz-Buzz")
        elif counter % 3 == 0:
            print("Fizz")
        elif counter % 5 == 0:
            print("Buzz")
        else:
            print(counter)
        time.sleep(0.25)

    print("\nDone.")


if __name__ == "__main__":
    main()

Example Output

Code example output