Flowchart "for each" loop loop without variable increment
Asked Answered
B

7

52

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.

Boykins answered 14/12, 2013 at 5:33 Comment(1)
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.Sherl
C
47

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

Ceramics answered 23/3, 2017 at 11:0 Comment(2)
Too bad the image is not directly visible/embedded on Stackoverflow, but I like this answer best for its simplicity.Onomasiology
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.Fear
A
28

I had the same question and found the answer here.

enter image description here

Advantageous answered 7/7, 2016 at 22:35 Comment(1)
That looks to me like an explicit variable increment, which is what the asker is trying to avoid.Fear
N
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.

Noyes answered 24/7, 2019 at 8:48 Comment(0)
S
2

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"

Santa answered 5/6, 2018 at 19:6 Comment(3)
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.Santa
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.Santa
What is the parallelogram on the right doing? Furthermore, why do this and two of the rectangles have two unlabelled outgoing arrows?Fear
S
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.

Spotlight answered 20/12, 2023 at 22:33 Comment(2)
In your scheme, how do you represent multiple actions performed within a single foreach loop?Fear
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.Spotlight
P
0

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

Piloting answered 27/2, 2014 at 2:29 Comment(0)
H
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

Huckleberry answered 20/5 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.