Introduction To Programming Using C++: Lecture Two: Further Syntax
Introduction To Programming Using C++: Lecture Two: Further Syntax
Carl Gwilliam
gwilliam@hep.ph.liv.ac.uk
http://hep.ph.liv.ac.uk/~gwilliam/cppcourse
Variable i initialised to 1.
Process statements in block.
Increment i by 1.
If i less than 10 continue.
Increment Operators
factor++
The ++ operator increments the value of an integer
variable by 1.
This is the origin of the name C++ !
Similarly, the -- operator decrements the value of an
integer variable by 1.
i++
i--
//the same as i = i + 1
//the same as i = i - 1
factor++
++nfactors
int x = 2, y = 0;
y = x++;
// x incremented after assignment -> y = 2
y = ++x;
// x incremented before assignment -> y = 4
The increment and decrement operators are unary
operators. The mathematical operators are binary
operators.
Logical Operators
if ((!(myNumber % factor)) && (factor != 1))
PRECEDENCE
Logical negation
Logical OR
int x = 1, y = 1, z = 2;
if (x == 1 && y > 2 || z < 3) {
cout << condition true\n;
}
condition true
> stats1.exe
> stats1.exe
0.816397
456
Adding whitespace
Source code needs spacing out
to increase readability.
The efficiency of the program
does not decrease if whitespace
is liberally used throughout the
source code.
Often useful to add line breaks to
partition chunks of code
[stats2.cpp].
Variable Naming
Variable names bear no relation to their role in the program.
The purpose of the program can be interpreted much
easier if the variables have names that indicate their role
within the program.
In other words, descriptive names for variables enable
other developers to follow the "story" told by the code.
Some conventions:
Sum_of_value
SumofValue
sumOfValue
iSum
// bad
s_1 = s1 + s2 + s3;
// better
sum = sample1 + sample2 + sample3;
Declaration Styles
All the variables are declared at the top of the program.
int main() {
int x = 1;
// error
y = x;
:
}
BUT:
It is preferable to declare variables close to where they
are first assigned a value.
This seems too chaotic, why bother? This practice is
introduced to implement an important feature of C++
int x = 4; int y = 2;
if (y > 0) {
cout << "x = " << x << endl;
}
if (y > 1) {
int x = 7; //diff object
cout << "x = " << x << endl;
x++;
}
// original object
cout << "x = " << x << endl;
See [stats3.cpp].
x=4
statements
13
22
34
10
139
float sampleSwap;
if (sample1 > sample2) {
sampleSwap = sample1;
sample1 = sample2;
sample2 = sampleSwap;
}
if (sample1 > sample3) {
sampleSwap = sample1;
sample1 = sample3;
sample3 = sampleSwap;
}
if (sample2 > sample3) {
sampleSwap = sample2;
sample2 = sample3;
sample3 = sampleSwap;
}
Arrays
type variable [size]
An array is an ordered collection of objs of same type.
An object stored in an array is referred to as an array
element.
In the 1st example array x holds 10 objects of type int.
The first element in the x is x[0] and the last is x[9].
int x[10];
int y = x[4];
float x[5] = {5.0,4.3,6.1,2.1,9.2};
int x[2][3] = {{1,2,3},{4,5,6}}; // 2D
[stats4.cpp]
Error Catching
There is no way of handling incorrect input by the user.
If the input value for the routine is not in range 1 to 3 the
code will compile but the program will not indicate an
error back to the user.
Do not assume that
everyone who uses
your code will be
competent!
> stats
What would you like to do?
1 - Calculate the mean
2 - Calculate the standard deviation
3 - Order the sample lowest first
4
How big is the sample?
3
Sample 1: 4
Sample 2: 5
Sample 3: 6
>_
Error Catching
The solution is to replace the separate if blocks with one
if - else if - else block. if (request == 1) {
if (request == 1) {
:
}
if (request == 2) {
:
}
if (request == 3) {
:
}
:
} else if (request == 2) {
:
} else if (request == 3) {
:
} else {
cerr << "Routine doent exist\n;
return 1;
}
[stats5.cpp]
Unnecessary Code
Code is unnecessarily verbose.
It is good coding practice to attain a balance between
conciseness and clarity
Multiplication
*=
Division
/=
do {
statement;
:
} while (expression)
//increment in
//condition
int x = 0;
while (x++ < 10) {
:
}