Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to picture "for" loop in block representation of algorithm

Tags:

algorithm

I have probem / strange question, i got algorithm with few "for" loops and now i need to do block scheme of this algorithm.

I know how to picture "while" loop, but is this acceptable to represent "for" loop using "while" and at this point make difference between souce code and algorithm?. Ofcourse assuming that all "for" loops are right in place and using loop of any other kind would produce unnecessary code which i avoided using "for" loops.

I'm guessing that this is rather impossible (at least I can't imagine this) to simply picture "for" loop, but maybe there is a way (if exists).

Thanks in advance

like image 530
MoreThanChaos Avatar asked May 18 '09 19:05

MoreThanChaos


People also ask

How do you represent a loop in an algorithm?

C For Loop Syntax. The for loop's triad statement is like the ( i=0 ; i < n ; i++ ). First comes the initialization of the counter variable. For example – If the variable is “i” then it needs to be initialized like the following.

How do you represent a for loop in a flowchart?

Here again, a hexagon shaped flowchart symbol is used to represent the for loop and the symbol is divided into three to represent the initial condition, the increment, and the terminating condition.

How do you write a loop in pseudocode?

Using For Loops Say we wanted to loop through a block of code 5 times, we use i, a local variable, that is built into most programming languages, and can be used in pseudocode too. We would say: For i = 1 To 5; 5 being the number of times you want to loop the code; you can change this to what you would like.


2 Answers

Here's a flow chart that illustrates a for loop:

Flow Chart For Loop

The equivalent C code would be

for(i = 2; i <= 6; i = i + 2) {     printf("%d\t", i + 1); } 

I found this and several other examples on one of Tenouk's C Laboratory practice worksheets.

like image 90
Bill the Lizard Avatar answered Sep 24 '22 00:09

Bill the Lizard


What's a "block scheme"?

If I were drawing it, I might draw a box with "for each x in y" written in it.

If you're drawing a flowchart, there's always a loop with a decision box.

Nassi-Schneiderman diagrams have a loop construct you could use.

like image 34
Charlie Martin Avatar answered Sep 27 '22 00:09

Charlie Martin