Uploaded by Mary Grace Lagdamen Perocho

REVIEW LESSONS

advertisement
REVIEW LESSONS
ORDER OF PRECEDENCE
Example
• 3* 7 – 6 + 2 * 5 / 4 + 6
Answer:
= (((3 * 7) - 6) + ((2 * 5) / 4)) + 6
= ((21- 6) + (10 /4) ) + 6
= ((21 – 6) + 2) + 6
(evaluate *)
(evaluate /. Note: that this is an integer division)
= (15 + 2) + 6
(evaluate - )
= 17 + 6
(evaluate first + )
= 23
(evaluate first + )
Expressions
• Three types of expressions
1. Integral Expressions
2. Floating – Point (decimal expressions)
3. Mixed expressions
1. Integral Expressions
• All operands in the expressions are integers
• It yields an integral result.
Example:
2+3*5
3+x–y/7
x + 2 * (y – z) + 18
2. Floating – Point (decimal expressions)
• All operands in the expressions are floating – points (
decimal numbers)
• It yields an floating – point result
Example:
12.8 * 17.5 – 34.50
x * 10.5 + y – 16.2
3. Mixed Expressions
• It contains both integers and decimal numbers
Example:
2 + 3.5
6 / 4 + 3.9
5.4 / 2 – 13.6 + 18 /2
How to evaluate a mixed expressions
1. When evaluating an expressions in a mixed
a) If the operator has the same types of operands (that is, either
both integers or both floating – point numbers, the operator is
evaluated according to the type of the operands. Integer
operands yield integer results; floating – point numbers yield
a floating – point results.
How to evaluate a mixed expressions
1. When evaluating an expressions in a mixed
b) If the operator has both types of operands (that is, one is
integers and the other is a floating – point numbers), then
during calculation, the integer is changed to a floating – point
number with the decimal part of zero and the operator is
evaluated. The result is a floating – point number.
How to evaluate a mixed expressions
2. The entire expressions is evaluated according to the
order of precedence rules; * , /, and % are evaluated
before the +, and – operators
- Operators having the same level are evaluated from left to right
- Grouping are allowed for clarity.
Example
Mixed Expressions
Evaluation
Rule Applied
3 / 2 + 5.5
= 1 + 5.5
= 6.5
3 / 2 = 1 (integer division; Rule 1 (a)
(1 + 5.5
= 1.0 + 5.5 (Rule 1 (b)
= 6.5)
15.6 / 2 + 5
= 7.8 + 5
15.6 / 2
= 15.6 / 2.0 (Rule 1 (b)
= 7.8
7.8 + 5
= 7.8 + 5.0 / 2.0 ( Rule 1 (b)
=12.8
= 12.8
4 + 5 / 2.0
4 * 3 + 7 / 5 – 25.5
• Examples illustrate that an integer is not converted to a
floating – point number unless to be evaluated has one
integer and one floating - point
Type of Conversion (Casting)
• When was the integer value changed to a floating – point
value with a decimal part
• Implicit type coercion
• Occurred when a value of one data type is automatically
changed to another data type
• Cast operator (is an explicit type conversion ) use to
avoid the implicit type coercion
• also called type conversion or type casting
Form:
• static_cast <dataTypename> (expression)
• static_cast it is a reserved word in C++
Expressions
Evaluates to
static_cast <int> (7.9)
7
static_cast <int> (3.3)
3
static_cast <double> (25)
25.0
static_cast <double> (5 + 3)
static_cast <double> (8) = 8.0
static_cast <double> (15) / 2
static_cast <double> (15) = 15.0
= 15.0 / 2.0 = 7.5
Note:
• In C++ the cast operator can also take the form
dataType(expression). This form is called C-like casting.
For example, double (5) = 5.0, and int (17.6) = 17. static
cast is more stable than C- like casting.
string Type
• It is a programmer – define dtype
Prior to ANSI / ISO C++ language standard C++ library did
not provide a string data type. Compilers vendor often
supplied their own programmed – defined string type, and
the syntax and semantics of string operations often varied
from vendor to vendor.
string
• Is a sequence of zero and more characters
• It is enclosed in double quotation marks
• null or empty string refers to string containing no
characters; “” is the empty string
Example:
“William Jacob”
“Mickey”
“”
Variables, Assignment Statements and Input
Statements
• Two steps of storing data in the computer’s memory
1. Instruct the computer to allocate memory
2. Include statements in the program to put data into the
allocated memory
Allocating Memory with Constant and
Variables
• Instruct the computer to allocate memory
• name, data type to be store in memory locations
• Knowing the data type is crucial for performing accurate
calculations
• It is also critical to know whether your data needs to
remain fixed throughout program execution or whether is
should change
Named Contants
• Use to instruct a program to mark those memory locations
in which data is fixed throughout the program execution
• It refers to a memory location whose content is not
allowed to change during program execution
Syntax:
const dataType identifier = value;
Ex. const double CONVERSION = 2.54;
Note
• The default type of floating – point numbers is double.
Therefore, it you declare a named constant of type float,
then you must specify that the value is of type float as
follows:
const float CONVERSION = 2.54f;
float values is 4 bytes
double values is 8 bytes
Variables
• is a location in the computer’s memory where a value can
be stored for use by a program
Syntax:
dataType idenfier, identifier, …;
Example:
double amoutDue;
int x, y, z;
char ch;
string name;
Several Data Types
Type
Keyword
Boolean
bool
Character
char
Integer
int
Floating point
float
Double floating point
double
Valueless
void
Wide character
wchar_t
Several of the basic types can
be modified using one or more
of these type modifiers −
•
•
•
•
signed
unsigned
short
long
Data Types, Width and Range
Type
Typical Bit Width
Typical Range
char
1byte
-127 to 127 or 0 to 255
unsigned char
1byte
0 to 255
signed char
1byte
-127 to 127
int
4bytes
-2147483648 to 2147483647
unsigned int
4bytes
0 to 4294967295
signed int
4bytes
-2147483648 to 2147483647
short int
2bytes
-32768 to 32767
unsigned short int
Range
0 to 65,535
signed short int
Range
-32768 to 32767
Data Types, Width and Range
Type
Typical Bit Width
Typical Range
long int
4bytes
-2,147,483,648 to 2,147,483,647
signed long int
4bytes
same as long int
unsigned long int 4bytes
0 to 4,294,967,295
float
4bytes
+/- 3.4e +/- 38 (~7 digits)
double
8bytes
+/- 1.7e +/- 308 (~15 digits)
long double
8bytes
+/- 1.7e +/- 308 (~15 digits)
wchar_t
2 or 4 bytes
1 wide character
https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
Variables
• Each variable has dataType which determine the size and
layout of the variable memory
• All variables must be declared with a name and a data
type before they can be used in a program.
• int, double and char are called fundamental types.
• Fundamental-type names are keywords and therefore
must appear in all lowercase letters.
Naming a Variable
• A variable name is any valid identifier that is not a keyword
• is a series of characters consisting of letters, digits and
underscores ( _ ) that does not begin with a digit.
• If variable is combination of more than one word, the first
letter of each word, except the first word, is uppercase ex.
amountDue
• C++ is case sensitive —uppercase and lowercase letters are
different, so a1 and A1 are different identifiers.
Variable Types
Type
Description
bool
Stores either value true or false.
char
Typically a single octet (one byte). This is an integer type.
int
The most natural size of integer for the machine.
float
A single-precision floating point value.
double
A double-precision floating point value.
void
Represents the absence of type.
wchar_t
A wide character type.
Putting Data into Variables
1. Use C++ assignment operator
• In C++, = is an assignment operator
int num1, num2;
num1 = 4;
2. Use input (read statements)
• cin statement uses the input stream object cin and the stream
extraction operator, >>, to obtain a value from the keyboard.
Example
int feet;
int inches;
cout<< “Enter two integers: “;
cin>>feet >> inches;
cout<< endl;
Increment and Decrement Operators
• increment operator (++)
• decrement operator (--)
Syntax of increment operator (++)
Pre – increment : ++variable
Post – increment : variable++
Increment and Decrement Operators
• increment operator (++)
• decrement operator (--)
Syntax of decrement operator (++)
Pre – increment : --variable
Post – increment : variable--
Increment and Decrement Operators
C++ Logical Operators
• used if we want to compare more than one condition
• Depending upon the requirement, proper logical operator
is used
http://www.c4learn.com/cplusplus/cpp-logical-operator/
C++ Logical Operators
Operators
&&
Name of the Operator
Type
AND Operator
Binary
||
OR Operator
Binary
!
NOT Operator
Unary
http://www.c4learn.com/cplusplus/cpp-logical-operator/
C++ Logical Operators
Operator
Output
AND
Output is 1 only when conditions on both sides of
Operator become True
OR
Output is 0 only when conditions on both sides of
Operator become False
NOT
It gives inverted Output
C++ Logical
Operators Truth
Table
Operator
AND
OR
NOT
1st
Condition
2nd
Condition
Output
True
True
True
True
False
False
False
True
False
False
False
False
True
True
True
True
False
True
False
True
True
False
False
False
True
–
False
False
–
True
Activity # 7
• Create a program that will read strings and numeric data
Output
Enter your FirstName:
Name:
Enter your Lastname:
Age:
Enter your Age:
Weight:
Enter your Weight:
Program Styles and Form
• Syntax
• Rules of a language tell what is legal and what us not legal
• It was detected during compilation
What is the
Ex.
int x;
//Line 1
int y
//Line 2
double z;
//Line 3
y = w + x; //Line 4
compilation
error?
Use of Blanks
• Also used to separate reserved words and identifiers from
each other and from other symbols
• It should be never appear within a reserved word or
identifier
Semantics
• Refers to the set of rules that gives meaning to a
language
Example is the order of precedence rules for arithmetic
operators
Both
syntactically
correct
2+3*5
expressions but they have different
(2 + 3) * 5
meanings
Naming Identifiers
• Example1
Example 2
Self- documenting identifiers it can make comments less
Naming Identifiers
• annualsale – consider also as self – documenting
identifier, but it is called a run-together word
• It may lessen the clarity of your documentation
• You can capitalize the beginning of each new word or by
inserting underscore (_) just before the new word.
Ex. annualSale; annual_sale
Prompt Lines
• Are executable statements that inform the user what to do
Ex.
cout<<“Please enter a number between 1 and 10 “
<<“ and press the return key” << endl;
cin>> num;
Documentation
• the program should be not only you but also anyone else
• A well – documented program is easier to understand and
modify
• Comments is used to documents programs
• It should appear in a program to explain the purpose of
the program, identify who wrote it,
purpose of particular statements
and explain the
Form and Style
• Different ways to declare variable
Form and Styles
Download