answersLogoWhite

0

We use loops with arrays because it's one the simplest methods of traversing an array. since each element in an array is the same size, every element can be access via an offset from the start of the array, using the array suffix operator []. Arrays employ zero-based offsets because the first element is always at the same address as the array itself. Thus the first element is at offset [0], while the third is at offset [2]. What this means is that if we multiply the size of an element by its offset index, we can determine the address of that element and therefore access that element's value. The subscript operator does this for us automatically, thus giving us constant-time random access to any element in the array. We can also use pointers to manually calculate the address of an element (which is what actually goes on Behind the Scenes). However, when we wish to traverse the array, one element at a time, a loop is the simplest method of doing so. The loop simply iterates through all the offset indices, beginning with offset 0, then 1, and 2, and so on. The final index is always 1 less than the number of elements in the array, because arrays are zero-based.

User Avatar

Wiki User

11y ago

Still curious? Ask our experts.

Chat with our AI personalities

JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
ReneRene
Change my mind. I dare you.
Chat with Rene
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
More answers

For the same reason you use arrays in any other language -- to store one or more related data elements of the same type within contiguous memory, with constant time random access to any element in the array. The most common type of array is a string, which is a simply an array of type char (ANSI/ASCII and multi-byte ASCII characters) or wchar_t (Unicode characters) .

User Avatar

Wiki User

11y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Why you use loop in arrays in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp