SI204

Class 6: IF I

Reading

Section 2.1 and Appendix 2 of Absolute C++

 

Lecture

A Note on expressions, precedence and associativity

What happens when an expression like 2 + 3 * 5 is evaluated? Do I get 17 or 25? Well, your math classes should've taught you that 17 is the answer, and indeed that's true in C++ as well.

When you have two different operators in an expression and parenthisization does not tell you which operation is performed first, the relative precedence of operators is what determines which operation is performed first.

Since * has a higher precedence than +, the expression 2 + 3 * 5 is evaluated like 2 + (3 * 5). But what about when both operators are the same, or both have the same precedence? What happens with a * b * c?

When you have two identical operators in an expression (or two different operators with the same precedence) and parenthisization does not tell you which operation is performed first, the associativity of the operator(s) is what determines which operation is performed first.

The associativity of * and / (which both have the same precedence) is left-to-right, so a * b * c is evaluated as (a * b) * c. This can matter in C++. (For example, what does 3 / 4 * 1.5 evaluate to?)

However ... Always use parentheses rather than relying on subtle precedence and associativity rules!

This table lists the operators and their associativity. They are grouped together on lines with operators of the same precedence, and the lines go from highest precedence at the top, to lowest at the bottom. You should know about precedence and associativity, and you should be able to use tables like this to fix precedence and associativity related bugs, but rely on parentheses when you're unsure.

Challenge question: Why does the expression cout << 3.0 << " percent" rely on associativity of the << operator to make sense?

 

Decisions

The ability to make decisions and react to different input is a key part the power of computers. For example, we would certainly expect it to be possible to write a program that reads in a number and writes "even" if the number is even, and "odd" if the number is odd. In C++ (as in English!) "if" is the key to expressing this.

int k;
cin >> k;
if (k is even)
{
  cout << "even" << endl;
}
else
{
  cout << "odd" << endl;
}

Of course we've got to figure out some C++ that will do the "k is even" for us. What's inside the ()'s in an if statement needs to be an expression that evaluates to type boolean - if the expression evaluates to true the first block of code (code surrounded by {}'s forms a block) is executed, otherwise the block following the else is executed. This is called the test condition.

A number is even if 2 divides it evenly, i.e. if its remainder when divided by 2 is 0. So for k to be even, k % 2 must be zero. We can test this using the == operator. A single "=" in C++ is used for assigning values to variables, a double "=" (i.e. ==) is used to test whether two values are equal. A "==" expression evaluates to an object of type bool that is true if the left and right-hand expressions are equal, and false otherwise. Thus, (k % 2) == 0 is the test condition we need.

int k;
cin >> k;
if ((k % 2) == 0)
{
  cout << "even" << endl;
}
else
{
  cout << "odd" << endl;
}

[The Complete Program]

 

Scope

We might consider solving the above problem in a slightly different way: We'll assign a variable of type string the value "even" if k is even and "odd" otherwise. Then, after the if-statement, we'll do the printing. We might implement it like this:

 

if ((k % 2) == 0)
{
  string s;
  s = "even";
}
else
{
  string s;
  s = "odd";
}
cout << s << endl;

However, when we try to compile this the compiler complains that s is "an undeclared identifier", which is exactly what it would say if we'd never defined s at all. Well, as far as the compiler is concerned when it processes the "cout << s << endl;" statement, we haven't defined s. The problem is caused by the scope of variables in C++.

In C++, a variable only exists from the point at which it is declared to the } that closes off the innermost block in which it was declared. So the s that we define inside the else-block is invisible, is unknown, does not exist outside of that else-block. In particular, this is true for our cout-statement. The scope of a variable is the portion of the code that "sees" the variable, i.e. that knows it exists. The scope of a variable ends with the innermost block in which it was defined.

To fix up this version of our even/odd program, we simply need to move the declaration of s outside of the if/else-blocks so that its scope extends to cout statement. This'll work:

 

string s;
if ((k % 2) == 0)
{
  s = "even";
}
else
{
  s = "odd";
}
cout << s << endl;

 

 

Relational Operators

The "==" is an example of a relational operator. Relational operators make comparisons of their left and right-hand arguments, and return bool values accordingly. They are:

== (equal)
!= (not equal)
<  (less than)
>  (greater than)
<= (less than or equal to)
>= (greater than or equal to)

As you can see from the operator precedence table, they have lower precedence than the arithmetic operators, so things like 2*k > k + 5 do what we'd like them to do - they evaluate the arithmetic expressions on the left and right, then they apply the ">" operator to compare the two values. This means, for example, that instead of writing ((k % 2) == 0) we could write (k % 2 == 0) and get the same result.

 

Blocks

Code in between {}'s forms a block. So the if is followed by a block (the then block) and the else is followed by a block (the else block). You've already seen one example of a block: The block following main(). Anything you can write inside of the main block, you can write inside of any block. This means that our then blocks and else blocks can declare variables, read input, make assignments to variables ... anything. So, suppose I wanted to write a program that would compute areas of circles and triangles for the user, first asking the user which kind of object he was interested in. My program might look something like:

int main()
{
  // Read type of object: circle or triangle
  string s;
  cout << "Do you have a circle or a triangle? ";
  cin >> s;
 
  if (s == "circle")
  {
    // Compute area of circle
  }
  else
  {
    // Compute area of triangle
  }
 
  return 0;
}

... where we'd have to fill in the then-block with code that gets the radius of the circle and computes and outputs its area, and we'd have to fill in the else-block with code that reads in the base and height from the user and computes and outputs the triangle's area.

 

Code for circle area

Code for triangle area

    double r,Pi;
    Pi = 3.14159265358979324;
    cout << "Enter the radius of your circle: ";
    cin >> r;
    cout << "Area equals " << Pi*r*r << endl;
    double b,h;
    cout << "Enter base length: ";
    cin >> b;
    cout << "Enter height: ";
    cin >> h;
    cout << "Area equals " << 1/2.0*b*h << endl;

Each of these "miniprograms" can be placed in its appropriate block, and we get the whole program.

 

Dropping the else

Sometimes you have a condition which, if it's true, should cause you to do some extra work, but which, if it's false, should have no effect on the program. For example, suppose we read an int from the user and we want to change it to its absolute values and print it out. We'd probably write something like this:

int k;
cin >> k;
 
if (k < 0)
{
  k = -1*k;
}
else
{
  // nothing to do!
}
 
cout << k << endl;
      

When there's nothing to do in the else-block, we can simply drop it. (Sometimes your if-statement is written so that the then-block is empty and the else-block isn't. You can't simply drop the then-block, so first rewrite your if-statement with a new condition so that the then-block contains the work.)

int k;
cin >> k;
 
if (k < 0)
{
  k = -1*k;
}
 
cout << k << endl;
      

 

Problems

1.      Remainders - this is a toy problem that forces you to face a fundamental problem in computer science: swapping values. This is important to know and understand!

2.      Converting 12-hour clock time to 24-hour clock time. Here is a first try at solving this problem. Can you see what's wrong? There are two bugs.  First, try input 10:02AM.  Can you find the second bug?  Here are two different solutions to this problem: Version 1 and Version 2.

3.      Determining the state of H20 - this gives you a good look at "nested" if statements.

4.      Real & Complex Roots of a Quadratic Polynomial


Assoc Prof Christopher Brown

Last modified by LT M. Johnson 08/30/2007 03:47 PM