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

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Structured Programming Programming. COMP102 Prog Fundamentals I: Structured Programming /Slide 2 Structured Programming l Structured programing is the.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Chapter 2: Introduction to C++.
Chapter 4 Making Decisions
Introduction to C Programming
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
If Statements & Relational Operators, Part 2 Programming.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Programming Introduction to C++.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Basic Of Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Flow of Control Part 1: Selection
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
CPS120: Introduction to Computer Science Decision Making in Programs.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Computing and Statistical Data Analysis Lecture 2
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Expression Review what is the result and type of these expressions?
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
3 Control Statements:.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

1 Midterm Review COMP 102

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!

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

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

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

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

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? }

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:

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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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$

COMP102 Prog Fundamentals I: Midterm Review /Slide 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 y Syntax: ; = ; Examples: int sum; int total = 3445; char answer = ' y ' ; double temperature = -3.14; Variable declarations

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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.

COMP102 Prog Fundamentals I: Midterm Review /Slide 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.

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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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 !

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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

COMP102 Prog Fundamentals I: Midterm Review /Slide “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;

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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.

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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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; }

COMP102 Prog Fundamentals I: Midterm Review /Slide 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);

COMP102 Prog Fundamentals I: Midterm Review /Slide 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)

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

COMP102 Prog Fundamentals I: Midterm Review /Slide 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'

COMP102 Prog Fundamentals I: Midterm Review /Slide 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.