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 |
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.