Discussion:
a4q2 for loop optional expressions
(too old to reply)
Ricky
2004-07-01 06:21:30 UTC
Permalink
In the SL specification, it says that the 'for' loop consists of a bracketed
list of three OPTIONAL expressions. Does this mean that if nothing is
entered for any of the expressions, this will go into an infinite loop?
This doesn't make much sense. Should the specification not say optional and
say required?
Nguyen Nguyen
2004-07-01 16:59:58 UTC
Permalink
The expressions are optional. Yes, this means that infinite loops are
possible. Making the expressions required does not prevent infinite
loops. For example,
int i;
for (i<-1; i>0; i<-i+1)

Something like this is probably a logical error on the part of the
programmer. In general, it is not a compiler's job to catch logical
programming errors. However, a good compiler would probably output a
warning about the potential for an infinite loop.


Nguyen
Post by Ricky
In the SL specification, it says that the 'for' loop consists of a bracketed
list of three OPTIONAL expressions. Does this mean that if nothing is
entered for any of the expressions, this will go into an infinite loop?
This doesn't make much sense. Should the specification not say optional and
say required?
Katherine Chiang
2004-07-01 17:07:16 UTC
Permalink
Just a quick question about the for loop
what exactly is the structure of the for loop? it's really unclear in the
SL spec. So far, I came up with a couple of different structures
already...

for expr1; expr2; expr3;
block statement
endfor;

OR

for expr1; expr2; expr3
block statement
endfor;

OR

for (expr1; expr2; expr3)
block statement
endfor; (according to your reply to the last question)

which is the correct one?

Katherine
Post by Nguyen Nguyen
The expressions are optional. Yes, this means that infinite loops are
possible. Making the expressions required does not prevent infinite
loops. For example,
int i;
for (i<-1; i>0; i<-i+1)
Something like this is probably a logical error on the part of the
programmer. In general, it is not a compiler's job to catch logical
programming errors. However, a good compiler would probably output a
warning about the potential for an infinite loop.
Nguyen
Post by Ricky
In the SL specification, it says that the 'for' loop consists of a bracketed
list of three OPTIONAL expressions. Does this mean that if nothing is
entered for any of the expressions, this will go into an infinite loop?
This doesn't make much sense. Should the specification not say optional and
say required?
Nguyen Nguyen
2004-07-01 21:21:14 UTC
Permalink
Only the last structure is valid. Please read the SL specification again
carefully.

Nguyen
Post by Katherine Chiang
Just a quick question about the for loop
what exactly is the structure of the for loop? it's really unclear in the
SL spec. So far, I came up with a couple of different structures
already...
for expr1; expr2; expr3;
block statement
endfor;
OR
for expr1; expr2; expr3
block statement
endfor;
OR
for (expr1; expr2; expr3)
block statement
endfor; (according to your reply to the last question)
which is the correct one?
Katherine
Post by Nguyen Nguyen
The expressions are optional. Yes, this means that infinite loops are
possible. Making the expressions required does not prevent infinite
loops. For example,
int i;
for (i<-1; i>0; i<-i+1)
Something like this is probably a logical error on the part of the
programmer. In general, it is not a compiler's job to catch logical
programming errors. However, a good compiler would probably output a
warning about the potential for an infinite loop.
Nguyen
Post by Ricky
In the SL specification, it says that the 'for' loop consists of a bracketed
list of three OPTIONAL expressions. Does this mean that if nothing is
entered for any of the expressions, this will go into an infinite loop?
This doesn't make much sense. Should the specification not say optional and
say required?
Loading...