Question

C++ For Loop Question: Multiple relational expressions cannot be placed into the test condition of a...

C++ For Loop Question: Multiple relational expressions cannot be placed into the test condition of a FOR loop? (True or False) {and please explain why that is}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

True. Multiple relational expressions cannot be placed into the test condition of a for loop.

Suppose, we have taken a for loop statement for example.

     for (x=1,y=2,z=3 ; x<y<z; x++, y++, z++);

In for loop if you are using test conditions like 'X<Y<Z' then basically compiler throws error as comparisons like ‘X<Y<Z’ do not have their mathematical meaning.

Because in general case, if you see, the expression 'X<Y<Z' means you are basically trying to find out whether X is less than Y and Y is less than Z. But when compiler tries to evaluate the expression, It will start from left to right. Where X<Y is true/false, which will be compared with Z. i.e. You will be comparing Z with the result of the expression X<Y, which is not what we wanted. In older (some) compilers, shows an error when you try to use multiple relational expressions in the same test condition. Latest compilers may not give an error for multiple relational expression if used, because they will try to evaluate from left to right.

Add a comment
Know the answer?
Add Answer to:
C++ For Loop Question: Multiple relational expressions cannot be placed into the test condition of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT