Now that we’ve had some experiences in programming loops and arrays, let’s talk
ID: 3748241 • Letter: N
Question
Now that we’ve had some experiences in programming loops and arrays, let’s talk about one of the major hazards in the stored program computer that when “going off the end” of a list, an array, or a string can suddenly cause havoc!
Do some web research on this issue, and share what you find. Think about strategies you could use to prevent this from happening, or to detect it before you try to add the 26th line of payroll data, into several parallel arrays that are only 25 payroll records deep.
If you use additional resources to help in developing your discussion post, be sure to provide proper citation for your source. Generally speaking, your responses should be two to three paragraphs (about 300 to 400 words).
Explanation / Answer
To prevent "going off the end" scenario, we can use following strategy:
We can track if iterator value is (length - 1) or not.
If yes, that means we reached to end of array, so that we can stop the loop.
If not, continue the loop.
This method checks if we reached to end or not, and we can break the loop if we reached end.
This method use prior index checking, which will help to know current value of index.
This method is useful in case of list, array or string.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.