At the end of this lesson, you will be able to:
A2.3 write algorithms with nested structures (e.g., to count elements in an array, calculate a total, find highest or lowest value, or perform a linear search).
understand and use nested loops and continue statements inside each other
With a pencil and paper, answer the following questions:
What is the purpose of a nested loop?
Create a flowchart that displays the multiplication times tables from 0 to 3 with all numbers from 0 to 5. For example, the output is displayed to the right.
Write the code in Python for your program above.
Write the code in C++ for your program above.
nested loops
from the online book Computer Based Problem Solving by Patrick Coxall, read:
loops can be placed inside if statements
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 is divisible by 3. Print 'buzz' for every number divisible by 5. Print 'fizzbuzz' for every number divisible by 3 and 5. If the number is not divisible by 3 or 5, print a dash (-).
Here is what the flowchart might look like:
Create a program that, using one for loop and one if statement, prints the integers from 1000 to 2000, outputting five integers per line with each integer separated by a space.
Hint:
use the % (modulus) operator
in Python3 to keep the cursor on the same line use:
print("Bob", end = "")
in groups of 2, do the following on the board for today's daily assignment:
Top-Down Design
Flow Chart
Pseudocode
Test Cases
complete the Daily Assignment section in Hãpara Workspace for this day
if Hãpara is not working, make a copy of this document
move it to your IMH-ICS folder for this course
recreate the same program in C++
Note: in C++, std::endl gives you a carriage return