473,387 Members | 1,693 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,387 software developers and data experts.

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 2082
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.comschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.googlegr oups.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.comschrieb im Newsbeitrag
news:11**********************@e3g2000cwe.googlegro ups.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.comschrieb im Newsbeitrag
news:11**********************@e3g2000cwe.googlegro ups.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
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...
0
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...
32
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...
7
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
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
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...
59
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
7
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...
5
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;...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.