# Class 19: Control Flow: Loops

Back to Control Flow: Conditionals (2). On to Control Flow: Miscellaneous.

This outline is also available in PDF.

Held: Tuesday, 28 September 2010

Summary: We consider the three main forms of looping in C.

Related Pages:

• EBoard.

Notes:

• Preparation for today's class got a bit derailed, so it may go differently than normal.
• I was surprised to get so few questions on the reading. (Don't worry, I'm pretty sure that the questions I got will take a bit of time.)
• Reading for tomorrow: K&R 3.7-3.8.
• There are certainly questions on exam 1.

Overview:

• Loop Basics.

## Loop Basics

• The standard loop: A while loop.
• Form
```while (TEST)
BODY;
```
• Meaning:
• Evaluate the test
• If the test does not hold (that is, has a value of 0), you're done
• Otherwise, evaluate the body and go back to the start.
• The For loop
```for (INIT; TEST; INCREMENT)
BODY;
```
• An alternate syntax for
```INIT;
while (TEST)
{
BODY;
INCREMENT;
}
```
• The Do-While Loop
```do
BODY;
while (TEST);
```
• An alternate syntax for
```BODY;
while (TEST)
BODY;
```

• I'm not sure what's going on when it talks about comma operators in 3.5, as I find the example code too confusing to be helpful.
• I found the shellsort example on page 62 the most confusing part of the reading. So, nested for loops are somewhat confusing.
• The do-while loop I understand conceptually but a few more examples on how it's implemented would be helpful.
• What I found most confusing was actually the syntax of the atoi function on page 61. I can't understand where the for loops start and end, or how they interact with each other - especially the first one which seems to do nothing. This could be because it's written with a different coding standard than what we're using though....

Back to Control Flow: Conditionals (2). On to Control Flow: Miscellaneous.

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 Wed Dec 8 10:57:08 2010.
The source to the document was last modified on Fri Aug 13 15:41:46 2010.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Outlines/outline.19.html`.

Samuel A. Rebelsky, rebelsky@grinnell.edu