Hey there! Today we are going to explore how programmers organize data using lists, and how we can use a software design process to solve real-world problems with loops!
A list, or an array, is like a row of labeled boxes. It lets us store many items together in one single place under one name.
To solve big coding problems, we use a software design process. We break the main problem into smaller subproblems, like creating a list first, and then using nested loops to search through it.
A nested loop is simply a loop inside another loop. The outer loop runs once, and then the inner loop runs completely before the outer loop moves to its next turn.
We want to design a program that prints a weekly schedule. It must print 'Day 1' through 'Day 3', and for each day, print 'Activity A' and 'Activity B'. Let's explain and design this using nested loops!
- Identify the subproblems: Subproblem 1 is tracking the days. Subproblem 2 is listing the activities for each day.
- Write the outer loop to repeat 3 times, once for each day (Day 1 to Day 3).
- Write the inner loop inside the outer loop to run 2 times, once for each activity (A and B).
- Trace the code: When Day is 1, the inner loop runs for Activity A, then Activity B. Then Day becomes 2, and the activities run again!
- Combine the loops into a text-based program to print the complete schedule.