Search code examples
cnested-loopsflowchart

How to convert nested loop construct into flow chart in C?


I'm drawing this code

if (V > 24)
{
    do
    {
        PWM();

        while (I = 0)
        {
            RA5 = 0;
            LED();
            I2C();
            Delay_ms(1000);
            RA5 = 1;
            Delay_ms(1000);

            if (I != 0)
            {
                break;
            }
        }

        LED();
        I2C();
    } while (v < 28.7)
}

to this

flow_chart

I really don't know how to deal with this nested loop, is there any better idea for it?


Solution

  • Unrelated, but

        if(I != 0){
            break;
        }
    

    Is implied by the for loop, you don't need it. Also be careful:

    while(I=0)
    

    will set I to 0 and your loop will never exit! You may have found this and decided to add the break later to compensate. What you want is:

    while(I==0)
    

    Note that what you did with the do while is correct, and a while loop is the same except the check happens before entering the loop rather then when exiting the loop. Remove the break and have your arrow simply go back to before the check for I == 0.