Skip over navigation

Basic Commands in C++

Problems

Arithmetic operators and Basic I/O

Other Basic Statements

Problem : Can you use more than one arithmetic operator in a line of code?

Yes! Be sure to keep order of evaluation in mind (ie. multiplication will be evaluated before subtraction), and in general try to use parentheses to make your expressions clear.

Problem : What are three ways of incrementing a variable?

The first is to use a statement like x = x + 5;. C++ makes this easier with a second method, which looks like x += 5;. The third method increments by 1: x++.

Problem : What is the difference between using the ++ operator before a variable name and using it after the variable name?

If it is used before the variable name, the variable will be incremented before any other calculation is done with your expression. When the operator is used after the variable name, the incrementing takes place after the rest of the expression is evaluated.

Problem : What is endl?

It is a stream manipulator. cout << endl; tells the computer to print out a new line on the screen, and tells it to flush the output stream meaning that it tells the computer to print out anything it was waiting to print out in a buffer.

Problem : How many input variables can the cin command take?

The command can take as many variables as you want to supply it with. However, you should strive to use only one at a time to make it less confusing for your program's users.

Follow Us