I am covering the basics fairly swiftly here so that you can begin writing programs in the right way. We will slow down and look at this material in more detail shortly. This lecture hits the high points.
Some material, in brown, can be skipped.
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!
- (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 |
Simple conditions
x == y | (true if x and y are equal) | |
x != y | (true if x and y are not equal) | |
x < y | (true if x is less than y) | |
x > y | (true if x is greater than y) | |
x <= y | (true if x is less than or equal to y) | |
x >= y | (true if x is greater than or equal to y) |
For example, the following are conditions.
a == b y + 4 < x z != n + 1
Complex conditions
Suppose that A and B are arbitrary conditions. Then the following are also conditions.
A && B | (true if A and B are both true) | |
A || B | (true if either A or B or both are true) | |
!(A) | (true if A is false) |
For example, the following are conditions.
n > m and m > 0 x == 0 || y == 0 !(x > y || y != 0)
Conditions and integers
A condition is a kind of expression.
A condition computes an integer in C++. A true condition has value 1
and a false condition has value 0. For example, expression 5 > 2
has value 1, but expression 2 > 5 has value 0.
If statements and while loops test these conditions. Normally,
the value that is tested is either 0 or 1. In unusual circumstances,
you end up using an integer that is not 0 or 1 as a condition. The
rule is that 0 is treated as false, and all other numbers are treated
as true. So
Warning
The equality test operator is two consecutive equal signs. If
you use just one equal sign, you get an assignment, not an equality
test. So
int y;
int x = 3;
if(x) {
y = 0;
}
else {
y = 1;
}
sets y = 0, since 3 is treated as true.
if(x = y) {
...
}
does not do what you expect it to do. Pay attention to this, and
guard against it in your program.
cin >> n;
You can do several reads in one line. To read n, m and k from the keyboard, write
cin >> n >> m >> k;
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";
#include <iostream.h>
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 (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.
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.
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.
The main function returns an integer. The integer returned is a status value that is sent back to the operating system. Status 0 means that all went well. A nonzero status indicates that an error occurred.
Typically, the main program looks like this.
int main() { Your main program body return 0; }
Here is an example of a main program. It reads a number n from the user and prints a table of the squares from 1 to n. It uses manipulator setw to set the number of characters to use for each number. To use them, you must include <iomanip.h>.
int main() { int n,k; cin >> n; k = 1; while(k <= n) { cout << setw(4) << k << setw(9) << k*k << endl; } return 0; }