Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Syntax Rules
Dr. Siti Barirah Ahmad Anas
[barirah@upm.edu.my][03-89466439][Room A.04.89]
OUTLINE
Topics
- C++ Program Structure
- Formatted Input/Output
- Expression: Unary & Assignment
- Typedef and sizeof operator
- Arithmetic conversion
- Statement
Learning Outcome
Students are expected to be able to:
- Distinguish between syntax, semantics and run-time error
- Use identifiers to name elements in program
- Use variables to store data, and declare them using numeric data types
- Differentiate between const keyword and #define directives
- Write C++ programs to output message on console, read input from
keyboard and perform simple calculation
- Use operators and punctuators in expression
- Understand precedence and associativity of operators
- Understand implicit and explicit conversion of numeric data type
SYNTAX RULES
Example
int main () int main ()
{ {
c = a + b;
Statements
return 0;
} } // main function
• A C++ program is first constructed as a sequence of characters
such as:
– uppercase letters => A to Z
– lowercase letters => a to z
– digits => 0 to 9
– special characters => + - * / = ( ) { } [ ]
< > ‘ “ ! # % & _ | ^ ~ \ . , ; : ?
– white space character => blank, newline, tab
• What is…
– Named memory locations
– Have a type and size
– Their values can be changed
• Declarations
– Used to name an object
– E.g. int radius;
char ch;
double area;
– Variables of the same type can be separated by commas
e.g. int i, j, k;
VARIABLE INITIALIZATION
• Initializer establishes the first value that the variable will contain
• Example:
int count = 0;
int count, sum = 0;
int count = 0, sum = 0;
int count = 0,
int sum = 0;
• Derived types
– Pointer, enumerated type, union, array etc.
• E.g. 3_type.cpp
Variables in Memory
3.1415926536 pi
double pi;
program memory
STATEMENTS
• 2 types:
– Expression statement
• Expression ended with semicolon
– Compound statement
• Unit of code between the opening and closing braces
CONSTANT
• Common error:
#define PI 3.142;
...
area = PI*r*r;
• Actual evaluation is
area = 3.142;*r*r; WHICH IS WRONG…!!!
#include <iostream>
using namespace std;
int main ()
{
const double PI = 3.14159; //try replacing this with #define
return 0;
}
CHARACTER SETS
• ASCII
– American Standard Code for Information Interchange
– used by most computers
• EBCIDIC
– Extended Binary Coded Decimal Interchange Code
– used only by IBM Mainframes and their clones
Table 1: ASCII Code
E.g. 3_character.cpp
ESCAPE CHARACTERS
cin >>
Keyboard
Buffer
cout <<
Monitor
Memory
FORMATTED INPUT
• Examples:
3_computeAverage.cpp
FORMATTED OUTPUT
• Examples
Operator Description
setprecision(n) Set precision of floating-point numbers
fixed Displays floating point in fixed-point notation
showpoint Cause floating-point to be displayed with decimal
point
setw(width) Specifies width of print field
left Justifies output to the left
right Justifies output to the right
• E.g. 3_streamManip.cpp
• setprecision(n)
– Specify total number of digits displayed in floating-point numbers
– Example
double num = 12.34567;
cout << setprecision(3);
– Output
12.3
• fixed
– Force a floating point numbers to be displayed in fixed number of digits
– If used with setprecision(n), then the number of digits after the decimal
point can be specified
• showpoint
– Can be used with setprecision(n) to specify the number of digits
after the decimal point
– Numbers are padded with trailing zeroes if there’s no fractional part
• setw(width)
– Specify number of columns of the output
• Equality operators:
== is equal
!= not equal
• Relational operators:
> greater than
< less than
>= greater than or equal
<= less than or equal
• 1+2*3
» the value is 7
» * has higher precedence than +, causing the
multiplication to be performed first, then addition.
• (1 + 2) * 3
» the value is 9
» expressions inside parentheses are evaluated first.
• 1+2–3+4–5
» The value is -1
» binary operators – and + have the same precedence, then
use associativity rule “left to right”
• Refer table 2.
EXPRESSION
• Sequence of operands and operators that reduces to a single value e.g. 2 + 5
– Primary expressions
• Identifier, constant and parenthetical expression
• E.g. a, 7 and (2 + a - 3)
– Binary expressions
• Formed by operand-operator-operand combination
• E.g. additive : a + 7 and multiplicative: 6 * 2
• Example 2:
int i = 1;
int j = ++i; // i is 2, j is 2
• Example 3:
int i = 1;
int j = i++; // i is 2, j is 1
• Example 4:
int i = 10;
int newNum = 10 * i++; is the same as
int newNum = 10 * i;
i = i + 1;
• Example 5:
int i = 10;
int newNum = 10 * ++i; is the same as
i = i + 1;
int newNum = 10 * i;
• example:
int a, b, c = 0;
a = ++c;
b = c++;
cout << a << b << ++c << c++ << endl;
3_unary.cpp
int a = 3, b = 4, c = 5, x, y;
x = a * 4 + b / 2 - c * b;
y = --a * (3 + b) / 2 - c++ * b;
• Assignment operators:
= += -= /= %= >>= <<= &=
?= |=
– same precedence
– right to left associativity
– changed value of variable
– Left operand must be a single value
• Simple Assignment
– As in algebraic expression
– E.g. a = 5, b = x + 1, i = i + 1
• Compound assignment
– Shorthand notation for a simple expression
– E.g. x *= y equals to x = x * y
• Example
– x *= y + 3 evaluated as x = x * (y + 3)
EXAMPLE
• E.g.1
int a, b = 2, c = 3;
a = b + c;
• E.g. 2
a=b=c=0; equivalent to a=(b=(c=0));
k += 2; k=k+2;
j *= k + 3; j=j * (k+3);
typedef OPERATOR
• uses:
– have type names that reflect the intended use
– Example 3_typedef.cpp
sizeof OPERATOR
• Syntax: sizeof(object)
• 2 conversion types
– Implicit type conversion
• Type is automatically converted by C++ program
• Based on the promotion hierarchy
Long double
double
float
Long int
Unsigned int
int
The Promotion Hierarchy
short
• A.k.a. casting
• Example
– (float) a
– (float) (x + y)
– (float) (a / 10)
– (float) a / 10
• E.g. 3_explicit.cpp