0% found this document useful (0 votes)
32 views28 pages

Data Types and Expressions: CSIS1117 Computer Programming

This document discusses data types and expressions in C++. It introduces variables as a way to store and represent data in programs. The main built-in data types for numbers in C++ are integers (int) and real numbers (double). Variables must be declared before use and can be initialized during declaration. Values of variables can be changed through input statements or assignment statements. Arithmetic expressions allow computations using operators like addition and multiplication. Type compatibility and precedence rules determine the type and order of evaluations in expressions. Strings are also discussed as a common data type, with the C++ string library providing functions to manipulate string values.

Uploaded by

julianli0220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views28 pages

Data Types and Expressions: CSIS1117 Computer Programming

This document discusses data types and expressions in C++. It introduces variables as a way to store and represent data in programs. The main built-in data types for numbers in C++ are integers (int) and real numbers (double). Variables must be declared before use and can be initialized during declaration. Values of variables can be changed through input statements or assignment statements. Arithmetic expressions allow computations using operators like addition and multiplication. Type compatibility and precedence rules determine the type and order of evaluations in expressions. Strings are also discussed as a common data type, with the C++ string library providing functions to manipulate string values.

Uploaded by

julianli0220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

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

In programs, we define variables to store data.

You can store a particular value, and the value can be


used within the program later.
In reality, a variable corresponds to some location(s) in
the main memory.

The name of a variable, or an identifier, is a


series of characters, following some rules.
Characters include:

Letters: a to z , A to Z
Digits: 0 to 9
Underscore: _
Others: e.g. ! , @ , + , { ,

c1117 lecture 2

Valid identifiers

A valid identifier

It must start with a letter or an underscore, and


The remaining characters must be letters, digits, or
underscores.

C++ is case-sensitive, so radius, RADIUS,


Radius, etc, are different.
It cannot be a keyword of C++

What are the keywords in C++ ?

c1117 lecture 2

Keywords in C++

They are reserved as part of the C++ language


and cannot be used as identifiers
There are about 70 keywords in C++.

e.g. int,return,if,else,double,break,for,
All keywords are listed in the last slide of this lecture
node.

They have special meanings to the compiler, and


cannot be changed by the programmer.

c1117 lecture 2

Valid C++ identifiers?


200612345

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

Simple data types

Each variable is associated with a type, which


specifies the type of data to be stored in the
variable.
C++ support the following built-in data types for
numbers

int for integer


double for real number

c1117 lecture 2

Simple data types

int : type for integer numbers

Conceptually there are infinite number of integers

e.g. 12345, 0, 3295,

But the range is limited to [-231, 231-1] on most systems

double : type for real numbers

e.g. 3.1416, 0.4239, 2.7e4, -0.45e-7,


Not represented exactly in computer, so expression like
0.1 0.025 0.075 may give unexpected results.
See precision.cc as an example

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

Each variable must be declared prior to use


A variable s value can be initialized at the point of
declaration.
// Examples:
int age = 20;
double height = 160.3;

Watch out when using un-initialized variables!


They may contain garbage values and give
unexpected results.

c1117 lecture 2

10

Input statements

The value of a variable can be changed using an


input statement. E.g. cin >> input_no
Imagine that cin is associated with the keyboard,
data are extracted from cin using >>, the
extraction operator, and must be stored in a
variable.
User input with

keyboard (press enter


int input_no;
after entering 40 )
cin >> input_no;
Cout << "your input no. is:" << input_no << endl;

Program code
c1117 lecture 2

Output from program

40
your input no. is 40

Command Prompt
11

Assignment statements

Another way to change the value of a variable is


using an assignment statement.
Syntax
variable_name = expression;
Assignment operator

c1117 lecture 2

12

Arithmetic expressions

Arithmetic expressions are very common because


numeric computation is the core of many
programs.
An expression is formed by operator(s) and
operand(s).
Common operators: +,-,*,/,% (remainder),

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

What happens when a statement involving


expressions is put into execution?

The evaluation mechanism of an expression is


important!
The result of evaluating an expression is a value
The value itself belongs to a certain type.

In evaluating an expression with more than one


operators, we need to know which one should
carry out first.

E.g. in Mathematics, you know multiplications will be


evaluated before additions.

c1117 lecture 2

14

Precedence & Associativity

Precedence of operators

Some operators have higher priority than the others. In


an expression with mixed operators, the one with a higher
priority will be carried out before the lower ones.
E.g. *,/ have higher priority than +,-

Associativity of operators

Associativity rule is used if more than one operators have


the same priority, in C++, it evaluates from left-to-right,
except the assignment

c1117 lecture 2

15

Precedence & Associativity

Use parentheses to force the evaluation order.

E.g.
written as

((u

v)/(x * y))*((-b)/(p/q))
in C++ expression

See assign.cc and swap.cc for more examples


c1117 lecture 2

16

Type compatibility

C++ is a strongly-typed language. Mixed type


operation is usually not allowed unless the types are
compatible.

E.g. error message will be given by compiler if you try to


assign a string literal to an int value, int x="happy";
int and double are compatible, it is acceptable to
assign int to double, e.g. double x=10;
Assigning double to int is also allowed, but compiler
will issue a warning message about the mixed-type
assignment, e.g. int x = 1.3;

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

Division between int results in an int

If either one of the operands are double, the result


will also be in double value.

e.g. 21/6.0 = 3.5

The operands of the modulus operator % must be


int type

e.g. 21/6 = 3, 21/7 = 3

e.g. 3.14%3 illegal, 21%6, 21%7 legal

See compatibility.cc as an example

c1117 lecture 2

18

Low level view

How the variables are handled in the main memory?


int height, width;
int area = 0;
cin >> height;
cin >> width;
area =height*width;
cout<< the area is:
<< area << endl;

Program code

Memory
address
1004

height

1008

width

Main memory

Memory spaces are allocated in declaration


statements
c1117 lecture 2

19

Low level view

How the variables are handled in the main memory?


int height, width;
int area = 0;
cin >> height;
cin >> width;
area =height*width;
cout<< the area is:
<< area << endl;

Program code

Memory
address
1004

height

1008

width

1112

area

Main memory

The value of the variable area is initialized


c1117 lecture 2

20

Low level view

How the variables are handled in the main memory?


int height, width;
int area = 0;
cin >> height;
cin >> width;
area =height*width;
cout<< the area is:
<< area << endl;

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

Low level view

How the variables are handled in the main memory?


int height, width;
int area = 0;
cin >> height;
cin >> width;
area =height*width;
cout<< the area is:
<< area << endl;

Program code

Memory
address
1004
800{
1008

1112

20
40

height

0 800

area

width

Main memory

The expression is evaluated, and the


result is stored in area
c1117 lecture 2

22

Low level view

How the variables are handled in the main memory?


int height, width;
int area = 0;
cin >> height;
cin >> width;
area =height*width;
cout<< the area is:
<< area << endl;

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

How to display the double-quote ?


cout << " " "

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?

We use backslash character \ to help

e.g. use cout << " \" "; to display the doublequote

c1117 lecture 2

24

Escape sequence

The backslash character \ tells the compiler that the


character that follows does not have the same meaning
as the character appearing by itself according to the C++
language.
Such a sequence is called an escape sequence.
The sequence is typed in as two characters with no
space between the symbols.
Some escape sequences defined in C++:
\t (a horizontal tab)
\n (newline character)
\" (the double quote)
\\ (backslash)
Try seffect.cc for an example

c1117 lecture 2

25

C++ string library

Strings are frequently used in text manipulation

Nearly all programs involve string operations.


e.g. checking the name of a file, reading the
folder/directory information, searching a piece of text in
a document.

C++ provide a library for string.

To use it, include the header #include <string>


We can declare string type variables, and the I/O
operations are just the same as numbers.

c1117 lecture 2

26

Simple string operations


string name="Jack"; //declaration + initialization
cout << name << endl; //output
string addr;
cin >> addr;
//input

Note that string is neither a C++ keyword nor a


built-in type!
Two strings can be concatenated using operator: +

But it cannot be used for two string literals.

String variables can be used in assignment


expressions.
See string.cc as an example.
c1117 lecture 2

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

You might also like