csc110: 2010 03 02

Posted in csc110 by bnmng on 2010 03/02

Notes HW 4:
The precedence table is important. Keep in mind, in a statement like
5 % 2 * 4 > 5 OR 4 % 9 * 5 < 7
The Mods come before the other operators. The comparisons are last.

Switch statemnets in C always compare constants. Cannot use range.

Homework for Chapter 5.

Section 5.1: 2 and (3 Parts (a) and ©)

Section 5.2: 7.
Section 5.3: 1, 4, and 10
Section 5.4: 4 (Do exersise 4 in section 5.3 using a do-while loop).

There's a test 17th

Topics today:
Chapter 5
5.2 cin in a lop
5.3 sentinal
5.4 for loop
5.5 do while
5.6 nested loops.

cin within a loop: On page 187 is an example of a loop that uses the constant MAXNUMS to control how many times the loop will run. This program asks for four grades, one at a time. After the four grades are entered, the loop ends and the rest of the program continues.

If we wanted more flexibility, we could use and integer, perhaps "int maxnums" instead, and ask the user how many grades he plans to enter.

Using sentinel values: There are other ways to control loops. In program 5.8, instead of knowing how many times the loop will run, it runs until a special value is entered. In the example, that value is any number number over 100. If 101 were to be entered, it would stop the loop and the rest of the program would continue. That value is called a sentinel.

One technique in looping while reading is using a “priming read” . This is often a copy of an input statement that is inside of the loop, but it’s strategically located before the loop to prevent errors which may occur in some situations. ** I’ll try to add some examples on this **

There are two kinds of loops. Test top and test bottom. A [for] loop is a test top loop. A for loop looks like this:
for (count = 2; count <= 20; count = count + 2)
cout << count <> a;
cout << a;

Technically, a sentinal loop can be written in for loop format, but it's not recommended.

A test bottom loop looks like this:
while (count <= 10);

A test-bottom loop runs once even if a sentinel value has been entered, because it doesn't test until after the first run.

Loops can be nested.
for (i=1; i<=4, i++ ) {
for (j=1; j<=i; j++) {\
cout << j << " ";
cout<< i << endl;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: