469,076 Members | 1,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,076 developers. It's quick & easy.

How while loop works

Hi All,

I have doubt regarding how compiler understands about while loop. For
example the bellow mentioned code produce the output as mentioned
bellow.

#include<stdio.h>

int main(void)
{
int i =10 ;
goto label;
while(--i 0)
{
label:
printf("\n Value of i = %d \n",i);
}

return 0;
}
Value of i = 10
I/O:
I/O: Value of i = 9
I/O:
I/O: Value of i = 8
I/O:
I/O: Value of i = 7
I/O:
I/O: Value of i = 6
I/O:
I/O: Value of i = 5
I/O:
I/O: Value of i = 4
I/O:
I/O: Value of i = 3
I/O:
I/O: Value of i = 2
I/O:
I/O: Value of i = 1

My understanding of the behavior is when the execution encounter goto
statement it jumps to label: and when it find that it is inside while
loop it goes back to while(--i 0) and execute the loop.
Is my understanding correct ?
If it is correct then my question is how "c" compiler understand that
it is inside loop?

Please help .

Regards,
Somenath

Aug 27 '07 #1
9 2881
On Aug 27, 9:56 am, somenath <somenath...@gmail.comwrote:
Hi All,

I have doubt regarding how compiler understands about while loop. For
example the bellow mentioned code produce the output as mentioned
bellow.

#include<stdio.h>

int main(void)
{
int i =10 ;
goto label;
while(--i 0)
{
label:
printf("\n Value of i = %d \n",i);
}

return 0;}

Value of i = 10
I/O:
I/O: Value of i = 9
I/O:
I/O: Value of i = 8
I/O:
I/O: Value of i = 7
I/O:
I/O: Value of i = 6
I/O:
I/O: Value of i = 5
I/O:
I/O: Value of i = 4
I/O:
I/O: Value of i = 3
I/O:
I/O: Value of i = 2
I/O:
I/O: Value of i = 1

My understanding of the behavior is when the execution encounter goto
statement it jumps to label: and when it find that it is inside while
loop it goes back to while(--i 0) and execute the loop.
Is my understanding correct ?
If it is correct then my question is how "c" compiler understand that
it is inside loop?

Please help .

Regards,
Somenath
I think compiler is not so intelligent. What is the compiler you use ?
Normally, It will not be able to do like that.
Here, It will print for the first time alone and will return 0.

But, If you have used 'do while' i think, it will behave the way you
expect/described.

Karthik Balaguru

Aug 27 '07 #2
>int main(void)
>{
int i =10 ;
goto label;
while(--i 0)
{
label:
printf("\n Value of i = %d \n",i);
labul:
}
How can this program possibly print the output below? There is nothing
that prints "I/O:" in the program.
>
return 0;
}
Value of i = 10
I/O:
I/O: Value of i = 9
I/O:
I/O: Value of i = 8
I/O:
I/O: Value of i = 7
I/O:
I/O: Value of i = 6
I/O:
I/O: Value of i = 5
I/O:
I/O: Value of i = 4
I/O:
I/O: Value of i = 3
I/O:
I/O: Value of i = 2
I/O:
I/O: Value of i = 1

My understanding of the behavior is when the execution encounter goto
statement it jumps to label: and when it find that it is inside while
loop it goes back to while(--i 0) and execute the loop.
If it is your contention that when the execution encounters a goto
label statement the CPU bounces off of the force shield surrounding
"label", ricochets and ends up at "labul" (which I added in the
above code), without executing the printf() between them that is
absolutely incorrect.
>Is my understanding correct ?
If it is correct then my question is how "c" compiler understand that
it is inside loop?
At compile time, parsing the program reveals that it is inside a loop.
At run time, it may not be necessary to figure out that it is inside a
loop; the assembly-language branch instructions take care of it all.

Things get more interesting if the code block for the loop has local
variables declared at the beginning of it.

Aug 27 '07 #3
On Aug 27, 10:25 am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
On Aug 27, 9:56 am, somenath <somenath...@gmail.comwrote:


Hi All,
I have doubt regarding how compiler understands about while loop. For
example the bellow mentioned code produce the output as mentioned
bellow.
#include<stdio.h>
int main(void)
{
int i =10 ;
goto label;
while(--i 0)
{
label:
printf("\n Value of i = %d \n",i);
}
return 0;}
Value of i = 10
I/O:
I/O: Value of i = 9
I/O:
I/O: Value of i = 8
I/O:
I/O: Value of i = 7
I/O:
I/O: Value of i = 6
I/O:
I/O: Value of i = 5
I/O:
I/O: Value of i = 4
I/O:
I/O: Value of i = 3
I/O:
I/O: Value of i = 2
I/O:
I/O: Value of i = 1
My understanding of the behavior is when the execution encounter goto
statement it jumps to label: and when it find that it is inside while
loop it goes back to while(--i 0) and execute the loop.
Is my understanding correct ?
If it is correct then my question is how "c" compiler understand that
it is inside loop?
Please help .
Regards,
Somenath

I think compiler is not so intelligent. What is the compiler you use ?
I used multi . But if i used the gcc , even I am getting the bellow
output.
Value of i = 10

Value of i = 9

Value of i = 8

Value of i = 7

Value of i = 6

Value of i = 5

Value of i = 4

Value of i = 3

Value of i = 2

Value of i = 1
Aug 27 '07 #4
How can this program possibly print the output below? There is nothing
that prints "I/O:" in the program.
Actually i was executing the program in a embedded simulation
environment.Which produced the "I/O"

Aug 27 '07 #5
somenath said:
Hi All,

I have doubt regarding how compiler understands about while loop.
More idiomatically: "I have a question about while loops."
#include<stdio.h>

int main(void)
{
int i =10 ;
goto label;
while(--i 0)
{
label:
printf("\n Value of i = %d \n",i);
}

return 0;
}
Firstly, I would just like to point out that it's rarely a good idea to
use goto (in fact, I've never come across a good reason to use it in
"real" code).

But here, your reason for using it is to help to deepen your
understanding of C, and that's a pretty good reason, so let's look at
this very closely.

To do so, I am going to cheat. I'm going to invent an assembly language
that targets a non-existent machine, and then I'm going to pretend that
your C compiler converts your program into an assembly language program
for that machine.

Here are the relevant parts of the assembly language:

Rn - refers to register n
SET Rn, value - assigns value to register n
JMP address - jumps to given address
DEC Rn - reduce register n's value by 1
CMP Rn - compares value in register n with 0
JLE address - if last value compared was negative, jump to given address
PRINT Rn - prints newline, space, "Value of i = ", value of given
register, space, newline (!)
EXIT n - halts program with return code of n

Addresses start at A and continue in counting fashion.

Here, then, is your program after compilation:

ADDRESS INSTRUCTION
A SET R0, 10
B JMP F
C DEC R0
D CMP R0
E JLE H
F PRINT R0
G JMP C
H EXIT 0

If you follow through the logic here, you'll perhaps see why you're
getting the output that you see in your own code.

There's nothing magic about the while loop. It is very likely to be
translated as a simple compare and conditional jump at the top, with an
unconditional jump at the bottom (as in the above example). So this is
just a matter of what jumps where and when.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 27 '07 #6
There's nothing magic about the while loop. It is very likely to be
translated as a simple compare and conditional jump at the top, with an
unconditional jump at the bottom (as in the above example). So this is
just a matter of what jumps where and when.
I find that the 'Uncoditional Jump at the bottom" Very interesting.
Your explanation by converting in terms of assembly is really good.
Is it the same with all kind of compilers or dependent on processor/
compiler ?

Thx,
Karthik Balaguru

Aug 27 '07 #7
On 27 aug, 08:57, karthikbalaguru <karthikbalagur...@gmail.comwrote:
There's nothing magic about the while loop. It is very likely to be
translated as a simple compare and conditional jump at the top, with an
unconditional jump at the bottom (as in the above example). So this is
just a matter of what jumps where and when.

I find that the 'Uncoditional Jump at the bottom" Very interesting.
Your explanation by converting in terms of assembly is really good.
Is it the same with all kind of compilers or dependent on processor/
compiler ?
If you turn off all optimisations, the same basic structure can be
recognised on all compilers.
Once the optimiser gets his hands on the code, the result might be
completely unrecognisable.
>
Thx,
Karthik Balaguru
Bart v Ingen Schenau

Aug 27 '07 #8
Firstly, I would just like to point out that it's rarely a good idea to
use goto (in fact, I've never come across a good reason to use it in
"real" code).

But here, your reason for using it is to help to deepen your
understanding of C, and that's a pretty good reason, so let's look at
Many Many thanks Richard Heathfield for the explanation.I would like
tell you that I have seen use of goto in real code.It is a SNMP
protocol implementation. There for different condition one function
has to set error code in some specif structure and for some specific
error it has to set the value in some variable(structure) and continue
with in same function , for some error condition it simply return
error code and for successfull condition it has to set specific value
to specific structure.
I dont know if it could have been written in better way with out
goto .

But the code I posted purely to understand the while loop in C.
Thanks once again.

Aug 27 '07 #9
On Aug 27, 1:00 pm, Bart van Ingen Schenau
<Bart.van.Ingen.Sche...@ict.nlwrote:
On 27 aug, 08:57, karthikbalaguru <karthikbalagur...@gmail.comwrote:
There's nothing magic about the while loop. It is very likely to be
translated as a simple compare and conditional jump at the top, with an
unconditional jump at the bottom (as in the above example). So this is
just a matter of what jumps where and when.
I find that the 'Uncoditional Jump at the bottom" Very interesting.
Your explanation by converting in terms of assembly is really good.
Is it the same with all kind of compilers or dependent on processor/
compiler ?

If you turn off all optimisations, the same basic structure can be
recognised on all compilers.
Once the optimiser gets his hands on the code, the result might be
completely unrecognisable.
Thats cooool :):)

Thx,
Karthik Balaguru

Aug 28 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by ritterhaus | last post: by
10 posts views Thread by Nick L | last post: by
8 posts views Thread by Abby Lee | last post: by
6 posts views Thread by Dave Spencer | last post: by
8 posts views Thread by Terry Olsen | last post: by
6 posts views Thread by Gary Wessle | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.