Download presentation
Presentation is loading. Please wait.
Published byOliver Stewart Modified over 9 years ago
1
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo
2
1. Introduction to Computer Systems Hardware Input Process Memory Output Software System Operating system Translation System Application
3
2. Data Types, Variables and Constants Data Types void, int, char, float, double, bool Variables Name / Identifiers Name Start with a-z/A-Z Contain only a-z/A-Z/0-9/$/_ Case sensitive Variables Declaration ; = ;
4
2. Data Types, Variables and Constants Constants Never changed in the program const = ; Type casting int( ) double( ) bool( ) Common Mistakes char a = A; Should be char a = ‘A’;false for 0, true for others1 for true, 0 for falseascii value of characterstruncate the decimal place
5
3. Structure of C++ Programs #include using namespace std; Match with, for time()for srand()/rand()for cin/coutfor setw()/setprecision()
6
3. Structure of C++ Programs cin >> User input cout << Screen output Common mistakes cin >>a<<endl; Should be cin>>a; cout<<endl;
7
3. Structure of C++ Programs Rules for division double / double = double double / int = double int / double = double int / int = int Random number srand(time(0)); int ran = rand() % x; truncate the decimal placeRandom no: 0 to x-1
8
3. Structure of C++ Programs Order of Precedence Parentheses ( … ) Post-increment/decrement i++ i-- Unary operators ! Pre-increment/decrement ++i --i Multiplicative operators * / % Additive operators + - Relational ordering = > Relational equality == != Logical and && Logical or || Assignment = += -= *= /= %=
9
4. Selections condition action true false if statement condition action true false action if-else statement
10
4. Selections condition action true false action if-else if statement condition action false true selector action Value1 action switch statement action Value2 Value3
11
4. Selections Truth Table E1E2E1 && E2 true false truefalse E1E2E1 || E2 true falsetrue falsetrue false E!E truefalse true
12
5. Loops for loop for(i= ; i ; i ) {…} while loop while ( ) {…} do-while loop do {…} while ( )
13
6. Functions Return statement void = no return statement or return nothing other = return value to the caller through the function name Pass by value parameters inside and outside functions is not related Pass by reference parameters inside and outside functions is related Value changed inside functions will affect outside Value changed outside functions will affect inside
14
6. Functions (03 past paper Q6) #include <iostream.h> int Test(int&, int, int&); int a = 2; int b = 3; int main() { int b = 4; int c = 5; int d ; d = Test(a, b, c); b = b + 3; cout << a << ' ' << b << ' ' << c << ' ' << d << endl; return 0; } int Test(int& z, int x, int& y) { z = 3; x = b; a = y + a; cout << z << ' '<< --x << ' ' << y++ <<' ' << a << endl; return (y - x); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.