I am new to programming and I am having a problem grasping the concept of looping an array. How do I go about doing this?

Dani AI

Generated

’s example is a good starting point. A couple of points that beginners (and many posts) often miss: explicitly state your indexing convention (0-based vs 1-based) and decide whether you need the element’s index or only its value. Below are compact pseudocode patterns and practical cautions that fill gaps in the thread.

# pseudocode (1-based indexing)
i := 1
while i <= length(A) do
    print A[i]
    i := i + 1
end while
# pseudocode (value-only, language-agnostic "for-each")
for element in A do
    print element
end for

When you must remove or shrink the array while iterating, go backwards so indexes of remaining items are stable:

# iterate backwards when deleting
for i := length(A) down to 1 do
    if shouldRemove(A[i]) then
        remove A[i]
    end if
end for

Troubleshooting checklist (addresses gaps from earlier replies): be explicit about whether your array starts at 0 or 1 — use < length for 0-based loops and <= length for 1-based; check for empty arrays before entering a loop; avoid off-by-one errors by printing both index and value while debugging; if you only need values use a for-each (simpler and less error-prone); and if you must change the array structure during iteration, either iterate backwards or build a new array of kept items. These patterns work in pseudocode and translate cleanly to C++, Java, Python, etc., once you apply the correct indexing rules for the target language.

Recommended Answers

All 3 Replies

i don't have any idea about how to write Pseudo code but i think its like providing logic for the solution of the problem . try to understand the following concept of array(However i am still writing an tutorial on Array in C++, i will try to publish it whenever i have time to complete it)

for(index=0;index<size;index++)
{
    cout<<arr[index]<<endl;
}

and without loop :

cout<<arr[0];
cout<<arr[1];
cout<<arr[2];
cout<<arr[3];
cout<<arr[4];

actually loop allows us to repeat statements or a group of statements.So whenever think to execute statement(s) again and again then you should go through loop.

for more information on loop Click Here

Thank you Learner010. This has helped. It answered my question. I'm looking forward to the publication of your tutorial on Arrays.
Again, thank you ^_^

I'm looking forward to the publication of your tutorial on Arrays

i'll publish it very soon whenever i have time to complete the tutorial.

Thanx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.