














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of the computing and statistical data analysis lecture series by g. Cowan from the royal holloway university of london. In this lecture, the basics of c++ programming are covered, including variables, data types, simple input/output, and basic control structures such as if, else, while, and for loops.
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!















int main(){ int numPhotons; // Use int to count things double photonEnergy; // Use double for reals bool goodEvent; // Use bool for true or false int minNum, maxNum; // More than one on line int n = 17; // Can initialize value double x = 37.2; // when variable declared. char yesOrNo = ‘y’; // Value of char in ‘ ‘ ... }
int main(){ bool aOK = true; // true, false predefined constants double x, y, z; x = 3.7; y = 5.2; z = x + y; cout << "z = " << z << endl; z = z + 2.8; // N.B. not like usual equation cout << "now z = " << z << endl; ... }
enum { RED, GREEN, BLUE };
! const int RED = 0; const int GREEN = 1; const int BLUE = 2;
enum { RED = 1, GREEN = 3, BLUE = 7 }
int n, m; n = 5; m = 3; int ratio = n/m; // ratio has value of 1
int nModM = n%m; // nModM has value 2
int n, m; n = 5; m = 3; bool b = n < m; // value of b is false
greater than > greater than or equals >= less than < less than or equals <= equals == not equals !=
(n < m) && (n != 0) (false) (n%m >= 5) || !(n == m) (true)
n = n + m n += m n = n - m n -= m n = n * m n *= m n = n / m n /= m n = n % m n %= m
! if ( boolean test expression ){ Statements executed if test expression true }
! if ( expression1 ){ Statements executed if expression1 true } else if ( expression2 ) { Statements executed if expression1 false and expression2 true } else { Statements executed if both expression1 and expression2 false }
! if ( x > y ){ x = 0.5*x; }
! if ( x > y ) { x = 0.5*x; }
if ( x > y ) x = 0.5*x;
while( boolean expression ){ // statements to be executed as long as // boolean expression is true } while (x < xMax){ x += y; ... }
do { // statements to be executed first time // through loop and then as long as // boolean expression is true } while ( boolean expression )
int sum = 0; for (int i = 1; i<=n; i++){ sum += i; } cout << "sum of integers from 1 to " << n << " is " << sum << endl;
int n; bool gotValidInput = false; do { cout << "Enter a positive integer" << endl; cin >> n; gotValidInput = n > 0; } while ( !gotValidInput );
// loop over pixels in an image for (int row=1; row<=nRows; row++){ for (int column=1; column<=nColumns; column++){ int b = imageBrightness(row, column); ... } // loop over columns ends here } // loop over rows ends here