Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

Similar presentations


Presentation on theme: "1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)"— Presentation transcript:

1 1 Midterm Review COMP 102

2 Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.) l Exam time 6:30 -8:30pm. l But come a little early to find your seat assignment!

3 COMP102 Prog Fundamentals I: Midterm Review /Slide 3 3 Seating Plan l Lecture Theater A: L2, L4 l Lecture Theater B: L1, l Lecture Theater C: Lab3a, Lab3b l Lecture Theater E: Lab3c, Lab3d

4 Know l INTRODUCTION TO COMPUTER SYSTEMS l DATA TYPES, VARIABLES AND CONSTANTS n Data types n Identifiers, variables and constants n Type casting

5 Know l STRUCTURE OF C++ PROGRAMS n Compiler directive #include n I/O operators cin & cout n Assignment statements n Expressions, operators and operator precedence l SELECTION STRUCTURES n Relational and logical operators n Logical expressions n If, if-else, if-else-if, and nested if statements n Dangling else n switch statement

6 Know l LOOPS n for loops n while loops n do-while loops n Increment and decrement operations n Nested loops l PROGRAM DESIGN AND FUNCTIONS n Top-down design and stepwise refinement n Function prototypes and function definitions n Parameter Passing: pass-by-value, pass-by- reference

7 Watch Out For l Programming style problems n indentation l Nested Loops [EX 1] for(a = 1; a < 10; a++){ for(a =1; a < 10; a++) cout << a;// what happens here? }

8 COMP102 Prog Fundamentals I: Midterm Review /Slide 8 8 C++ Data Type A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domain for the type. C++ contains 5 standard types:

9 COMP102 Prog Fundamentals I: Midterm Review /Slide 9 9 l Identifiers appear in black in Visual C++. n An identifier is a name for a variable, constant, function, etc. n It consists of a letter followed by any sequence of letters, digits, and underscores. Examples of valid identifiers: First_name, age, y2000, y2k Examples of invalid identifiers: 2000y Identifiers cannot have special characters in them. For example: X=Y, J-20, ~Ricky,*Michael are invalid identifiers. Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI, whoami are unique identifiers. C++ identifiers

10 COMP102 Prog Fundamentals I: Midterm Review /Slide 10 10 Constant declarations l Constants are used to store values that never change during the program execution. l Using constants makes programs more readable and maintainable. Syntax: const = ; Examples: const double US2HK = 7.8; //Exchange rate of US$ to HK$ const double HK2TW = 4.11; //Exchange rate of HK$ to TW$ const double US2TW = US2HK * HK2TW; //Exchange rate of US$ to TW$

11 COMP102 Prog Fundamentals I: Midterm Review /Slide 11 11 l Variables are used to store values that can be changed during the program execution. l A variable is best thought of as a container for a value. 3445 y -3.14 Syntax: ; = ; Examples: int sum; int total = 3445; char answer = ' y ' ; double temperature = -3.14; Variable declarations

12 COMP102 Prog Fundamentals I: Midterm Review /Slide 12 12 Type Conversion l You can change the type of an expression with a cast operation. l Syntax: (type) expression (type) variable l Example [EX 2]: (char) 65 // returns ‘A’ (char) 7// returns bell (int) ‘a’ // returns 97 (int) 10.5 // returns 10

13 COMP102 Prog Fundamentals I: Midterm Review /Slide 13 13 General form of a C++ program // Program description #include directives int main() { constant declarations variable declarations executable statements return 0; }

14 COMP102 Prog Fundamentals I: Midterm Review /Slide 14 14 Assignment Statement l The value of a variable can be changed through an assignment statement. l The syntax of the assignment statement is: = ; l An expression is a sequence of operands and operators. l The expression to the right of the equal sign is evaluated and its value becomes stored in the variable on the left hand side.

15 COMP102 Prog Fundamentals I: Midterm Review /Slide 15 15 Operators and Precedence l Which of the following is equivalent to mx + b ? n (m * x) + b n m * (x + b) l Operator precedence tells the order in which different operators in an expression are evaluated. l Standard precedence order ( ) Evaluated first, if nested then evaluate the innermost first. * / % Evaluated second. If there are several, then evaluate from left-to-right. + - Evaluate third. If there are several, then evaluate from left-to-right.

16 COMP102 Prog Fundamentals I: Midterm Review /Slide 16 16 Standard Input/Output

17 COMP102 Prog Fundamentals I: Midterm Review /Slide 17 17 Relational Operators Relational operators are used to compare two values to form a condition. MathC++Plain English === equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] << less than  <= less than or equal to >> greater than  >= greater than or equal to  != not equal to

18 COMP102 Prog Fundamentals I: Midterm Review /Slide 18 18 Operator Precedence Which comes first? * / % + - = > == != = Answer:

19 COMP102 Prog Fundamentals I: Midterm Review /Slide 19 19 The Boolean Type C++ contains a type named bool for conditions. l A condition can have one of two values: true (corresponds to a non-zero value) false (corresponds to zero value) l Logical operators can be used to form more complex conditional expressions. The and operator is && The or operator is || The not operator is !

20 COMP102 Prog Fundamentals I: Midterm Review /Slide 20 20 Operator Precedence Which comes first? ! * / % + - = > == != && || = Answer:

21 COMP102 Prog Fundamentals I: Midterm Review /Slide 21 21 The if-else Statement l Syntax if (condition) Action_A else Action_B l if the condition is true then execute Action_A else execute Action_B l Example: if(v == 0) cout << "v is 0"; else cout << "v is not 0"; condition Action_A Action_B true false

22 COMP102 Prog Fundamentals I: Midterm Review /Slide 22 22 “Dangling Else” Problem [EX 3] Use extra brackets { } to clarify the intended meaning, even if not necessary. int test_number=-1, test_number_2=1; int result = 0; if(test_number > 0) { if(test_number_2 > 0) result = 1; else //dangling else result = 2; } cout << "Result = " << result << endl;

23 COMP102 Prog Fundamentals I: Midterm Review /Slide 23 23 A Loop Statement l Syntax while (condition) action l How it works: n if condition is true then execute action n repeat this process until condition evaluates to false l action is either a single statement or a group of statements within braces. condition action true false

24 COMP102 Prog Fundamentals I: Midterm Review /Slide 24 24 Another Loop Statement l Syntax for (initialization; condition; update) action l How it works: n execute initialization statement n while condition is true –execute action –execute update condition action true false initialization update

25 COMP102 Prog Fundamentals I: Midterm Review /Slide 25 25 Yet Another Loop Statement l Syntax do action while (condition) l How it works: n execute action n if condition is true then execute action again n repeat this process until condition evaluates to false. l action is either a single statement or a group of statements within braces. action true false condition

26 COMP102 Prog Fundamentals I: Midterm Review /Slide 26 26 Postfix expressions Two postfix operators: ++ (postfix increment) -- (postfix decrement) the effect of a++ is the same as a = a + 1 the effect of a-- is the same as a = a - 1

27 COMP102 Prog Fundamentals I: Midterm Review /Slide 27 27 Prefix expressions Two prefix operators: ++ (prefix increment) -- (prefix decrement) the effect of ++a is the same as a = a + 1 the effect of --a is the same as a = a - 1

28 COMP102 Prog Fundamentals I: Midterm Review /Slide 28 28 Top-Down Design l A program is divided into a main module and its related modules. Each module is in turn divided into submodules until the resulting modules are intrinsic; that is, until they are implicitly understood without further division.

29 COMP102 Prog Fundamentals I: Midterm Review /Slide 29 29 User-Defined Functions l C++ programs usually have the following form: // include statements // function prototypes // main() function // function definitions

30 COMP102 Prog Fundamentals I: Midterm Review /Slide 30 30 Function Definition A function definition has the following syntax: ( ){ } For example: Definition of a function that computes the absolute value of an integer: int absolute(int x){ if (x >= 0)return x; else return -x; }

31 COMP102 Prog Fundamentals I: Midterm Review /Slide 31 31 Function Prototype l The function prototype declares the input and output parameters of the function. l The function prototype has the following syntax: ( ); Example: A function that returns the absolute value of an integer is: int absolute(int);

32 COMP102 Prog Fundamentals I: Midterm Review /Slide 32 32 Parameter Passing l There are two ways to pass parameters into a function: Pass by value - The arguments of a function retain their original values after the function’s execution. (int variable_name) Pass by reference - The address of a variable rather than its value is sent to the called function. (int& variable_name)

33 COMP102 Prog Fundamentals I: Midterm Review /Slide 33 Multiple Selection: The switch Statement Syntax: switch ( ) { case : ; break; case : ; break; case : ; break; default : ; }

34 COMP102 Prog Fundamentals I: Midterm Review /Slide 34 34 Relational Operators and the Type char '0' through '9' have ASCII code values 48 through 57 '0' < '1' <... < '9' 'A' through 'Z' have ASCII code values 65 through 90 'A' < 'B' <...< 'Z' 'a' through 'z' have ASCII code values 97 through 122 'a' < 'b' <...< 'z'

35 COMP102 Prog Fundamentals I: Midterm Review /Slide 35 35 Scope n The scope of an identifier does not apply if the same identifier is declared in an inner block. n A global declaration of an identifier is made outside the bodies of all functions, including the main function. It is normally grouped with the other global declarations and placed at the beginning of the program file. n A local declaration of an identifier is made inside a block of code which could be the body of a function. n Globally declared identifiers can be accessed anywhere in the program. n Locally declared identifiers cannot be accessed outside of the block they were declared in.


Download ppt "1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)"

Similar presentations


Ads by Google