473,788 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there any loop that can expressed in while that cannot be expressed in for..?

As we all known, each for-loop can be expressed in while statement.
Can we also say that each while statement can be expressed in
for-statment?

Thanks!

-Andy

Sep 7 '06 #1
8 2104
yu******@gmail. com wrote:
As we all known, each for-loop can be expressed in while statement.
Can we also say that each while statement can be expressed in
for-statment?
Sure. while(x) is the same as for(; x; )

Regards,
Bart.

Sep 7 '06 #2

Bart wrote:
yu******@gmail. com wrote:
As we all known, each for-loop can be expressed in while statement.
Can we also say that each while statement can be expressed in
for-statment?

Sure. while(x) is the same as for(; x; )
No: while(int bar = foo()) { } is a while-statement that doesn't
transform
as you suggest. It does have an equivalent for-loop though.

Sep 7 '06 #3

yu******@gmail. com wrote:
As we all known, each for-loop can be expressed in while statement.
We do? I do not believe that is correct. To translate a for statement
you need a while statement and a bit of initialisation in the general
case. How else would you do:

int f1();
int f2();

for (int begin = f1(), int end = f2(); begin < end; ++begin)
stmt;

/Peter

[snip]

Sep 7 '06 #4
<Mi************ *@tomtom.comsch rieb im Newsbeitrag
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
>
Bart wrote:
>yu******@gmail. com wrote:
As we all known, each for-loop can be expressed in while statement.
Can we also say that each while statement can be expressed in
for-statment?

Sure. while(x) is the same as for(; x; )

No: while(int bar = foo()) { } is a while-statement that doesn't
transform
as you suggest. It does have an equivalent for-loop though.
Are you sure? If bar is a built-in type, I agree. But what about user
defined types? How would

while (SomeUglyClass bar = foo()) {...}

be written as a for loop, assuming that SomeUglyClass has all the necessary
constructors and conversions, but no assignment operator (or if assignment
behaves slightly different from copy construction). The while loop
constructs (and destructs) a new instance of SomeUglyClass for each
execution of its body. An obvious solution using a for loop (for
(SomeUglyClass bar; bar=foo(); ) {}) only creates one instance of
SomeUglyClass, so it would not be equivalent to the while loop.

The only really equivalent for loop I can think of, is something like

for (;;)
{
if (ComeUglyClass bar = foo())
{
...
}
else
break;
}

but I don't think this really counts as a valid replacement.

Heinz

Sep 7 '06 #5
"peter koch" <pe************ ***@gmail.comsc hrieb im Newsbeitrag
news:11******** **************@ e3g2000cwe.goog legroups.com...
>
yu******@gmail. com wrote:
>As we all known, each for-loop can be expressed in while statement.

We do? I do not believe that is correct. To translate a for statement
you need a while statement and a bit of initialisation in the general
case. How else would you do:

int f1();
int f2();

for (int begin = f1(), int end = f2(); begin < end; ++begin)
stmt;
That is not a valid for statement. for (int begin=f1(), end=f2(); ...) would
be valid, but there can only be one decl-specifier-seq in a
for-init-statement.

Heinz

Sep 7 '06 #6

Heinz Ozwirk wrote:
"peter koch" <pe************ ***@gmail.comsc hrieb im Newsbeitrag
news:11******** **************@ e3g2000cwe.goog legroups.com...

yu******@gmail. com wrote:
As we all known, each for-loop can be expressed in while statement.
We do? I do not believe that is correct. To translate a for statement
you need a while statement and a bit of initialisation in the general
case. How else would you do:

int f1();
int f2();

for (int begin = f1(), int end = f2(); begin < end; ++begin)
stmt;

That is not a valid for statement. for (int begin=f1(), end=f2(); ...) would
be valid, but there can only be one decl-specifier-seq in a
for-init-statement.

Heinz
Right. But my statement remains valid even if the example was
ill-formed.

/Peter

Sep 7 '06 #7

Heinz Ozwirk wrote:
>
That is not a valid for statement. for (int begin=f1(), end=f2(); ...) would
be valid, but there can only be one decl-specifier-seq in a
for-init-statement.
which is an annoying language feature when you want to initialise two
or more locals of different types. Of course there is a workaround
using tuples but that is relatively clumsy.

Sep 7 '06 #8
Earl Purple wrote:
Heinz Ozwirk wrote:
>>
That is not a valid for statement. for (int begin=f1(), end=f2();
...) would be valid, but there can only be one decl-specifier-seq in
a for-init-statement.

which is an annoying language feature when you want to initialise two
or more locals of different types. Of course there is a workaround
using tuples but that is relatively clumsy.
What's wrong with wrapping the loop in another set of curly braces?
Initialise all you need before the 'for'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 7 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
2212
by: Mark Anderson | last post by:
A 'for' loop takes 3 arguments (initialize; test; increment). The 'test' must equate as true or false This doesn't work... x = 5; for (y=1; (y==5); y+=1) { alert(x * y); } ...nor does... x = 5;
0
2266
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of images: /Users/t/t4/oh/DSCN2059m-s.jpg
32
4660
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers."
7
3208
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
9
4179
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
7
3011
by: ssantamariagarcia | last post by:
I have found a problem while using a while statement and a linked list. I had never met this matter before....and I am wondering that if you have , please tell me what it is wrong. I am traversing the linked list using a pointer to cycle through. int Function(...){ node * p;
59
79813
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
7
1370
by: UnkleVo | last post by:
Can someone run the code below and tell me why it never reaches 0.06? I am really puzzled..... or just going crazy? Dim i As Double For i = 0.01 To 0.05 Step 0.01 Debug.WriteLine(i) Next Debug.WriteLine("*******max0.05************") For i = 0.01 To 0.06 Step 0.01
5
1941
by: Tinku | last post by:
I am sorry for asking this silly question, but i don't understand why it is happening please suggest me ================================= #include <stdio.h> int main() { static int i=1; printf("function call: %d time \n", i); i++; main();
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10173
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.