Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,519 people online right now. Registration is fast and FREE... Join Now!




multiple conditions in a loop

 
Reply to this topicStart new topic

multiple conditions in a loop

czilofty
3 Nov, 2006 - 09:41 PM
Post #1

New D.I.C Head
*

Joined: 3 Nov, 2006
Posts: 1


My Contributions
CODE

#include <iostream>
using namespace std;

int main()
{
    bool error = false;
    
    for (int i=-3; error, i++; error = !error)
        if (error)
            cerr << "true ";
        else
            cout << "false ";
            
    return 0;
}


I have a C++ exam coming up and I thought i knew everything we had learnt so far. However in the last lecture they gave us some practice questions, one of them was the above code. The lecturer tried to explain it to us but I didn't really get it.

The result of the above code is "false true false" and I got confused by how the multiple conditions work in the for loop (seperated by a comma). So I changed the code replacing 'error' and 'i++' with variations of 'true' and 'false'. I found that the result of the condition only depended on the last condition.

My question is this; in this code and in any code, if you have multiple conditions is the only one that counts the last one? Are the rest pointless?

Thankyou,
Isaac
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Multiple Conditions In A Loop
3 Nov, 2006 - 10:46 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(czilofty @ 3 Nov, 2006 - 10:41 PM) *

CODE

#include <iostream>
using namespace std;

int main()
{
    bool error = false;
    
    for (int i=-3; error, i++; error = !error)
        if (error)
            cerr << "true ";
        else
            cout << "false ";
            
    return 0;
}


I have a C++ exam coming up and I thought i knew everything we had learnt so far. However in the last lecture they gave us some practice questions, one of them was the above code. The lecturer tried to explain it to us but I didn't really get it.

The result of the above code is "false true false" and I got confused by how the multiple conditions work in the for loop (seperated by a comma). So I changed the code replacing 'error' and 'i++' with variations of 'true' and 'false'. I found that the result of the condition only depended on the last condition.

My question is this; in this code and in any code, if you have multiple conditions is the only one that counts the last one? Are the rest pointless?

Thankyou,
Isaac

Isaac

When trying this sort of experiment, sometimes a paperbased exercise is better. A for loop has three parts, being:
  • initialisation
  • conditional exit
  • update

The initialisation and update are fairly straight forward. The conditional uses operator, and you are quite right, the expression will take the last value for the conditional exit, in this case when i==0.

As to the question are they pointless? In the example, I would have to say yes. The operator used simply prevents the state of error from being combined with i. Had this been done for ( int i = -3 ; error && i++ ; error != error) now we have combined them logically.

The comma operator has a lot of uses, but this usage doesn't really explain it well, nor instill the way to use it.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Multiple Conditions In A Loop
4 Nov, 2006 - 03:50 AM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Nice job gregoryH, that couldn't be worded better.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 06:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month