Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

complex for loops in c

Posted on 2011-04-26
16
Medium Priority
?
232 Views
Last Modified: 2012-05-11
In a For Loop in C, I have an example where there are multiple conditions separated by commas.
There are 2 initializations, then 2 conditions, then 2 controls.

My question are the conditions separate by commas considered an OR or AND?
I am assuming OR.
0
Comment
Question by:dpalyca755
  • 6
  • 4
  • 2
  • +2
15 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 35469533
>> My question are the conditions separate by commas considered an OR or AND?

The loop condition is an expression with a boolean result (or a result that can be converted to a boolean value).

The comma operator evalutes both statements on either side of the comma. However the result is that of the second statement (in other words, the result of the first statement is discarded).

Combine these two bits of information, and you should have the answer to your question ;)
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35469538
Your conditions are however you want them to be just like you would define them in an if statement, The three parts of the for loop are all that's necessary.

EG: for (int i = 5; i < 10 && i >= 5; i++)

You can also have multiple inits and increment/decrements as well as complex variable changing. These will be comma delimited but are always ANDs.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35469572
>> These will be comma delimited but are always ANDs.

The comma operator does not perform an AND operation.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:brutaldev
ID: 35469707
No it doesn't, but in the context of this question I meant that all statements will be executed.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35469739
I just wanted to avoid any confusion, given that dpalyca755 was using the AND in the sense of a logical AND (as far as I understood it).
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35469807
I see your point, but the comma delimited statements cannot contain any conditions, so I thought he was wondering if one OR the other got executed or one AND the other.

Anyway, it's all in the details, I think we answered the question, didn't we?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35469906
>> but the comma delimited statements cannot contain any conditions

Sure they can. A conditional expression is a valid assignment expression (as the C standard calls it), so it can be an operand to the comma operator.

This works just fine :

        for (i = 20; --i, i > 10;) printf("%d ", i);
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35470034
Meant "shouldn't". For loops can be as complex as they need be, each comma delimited statement is still executed regardless of the previous one's conditional outcome.

I wouldn't consider --i a "condition" regardless of what the standard calls it. It evaluates to something sure, but in other languages that wouldn't even compile because it's usually an error. That for loop works just fine but it's bad practice.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35470095
>> I wouldn't consider --i a "condition"

That's because it isn't, and nobody said it was (although technically, it can be used as a condition, since its result can be used as a boolean value).

The point is that the loop condition in a for loop can be any valid expression. And a comma operator with two operands is a valid expression. As long as the right operand yields a result that can be used as a boolean value, it makes perfect sense (and is entirely valid) that it can be used as the loop condition in a for loop.


>> That for loop works just fine but it's bad practice.

I never have claimed (and will never do so) that this is good practice.
It is however what this question is about, so that's what I'm answering.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35474589
Examples for better understanding of comma operator:
1. a = (b = 2, c = 3, d = 4);
    In this, a gets the value 4. (that is the value of the rightmost sub-expression).
2. fun(x, (y=7, y+3), z);
    In this, the 2nd argument passed to fun() uses a comma operator.
    So, the value passed is 10.
    The first part is executed and y gets the value 7. And the 2nd part is executed after that. y is added to 3 and the value returned is 10. This value is passed on.

Hope this clarifies your doubt.
0
 
LVL 85

Expert Comment

by:ozo
ID: 35474780
2. fun(x, (y=7, y+3), z);
    In this, the 2nd argument passed to fun() uses a comma operator.
    So, the value passed is 10.

That is correct.
But another subtle point is that the commas between the function arguments are not comma operators, so if it had been
fun(y+2, (y=7, y+3), y+4)
there would be no guarantee whether the y=7 is executed before or after the y+2 or y+4, nor, since the behavior would be undefined, any guarantee that it would execute at all.



> As long as the right operand yields a result that can be used as a boolean value

Besides an aggregate type, is there any possible right operand of a comma operator that cannot be used as a boolean value?
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35474895
> fun(y+2, (y=7, y+3), y+4)
> there would be no guarantee whether the y=7 is executed before or after the y+2 or y+4, nor, since the
> behavior would be undefined, any guarantee that it would execute at all.
How does it matter, which order the arguments are processed in the above example?
Suppose, y+2 is executed first and then (y=7, y+3) is executed - the 2nd argument will get the value 10.
Suppose, y+4 is executed first and then (y=7, y+3) is executed - the 2nd argument will get the value 10.
So, the value of 2nd argument is not changing, based on the order of evaluation.

Also, I didn't understand the statement "behavior would be undefined, any guarantee that it would execute at all".
May be I am missing some point. So, can you please elaborate?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35474934
>> Besides an aggregate type, is there any possible right operand of a comma operator that cannot be used as a boolean value?

Besides aggregate types (which you already mentioned), there is void (or anything returning void, or cast to void).
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37392796
Sorry, I was just cleaning this and noticed that an important question from ssnkumar had gone unanswered.

>> How does it matter, which order the arguments are processed in the above example?

Ok, I'm going to try and explain this in the simplest way possible to try and make clear why ozo's assertion is true.

The order than function arguments are evaluated is not defined by the standard but they will all be evaluated before the function is called. This is what the ANSI C standard says...

"In preparing for the call to a function, the arguments are evaluated,
and each parameter is assigned the value of the corresponding argument"

"The order of evaluation of the function designator, the actual arguments, and
subexpressions within the actual arguments is unspecified, but there is a sequence point
before the actual call."

So if we look at your example...

fun(y+2, (y=7, y+3), y+4)

and then look at how the compiler might process that line (I say might, because it is undefined, as ozo alludes)...

(y=7 : 7
y+3 : 10)
y+2 : 12
y+4 : 16

Now the function is called, thus...
func(16, 16, 16);

Or it might do this (assume y starts out as 0)...

y+2 : 2
y+4 : 6
(y=7 : 7
y+3 : 9)

Now the function is called, thus...
func(9, 9, 9);

...and so on!

The problem is the compiler can process those arguments in any order, it is compiler specific (or, undefined). Depending on the order and depending on the expressions the results could be different on different compilers, hence the result is undefined.

I hope that makes sense. It's not the easiest thing to understand and it can cause a lot of confusion (even for seasoned C programmers). I've tried to make my explanation as accurate as possible but may have glossed over the exact semantics of the standards definition in favour of clarity.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37419090
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question