nested 'for' loop

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
nested 'for' loop in c
Code:
int main ()
{
  for (i=0; i<5; i++)
  {
    for (j=0; j<5; j++)
    {
      
    }
  }
return 0;
}
I am making flow chart for nested 'for' loop in c. Does my flow chart match with code ?
1585814224567.png
 

WBahn

Joined Mar 31, 2012
32,784
Close. Your code ends by returning a value of 0 to the caller. Your flowchart simply stops.

To put it another way, how would your flowchart be different if you were to change the return statement to "return 42;"? If the flowchart wouldn't change, then you would be claiming that the two pieces of code must behave identically, which clearly they would not.

While I understand why you laid out your flowchart the way you did, it actually makes it harder to follow. You will find it easiest to follow if the flowchart for each structure, such as a for() loop, always looks the same way. Humans are very good at pattern recognition, exploit that.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
To put it another way, how would your flowchart be different if you were to change the return statement to "return 42;"? If the flowchart wouldn't change, then you would be claiming that the two pieces of code must behave identically, which clearly they would not.
@WBahn I didn't understand what do you mean by return statement to "return 42;". both loop doesn't return anything
 

Papabravo

Joined Feb 24, 2006
22,078
IMHO, both loops should look graphically identical, with the same elements (the increment step) in the same relative locations, and the arrows entering and exiting the boxes in the same way. You should be able to instantly recognize that the loops are nested.
 

MrChips

Joined Oct 2, 2009
34,731
Here is an example of a single FOR loop.

As @Papabravo indicated, the structure is the same for a nested loop.

Place the inner loop in the Process box.
And it does not matter what programming language is being used.
WHILE-DO Structure.jpg
 

MrAl

Joined Jun 17, 2014
13,698
I agree with Papabravo.
The main reason you cant be sure it matches the code is because it is not arranged the same way.

You should really rearrange your blocks to match the structure as well as the functionality.
To accomplish this, just put the increment and test at the end of each loop. This means once the inner loop falls through the outer loop then increments again and tests again, and if the exit condition is met it falls through to the end or return statement. In this way the flow chart will look very much like the C code.
 

Ian Rogers

Joined Dec 12, 2012
1,136
Short answer is no! As WBahn pointed out the when the first loop get to 5 it returns to system.. In other words the program stops

You will need a forever loop to encapsulate the code to match the flow chart...
C:
int main (){

   while(1){
       .... nest loops here!
   }
   return 0;
}
 

MrChips

Joined Oct 2, 2009
34,731
Your flowchart is missing the PROCESS box.

Draw the inner loop.
Now draw the outer loop where the inner loop is the PROCESS of the outer loop.

Loop Process.jpg
 

MrAl

Joined Jun 17, 2014
13,698
Here is the best rendition i believe. See how regular the structure is. "Action" is the routine that actually does something.
You can nest as many loops as you need this same way and it is always very readable.

FlowChart-1.gif
 
Last edited:

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Here is the best rendition i believe. See how regular the structure is. "Action" is the routine that actually does something.
You can nest as many loops as you need this same way and it is always very readable.
What does action box do in your flow chart ? What does it need to convert to code ?
 

MrChips

Joined Oct 2, 2009
34,731
Action is the same as Process.

The rectangular box is any action or process that needs to done.
This is the generic way of drawing flowcharts that can be applied to any stituation.

If there is no action, leave it blank.

Look at your original code in post #1.
Anything in between the
{
}
is a Process.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Action is the same as Process.

The rectangular box is any action or process that needs to done.
This is the generic way of drawing flowcharts that can be applied to any stituation.
okay thanks, Why the flow chart in post #17 is best and what's drawback in my last flow chart post #16
 
Top