Post increment in a loop

Sindhu Chowdary

Sindhu Chowdary

@sindhu-chowdary-tDAv1D Oct 25, 2024
I am reading about what happens when a post increment is present in a loop and I came across this.Can anyone explain me the below code?
A simple loop like

i = 0;
while (a[i++] != 0)
{
...
}

has to be executed as

loop:
temp = i; /* save the value of the operand */
i = temp + 1; /* increment the operand */
if (a[temp] == 0) /* use the saved value */
goto no_loop;
...
goto loop;
no_loop:

or

loop:
temp = a; /* use the value of the operand */
i = i + 1; /* increment the operand */
if (temp == 0)
goto no_loop;
...
goto loop;
no_loop:

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Jan 18, 2014

    Post increment mean calculate the value and then increament.
    This will be

    loop:
    temp = i; /* save the value of the operand */
    i = temp + 1; /* increment the operand */
    if (a[temp] == 0) /* use the saved value */
    i = temp + 1; /* post increament */
    goto no_loop;