Data Types and Expressions: CSIS1117 Computer Programming
Data Types and Expressions: CSIS1117 Computer Programming
Contents
Introduction to variables
Simple data types
Declaration statements
Assignment statements
Arithmetic expressions
Type compatibility
Usage of String
c1117 lecture 2
Variables
Letters: a to z , A to Z
Digits: 0 to 9
Underscore: _
Others: e.g. ! , @ , + , { ,
c1117 lecture 2
Valid identifiers
A valid identifier
c1117 lecture 2
Keywords in C++
e.g. int,return,if,else,double,break,for,
All keywords are listed in the last slide of this lecture
node.
c1117 lecture 2
a_man
const
an integer
year1-student
change%2
string
Days_of_Week
cout
delete
program.cc
__oOOo__
ABC123x7
Try to avoid using, those pretty standard names with wellknown meaning, as variables, although they are not
keywords. E.g. cout, cin, string, sqrt, std
c1117 lecture 2
c1117 lecture 2
c1117 lecture 2
Variable declarations
Syntax
Data_type variable_name1, variable_name2, ;
int, double
Variables are
separated by
commas.
A declaration
must be ended
by a semicolon.
//Examples:
int age;
double height,
weight;
c1117 lecture 2
Variable declarations
c1117 lecture 2
10
Input statements
Program code
c1117 lecture 2
40
your input no. is 40
Command Prompt
11
Assignment statements
c1117 lecture 2
12
Arithmetic expressions
quotient
The operands can be numbers, variables,
as well as another expression.
2
See arithmetic.cc for an example 2 5
4
remainder
1
c1117 lecture 2
13
Arithmetic expressions
c1117 lecture 2
14
Precedence of operators
Associativity of operators
c1117 lecture 2
15
E.g.
written as
((u
v)/(x * y))*((-b)/(p/q))
in C++ expression
16
Type compatibility
Only the integral part of the double value is assigned, ie. the
stored value of x is 1. This may result in a loss of precision
c1117 lecture 2
17
Type compatibility
c1117 lecture 2
18
Program code
Memory
address
1004
height
1008
width
Main memory
19
Program code
Memory
address
1004
height
1008
width
1112
area
Main memory
20
Program code
c1117 lecture 2
20
40
Memory
address
1004
20
height
1008
40
width
1112
area
Main memory
Command prompt
for user input
21
Program code
Memory
address
1004
800{
1008
1112
20
40
height
0 800
area
width
Main memory
22
Program code
c1117 lecture 2
Memory
address
1004
height
1008
20
40
1112
800
area
20
40
the area is: 800
width
Main memory
Command prompt
with program output
23
Output statement
It does not work, the compiler will treat the 2nd doublequote is the end of a string literal with no content. Then
when it reads the 3rd double-quote, it will be stuck.
How to tell the compiler that the 2nd quote is not the
terminator of a string literal, but indeed is a character?
e.g. use cout << " \" "; to display the doublequote
c1117 lecture 2
24
Escape sequence
c1117 lecture 2
25
c1117 lecture 2
26
27
Keywords in C++
asm
auto
bool
break
case
catch
char
class
const
const_cast
continue
default
delete
do
double
c1117 lecture 2
dynamic_cast
else
enum
explicit
extern
false
float
for
friend
goto
if
inline
int
log
long
mutable
namespace
new
operator
private
protected
public
register
reinterpret_cast
return
short
signed
sizeof
static
static_cast
struct
switch
template
this
throw
true
try
typedef
typeid
typename
union
unsigned
using
virtual
void
volatile
wchar_t
while
28