CSCI 2610 Lecture 2 Notes

Remark

This lecture concentrates on basics of C++ expressions and input/output. Additionally, it introduces if statements and while statements. This material is covered in Chaper 2 of Savitch.

Expressions

An expression computes a value. Expressions mostly use notation that should be familiar to you. You can add (operator +), subtract (operator -), negate (also -), multiply (operator *) and divide (operator /).

An additional operator is the mod operator %. Expression a % b computes the remainder when you divide a by b. For example, if you divide 10 by 3, you get a quotient of 6 and a remainder of 2. So 20%6 is 2.

The following are examples of expressions.

    3 * 2
    z + 1
    x + y
    y * z
    a + x * y
    -y
    a % b

Warning. If you divide two integers using the / operator, you get an integer result. Anything to the right of the decimal point is thrown away. So 3/2 yields 1. Watch out for this!

Precedence

Operators have different precedence values. For the arithmetic operators, the precedences are as follows, from high to low.
   -  (unary)
   * / %
   + - (binary)
Operators with the same precedence levels are done from left to right.

You can override the precedence using parentheses. For example
1 + 2 * 3 = 7
(1 + 2) * 3 = 9

Where to use expressions

You can use an expression almost anyplace where you can use a variable or constant. We will see lots of examples of using expressions.

Input from the keyboard

Input is done using the object cin, which represents the keyboard. You read from the keyboard using the "read" operator >>. To get a number from the keyboard and put it into variable n, write
   cin >> n;

You can do several reads in one line. To read n, m and k from the keyboard, write

   cin >> n >> m >> k;

Output to the monitor

The monitor is represented by the object cout. To print a value, use the "print" operator <<. For example, to print a value n on the monitor, write
   cout << n;

You can print several values in one line. To print n, m and k, write

   cout << n << m << k;

There are some special things that you can print. To print a literal string, enclose the string in double quotes. For example, write

   cout << "The value of n is " << n;
You will not get a carriage return printed unless you ask for one. You can use the special value endl to stand for a carriage return, or you can use the special escape sequence \n in a string. The following two lines print the same thing.
   cout << "Here is a line of text" << endl;
   cout << "Here is a line of text\n";

Header files

To use the input and output features, you must include a header file that describes them. Near the top of your program, write
#include <iostream.h>

Conditionals (if statements)

You use an if statement to make a decision while a program is running. The form is
    if(condition) {
      What to do when the condition is true
    }
    else {
      What to do when the condition is false
    }
If you like, you can omit the else part. The default is to do nothing when the condition is false. You write
    if(condition) {
      What to do when the condition is true
    }
For example, to set m = the larger of x and y, write
    if(x > y) {
      m = x;
    }
    else {
      m = y;
    }
An alternative way to do the same thing is
    m = y;
    if(x > y) {
      m = x;
    }

While loops

A while loop is used for doing something repetitively. The general form is
  while (condition) {
    What to to over and over
  }
When the program reaches the top of the loop, it tests the condition. If the condition is true, then it does what is inside the loop (called the loop body) and goes back to the top of the loop to test the condition again. If the condition is false, then the program jumps to the bottom of the loop.

Notes on indentation

C++ is free-form. Except for the inside of string constants (inside double quotes), a line break is considered to be the same as a space.

You should be sure to write your program in a way to make it easy to read. This involves indenting the inside of an if statement or while loop a little bit. A good rule of thumb is to indent two spaces.

Here is an example program, with correct indentation.

  int main ()
  {
    int n,c;
    cout << "What number should I start with? ";
    cin >> n;
    c = 0;
    while(n != 1) {
      if(n % 2 == 0) {
        n = n / 2;
      }
      else {
        n = 3*n + 1;
      }
      c = c + 1;
    }
    cout << "I did " << c << " steps\n";
  }

Two steps that are done one after the other should be indented the same amount.

When should I indent?

If you want to make the programming process easy, keep your program readable during development. Do not try to develop a poorly indented program and then fix it up after it works. If you do, you are missing the point of indentation. Keeping a program well indented makes the program easier to work on.

Note. If you use the emacs text editor, your program will use C++ mode. In that mode, typing a tab indents the current line according to what emacs thinks is good indentation. This makes it easy to keep a program indented. Also, when you type a right brace, emacs shows you the matching left brace. This makes it easy to detect mismatched braces.

Emacs was designed for software development, and I highly recommend it.