55

I'm designed a flowchart to describe at a high level how a programming process works. Part of this process involves looping through a set of items. I'm wondering if there is any standard or semi-standard way of representing a "for each" style loop in a flow chart, that does not involve making the iteration explicit with an iteration box like m = m + 1 (e.g. here). Most modern programming languages have some kind of "for each" construct for enumerating a set or sequence of items, without having to think about indices. I'm basically looking for a similar visual construct for a flow chart, to avoid wasting space with an explicit counter increment.

2
  • 2
    I don't think there is a flowchart specifically designed for for..each loop since it was designed before such concept began. However, you could probably represent it same as the regular for loop, however instead of the standard increment say i=i+1, it would be Get the next Item of the Collection. Commented Dec 14, 2013 at 5:42
  • @Edper R is a syntactic clone of S. When you date its for construction you should back date it to the era when S was designed. Commented Sep 16 at 4:20

7 Answers 7

49

Sooo I know this topic is now 3 years old, but it might help others. I found a little trick to represent a "for each" loop in a UML flowchart. I don't think it is standart, though I find it quite instinctive.

Here it is :

embedded process

Sign up to request clarification or add additional context in comments.

2 Comments

Too bad the image is not directly visible/embedded on Stackoverflow, but I like this answer best for its simplicity.
I would imagine that, in any standard notation for this, the entry and exit of each loop iteration would be explicitly marked somehow. One idea I can see to do this is to add arrows connecting the "do this" box to the edge of the "for each element in array" box, maybe at the point where the arrows connected to that box meet it.
29

I had the same question and found the answer here.

enter image description here

1 Comment

That looks to me like an explicit variable increment, which is what the asker is trying to avoid.
3

Mendix is a rapid application development platform where most of the logic resides in so-called 'microflows', which are represented by flowcharts. Here is an example how it represents a 'for each' loop:

enter image description here

It's similar to @user21715's answer, but it uses the same 'parameter' pentagon which is also used to denote input parameters for a flow, with a 'loop' icon. The small gray circle is the end of the loop iteration (similar to continue in most languages); they have an orange one for a break (a premature end of the loop). The first and last statements in a loop are recognizable by having no incoming resp. outgoing arrow.

Comments

1

Here's an example I found that seems pretty intuitive. I have no idea if this is a standard practice, but it looks good to me.

enter image description here

Sorry about the resolution. The important parts are that the parallelogram on the left says "Listof numbers", the diamond in the middle says "For each", the arrow going down and to the right of the diamond says "Each Number", and the arrow going straight down from the diamond says "End of list"

3 Comments

Hmm... Looks like the image I linked to has moved. Unfortunately, I don't really remember what it looked like. I'll see if I can find it again somewhere and if not, I'll delete my answer.
The original link (svgur.com/s/zA) shows an X in a circle now. However, fortunately, it seems that google image search has cached the thumbnail of the original image, so I've just posted a screenshot of that thumbnail. It's lower resolution, but it gets the point across.
What is the parallelogram on the right doing? Furthermore, why do this and two of the rectangles have two unlabelled outgoing arrows?
1

The "bread-loaf" convention.

I prefer the "bread-loaf" convention, especially if one has to show lots of loops. Adjust wording as needed. I'm not sure if it's an official symbol anywhere, but it could be adopted as a shop convention regardless.

2 Comments

In your scheme, how do you represent multiple actions performed within a single foreach loop?
To put multiple tasks inside the loop, one could stack in more adjacent rectangles below. Or just have a numbered or bulleted list. If and when it gets complex, such as conditionals within, then reverting to the original "long cut" may be in order. I've pondered other variants on the idea, but this is probably not the proper venue.
0

I've done that by setting an initial variable N=numberOfItems, and the rest is history ;) (i.e. a conventional "for" loop).

Comments

0

I really liked @FloverOwe 's suggestion, but, since it isn't a standard shape, I couldn't use it in Visio. I found that there are two standard shapes in Visio that I can add together and clearly indicate the concept of for each. I just rename the arrow to be what set is to be iterated over.

enter image description here

1 Comment

You can define your own shapes in Visio.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.