Basic computer concepts


Programming languages


First sample program


#include 



int main()

{

	// Declare two integer variables

	int theInt, theIntSquared;



	// Prompt for input and retrieve an integer

	cout << "Enter an integer: ";

	cin >> theInt;



	// Perform calculation

	theIntSquared = theInt * theInt;



	// Display the result

	cout << theInt << " squared is " << theIntSquared << endl;



	// These next few lines may not be necessary

	// for some compilers/environments

	

	// Declare a character variable

	char wait;

	// Wait for user to input a character

	cin >> wait;



	// Indicate success status to OS

	return 0;

}




Variables


Simple types

bool
constants: true, false


char
constants: 'j', '2', '\n', '\t', '\\', '\0', '\''


int
constants: 52, -3, 072, 0xF1F1


double
constants: 3.1415, -4.0, 8.27E18, 1.5e-5


string constants
"Hello, world!\n", "\"Hello, world\" I said.", ""

Constant variables


	const double Pi = 3.141592;

	const int MAX_GUESSES = 10;

Variable assignment


	theIntSquared = theInt * theInt;

	x = y = z = 0;

	x = 5 + (z = 2);


Arithmetic operators

+ - * / %

Examples:

7 / 2 -> 2
7.0 / 2 -> 3.5
double(7) / 2
6.0 / 2 -> 3.0
1/2 -> ?

54 % 7 -> 5

int x, y = 6;
double z = 5.2;
x = y * z;
int * double -> double, int = double -> ?


Precedence and order of evaluation

Highest to lowest:
()
* / %
+ -
=
Left to right: * / % + -

Right to left: =

Undefined: ()

Examples:
8 + 6 * 4 / 2 - 1 = (8 + ((6 * 4) / 2)) - 1
x = y = z = 0 -> x = (y = (z = 0))

Shortcut assignment operators

+= -= *= /= %= ++ --

theInt = theInt + 5 ==> theInt += 5
theInt = theInt + 1 ==> theInt += 1 ==> theInt++
theInt++ vs. ++theInt

Same precedence as =: += -= *= /= %=

Higher precedence than */%: ++, --

Expression evaluation program


int main()

{

	int m, n = 2;



	// This expression does nothing (has no "side-effects")

	1 * 2 + 3 / 4 - 5 - 6 + 7 - 8;



	// This one is equivalent to n = n + 6

	n += 6;

	cout << "n = " << n << endl;



	// This one is equivalent to n = n / (2 + 1)

	n /= 2 + 1;

	cout << "n = " << n << endl;



	// This is equivalent to n = n + 1, or n += 1

	n++;

	cout << "n = " << n << endl;



	// What's the result here?

	m = n++;

	cout << "m = " << m << ", n = " << n << endl;



	// How about here?

	m = ++n;

	cout << "m = " << m << ", n = " << n << endl;



	// This expression has unpredictable side-effects.

	n = (n = 10) * 4 + (n = n * 10) * 2;

	cout << "n = " << n << endl;



	return 0;

}

Output:

n = 8

n = 2

n = 3

m = 3, n = 4

m = 5, n = 5

n = 240


Standard Input & Output

Input stream (istream) cin

Output stream (ostream) cout

cin >>
cin >> anIntVar >> aDoubleVar >> aCharVar;

cout <<
cout << anIntVar + 2 << aDoubleVar / 3 << aCharVar;

Double value output formatting (consider this a formula)
cout.setf(ios::fixed)
cout.setf(ios::showpoint)
cout.precision(2);

Lab:

Input a length given in whole centimeters. Convert to inches (cm / 2.54) and output the number of whole feet and remaining inches. You may convert double to int using a typecast, even though the effect of doing so is compiler dependent (most compilers truncate). Display inches to 2 decimal places.

Ex. 100 cms = 3 ft. 3.37 in.