bg287

csc110: 2010 02 25

Posted in csc110 by bnmng on 2010 02/25

Assignment: Read Chapter 5 of Bronson

Subjects:

if… else

Nested if … else

Switch

While Loop.

Review from previous notes.

Evaluating results of and (&&) or (||), less than (<), equal to (==), and not (!).

We looked at precidence rules.

Short circuiting. If you have a false on an &&, or a true on a nor, than the rest of the expression is not evaluated.

Looking at decisions.

On the screen, Prof. Sterling showed some examples of making blocks, and what happens if you're not careful about placing blocks.

If you want something like
if (a==b)
{
cout << “a equals b\n”;
}
else
{
cout << “a does not equal b\n”;
}

You can write it like it is above, or you can write
if (a==b)
cout << “a equals b\n”;
else
cout << “a does not equal b\n”;

The second example works only if there is only one statement after the [if] or [else]. An example of a mistake would be:
if (a==b)
cout << “a equals b\n”;
else
cout << “Too bad,”;
cout << “a does not equal b\n”;

The last line, “a does not equal b\n”; will run either way because it is not directly under the else. But bracketing fixes the problem:
if (a==b)
{

cout << “a equals b\n”;
cout << “very nice, ”;
}
else
{
cout << “Too bad,”;
cout << “a does not equal b\n”;
}
Without bracketing, an [else] goes with the nearest preceding [if] which doesn't already have an [else]. With bracketing, you can control which if the else belongs to.

On a different subject, Prof. Sterling placed this on the board:
error=false;
!error;
cout << error << '\n';

This will print “0” because the !error didn't change the value of error.
If you wanted to change the value, you should use error = !error;

Talking about switches and comparison operators.
Switch can only compare constants.
Make sure you put breaks with your switch command.
Prof Sterling doesn't like Switch statements, he'd rather use if/elses.

Repetition.
While, for, do while

while (i<=5);

This loop passes the test in terms of syntax, but probably won't do what you want. There is an implied null statement, or nop, between the last paren and the semicolon. If I is less than 5, this will lock in an endless loop.

For a loop to work, it has to halt and it has to execute code.

Note: Remember to initialize variables. You have to do this before any assignment that includes the variable on the right side of the equation.

A while look will technically work without brackets, but probably won't do what you want.

While (i<=n)
i++;
cout<<i<<endl;

This will work. It will add one to i until i = n, then print i. If you wanted to to print i every time, you need to put both statements in brackets.
While (i<=n)
{
i++;
cout<<i<<endl;
}
Professor Sterling gave several examples of errors made in while loops and if blocks. I didn't write them all down..

Advertisements

2 Responses

Subscribe to comments with RSS.

  1. valcpp said, on 2010 03/01 at 22:30

    have you looked at question 1 in sec 4.3?… i cant really figure out how to write that one!

    • bnmng said, on 2010 03/02 at 01:13

      Hi,

      You’ll be writing a program that’s similar to 4.5. At least the if/else structure will be similar, but the statements will be different. The phrasing: “converts the number grade to an equivalent letter grade” may be confusing. All you need to do is something like this:

      if (numgrade >=90)
      ltrgrade=”A”; // A 90 or above is an A
      else if (numgrade >=80) // Don’t have to ask if it’s less than 90.
      ltrgrade=”B”; // An 80 or above (if it’s not an A) is a B


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: