Week3 Chapter3
Week3 Chapter3
Computer Programming
Wing IDE Personal 6.1 Inteface
3
Run button
Computer Programming
The print Object
4
Computer Programming
The print Object
5
print (“I am ”)
print (“Iron Man”)
I am
Iron Man
Computer Programming
The print Object
6
I am
Iron Man
Computer Programming
Classes and Objects
7
The cout Object
This produces single line of output
I am Iron Man
Variables and Literals
8
Variables
Variable: a storage location in memory
Computer Programming
Variables and Literals
10
Literals
Literal: a value that is written into a program’s code.
Computer Programming
Variables and Literals
11
Literals
15 is an integer literal
Computer Programming
Identifiers
12
Computer Programming
Variable Names
13
itemsOrdered
Computer Programming
Identifier Rules
14
Computer Programming
Rules of naming variables
15
Explanation Example
Variable name should START
either with letter of underscore. score, _number
Cannot start with number.
The reminding character CAN
consist of letters, numbers and total_sales, marks1
underscore.
Should NOT made of reserved and (reserved word), password&
words and contain any symbols. (contain symbol)
May NOT contain spaces of total sales (not valid since has
naming variable names. space between total & sales)
Names are CASE SENSITIVE with totalsales is not same with
uppercase and lowercase. Totalsales
Computer Programming
Valid and Invalid Identifiers
16
totalSales Yes
total_Sales Yes
Computer Programming
Data Types
17
float
False
Computer Programming
Numeric Data Type - int
18
Computer Programming
Numeric Data Type - long
19
Computer Programming
Numeric Data Type - float
20
E.g.
12.45 -3.8
Computer Programming
Numeric Data Type - complex
21
Computer Programming
Character Data Type - str
22
state = “Singapore”
print (state)
Computer Programming
bool Data Type
23
Computer Programming
Variable Assignments
24
// ERROR!
12 = item;
Computer Programming
Variable Initialization
25
Computer Programming
Multiple Variable Assignment in the
26
Same Line
Computer Programming
Arithmetic Operators
27
Computer Programming
Binary Arithmetic Operators
28
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Computer Programming
A Closer Look at the / Operator
29
Computer Programming
A Closer Look at the % Operator
30
Computer Programming
Comments
31
Computer Programming
Comments
32
Single Line
Begin with // through to the end of line:
int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area
Computer Programming
Comments
33
Multi-Line Comments
Begin with /*, end with */
Can span multiple lines:
/* this is a multi-line
comment
*/
Can begin and end on the same line:
int area; /* calculated area */
Computer Programming
34
Thank You
Q&A
Computer Programming