473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3177
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
10
by: Nick L | last post by:
I'm working on a function which creates a pointers to an array of unsigned ints based off a number read from a file. I then continue to read file names from the file, convert the name to a char*...
8
by: Abby Lee | last post by:
My function works but there has got to be a way to make a for loop to handle this...but I can't get a for loop to work. You can tell, I'm not very good at this...help. "myvalue" is the number of...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
6
by: Dave Spencer | last post by:
Hi all, New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the list and updating the list on the...
8
by: Terry Olsen | last post by:
How do I loop back to the beginning of a for/next loop before getting to the end of it? Isn't there an "iterate" command or something like that? For Each This in That ...code if This = False...
6
by: Gary Wessle | last post by:
Hi using the debugger, I happen to be on a line inside a loop, after looping few times with "n" and wanting to get out of the loop to the next line, I set a break point on a line after the loop...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
2
by: akanialaka | last post by:
Hope I can get some help here... I connect to an SQL database using ASP. I can see the records. I have a small (very basic) function written in Javascript. When I use the MoveNext in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.