473,386 Members | 2,050 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,386 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 3180
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.