π
Iterator Concept
An iterator is a pattern used to perform repetitive tasks systematically, ensuring that each item in a dataset is visited exactly once. This involves several key steps:
Initialization
Set up the context for the iterator to function. This might involve arranging items in a specific order or making them accessible in a certain way.
- For example, when working with cards, this could mean arranging all cards in a single pile.
- For a dataset of words in a paragraph, the words need to be in the same sequence as they appear in the paragraph.
Picking and Examining One Data Element
Select one item from the dataset to work on. This could be the top card from a pile, a row from a table, or any element from an unordered set.
The selected item is then examined, and specific fields or values are processed. These values might be combined with other values or written down for further use.
Marking the Data Element as Seen
After examining the chosen element, it is moved to a "seen" pile to ensure it is not re-examined.
- This involves maintaining two piles: the original "unseen" pile and the "seen" pile. This ensures that all items are visited without repetition.
- In tabular form, this means moving a row from the "unseen" table to the "seen" table.
Stopping
The iteration continues until there are no more items left to examine. This naturally happens when the "unseen" pile is empty.
In tabular form, this occurs when the table runs out of rows.
Steps of the Iterator
- Initialization Step: Arrange all items in an "unseen" pile.
- Continue or Exit: Check if there are any items left in the "unseen" pile. If not, exit; otherwise, continue.
- Repeat Step: Pick an item from the "unseen" pile, process it, and move it to the "seen" pile.
- Repetition: Go back to Step 2 and repeat the process until all items are processed.
Additional Notes
- The step that checks whether to continue is placed before picking an item to handle cases where the dataset might be empty initially.
- The repetition is driven by Step 4, which directs the process back to Step 2, ensuring continuous iteration until completion.
Summary of Iterators and Flowcharts
Flowcharts
Flowcharts provide a visual representation of step-wise processes, making it easier to understand and communicate procedures. They are particularly useful for explaining protocols or procedures to others, such as customers or business partners.
Main Symbols Used in Flowcharts
We primarily use four symbols in flowcharts:
Terminal Symbol
Denotes the start or end of the flowchart.
Process Symbol
Represents any activity that needs to be performed.
Decision Symbol
Checks a condition and branches based on true/false.
Arrow
Shows progress from one step to another.
The Generic Iterator Flowchart
The generic iterator flowchart uses the symbols mentioned above to represent the iteration process. It starts with initialization, followed by a decision to continue or stop the iteration. If the iteration continues, it repeats the steps and returns to the decision box.
Iterator Flowchart for Cards
This flowchart is a specific example of the generic iterator, applied to a set of cards. It involves arranging cards in an "unseen" pile and moving them to a "seen" pile after examination. The decision step checks if there are more cards in the "unseen" pile.
Here are some example.
One thing to be notes is in computational thinking == means equality(we are checking values), = means we have assigned the value, fixed the value.
Try to form flowchart of following pseudocode.
Some examples on flowchart:
PA:
GA:
Comments
Post a Comment