CSC161 2011S Imperative Problem Solving

Reading: Loops (K&R 3.5-3.6)

Reading: K&R 3.5-3.6.

Please submit a question by 9 p.m. on Sunday, 27 February 2011.


Comma Operators

Are comma operators just used in for loops, or can they be used other places? Why should they be used sparingly- is this just a stylistic preference by K&R, or are they less efficient to use then statements seperated by semicolons?"

Section 3.5 states that comma operators should be used sparingly. Why is this?

You can use comma operators in many places. However, outside of for loops, they don't operate much differently than semicolons, so you might as well use semicolons. Within for loops, they have the stated advantage (you can have multiple iterator variables), but can affect readability.

Can the comma operator be used after the while statement?

Yes, but there's rarely a point to do so and it leads to less readable code.

Does using a comma operator in a single line of code allow the computer to execute a single statement faster than the equivalent in two statements? K&R seemed to suggest that it did but I may have misinterpreted.


Sample Code

Could you go over one of the sample codes in class? I think I understood most of the section but I had trouble following the code.

That's a bit vague. We'll look at itoa.

Is it possible to implement itoa without building backwards and reversing?

Yes. We'll go over one in class.

Can you give alternative examples to do-while?

Sure. We'll do two versions of itoa.


Choosing Looping Structures

Could you write the do-while loop as a for loop?

Yes. You can write any kind of loop as any other kind of loop. You'll just find some more natural.

In what cases is it advantageous to use an infinite loop, for(;;), instead of an ordinary for or while loop?

When there are so many termination conditions that it would be cumbersome to put them in the test of the for or while loop.

When would it be useful to use a do loop instead of a while or for loop?

As in the example, when you find that the body of the loop needs to be executed at least once.

I still don't understand the difference between while and for loop. When I used for loop for the exam it didn't work as well as the while loop.

They're more-or-less interchangable. I'm happy to look with you at why one would be better than another on your particular problem.


Miscellaneous

K & R say that given a for loop like this:

for (i = 0; i < n; i++) 

it is possible to alter the i and n variables within the loop.

Are there any cases where it's useful to be able to do this, or should we always keep our index variable and our limit constant in a loop?

No cases in which it's useful come to mind. My experience suggests that it's a bad idea to modify these variables within the loop, because we're used to not modifying them, so modifications make it harder to track down bugs.

Is there a way in C to iterate from the end of an array to the beginning without computing or being passed the length?

No. You need the length of an array to find its length. In the case of strings, you can search the array for the null character to find the length. In the case of other arrays, you can't even find the length.

For the do-while proc, so it is like "do this" while "this is happening"? The code in the book is hard to read. I can see why you ask us to do formatting.

Yes, it's do this, and keep doing it, until the condition fails to hold.


The reading about loops is clear, but I just realized that I find it odd we're going over control structures, conditionals, and loops after we've already used them. Granted we're learning a few more things like switch statements and do-while loops, but its strange to me that we're kind of backtracking.

Immersion method.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Feb 28 10:51:41 2011.
The source to the document was last modified on Mon Feb 28 10:51:38 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/loops.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu