× Home (CS 111) Lectures Assignments Review Ryba's Website

C++ Language Basics

img1
A typical C++ program will look something like this:
img2
Compiling and running this program looks like this:
img3
When you run the program, it will start at the beginning of the main function, execute each statement in order until it reaches the return statement at the end of the main function, and then exit (return statements will be explained later).
Each statement ends with a semicolon (;).
C++ is case-sensitive.

Comments

Comments are lines of text that are not execute. Comments are for our eyes only; compilers ignore them.


Output

We use cout to print to the console.
img6
When this statement is executed, whatever is to the right of the << is printed to the console. It may be either a literal or a variable (of any type)
C++ is a case-sensitive language. COUT, Cout, and cout are not the same things.
You can use chaining to print multiple literals and/or variables with a single statement.
img7


Literals, Data types, and Variables

Both literals and variables have a value and a data type. A literal represents a specific value. A variable has a name which refers to a location in memory, and its value may change.
Data types are what we use to represent data. Keywords are words reserved by the programming language. The keywords below are only data types we will use in this course.

Data Type Description Examples of Literals
int Integer (positive or negative) 5, -3 , 21927303
double Double-precision floating-point 3.14, -2.19, .00009
bool true or false true, false
char A single character 'c', '2', '$'
string A sequence of 0 or more characters "Hello", "", "3.14"

Some more notes:

Declaring Variables

Variables store data so that we can process the data. Variables have data types. We must declare (create) variables before we can use them. Variable names:


img8
x and y still refer to different locations in memory, so modifying x later in the program will not affect y.
Note: If you use a variable that you did not initialize, its value may be garbage, which will cause unpredictable results.


Input

We use cin to get input from the user (upon hitting ENTER).

img9
When the cin statement is executed, the program will wait until the user types something and presses enter. Whatever the user types is stored in the variable name.

Operators

An expression is a sequence of operators and operands (an operand can be a literal or a variable).

Below are some operators from highest to lowest precedence, and their associativity.

Precendence Operator Description Associativity
1 * , / , % multiplication, division, modulo left-to-right
2 + , - addition, subtraction left-to-right
3 = assignment right-to-left

See this page for more resources.

img10
  1. 1 + x is evaluated first because it is inside parentheses. It evaluates as 3. The value of x is not changed.
    y = 6 / 3 * 2
  2. 6 / 3 is evaluated next because / and * have left-to-right associativity. It evaluates as 2.
    y = 2 * 2
  3. 2 * 2 evaluates as 4.
    y = 4
  4. A value of 4 is assigned to y.

Notes about operators:

img11


Casting

Sometimes we want to convert from one type to another. For instance convert an int to a double for a more precise result. This can be done through type casting.
Type casting is a temporary change from one type to another.
img12