Programming Element PDF
Programming Element PDF
Programming Element PDF
Computer Programming 2
The print Object
• This produces multiple line of output:
print (“I am ”)
print (“Iron Man”)
Computer Programming 5
The print Object
• Another way of producing multiple-line text using
\n
Computer Programming 6
The output Object
Computer Programming 8
Computer Programming 9
Computer Programming 10
Variables and Literals
Variables
• Variable: a storage location in memory
11
Variables and
Literals Variables
Computer Programming 12
Variables and Literals
• Literal: a value that is written into a program’s code.
Computer Programming 13
Variables and Literals
15 is an integer literal
This is a string literal
Output Display
Computer Programmng
14
Identifiers
• An identifier is a programmer-defined name for
some part of a program: variables, functions, etc.
• You cannot use any of the C++ key words as an
identifier. These words have reserved meaning.
15
Variable Names
• A variable name should represent the purpose of
the variable. For example:
itemsOrdered
16
Identifier Rules
• The first character of an identifier must be an
alphabetic character or and underscore ( _ ),
• After the first character you may use
alphabetic characters, numbers, or
underscore characters.
• Upper- and lowercase characters are distinct
17
Rules of naming variables
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),
words and contain any symbols. password& (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 18
Valid and Invalid Identifiers
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes
total_Sales Yes
Computer Programming 19
Data Types
Numeric Data Type Character Data Type
❑int ❑string (str)
❑float
❑long Bool Data Types
❑complex ❑True
❑False
Computer Programming 20
Numeric Data Type - int
• Plain integers of positive or negative whole
numbers
• E.g. : 10, -10
• Assign for suitable variable application, such
as number of student (num_student),
number of car (num_car) and etc.
Computer Programming 21
Numeric Data Type - long
• Long integer with infinite size
• Similar with int, except the are followed by
letter “L”
• E.g. : 10L, -10L
Computer Programming 22
Numeric Data Type - float
• Represent real numbers.
• E.g.
12.45 -3.8
Computer Programming 23
Numeric Data Type - complex
• Use to represent complex number
• Represent by formula a+bi, where a and b are floats, while
i is the
−1
• E.g. : 10+28i
Computer Programming 24
Character Data Type - str
state = “Singapore”
print (state)
Computer Programming 25
bool Data Type
• Represents values that are true or false
• bool variables are stored as small integers
• false is represented by 0, true by 1:
bool allDone = true;
bool finished = false;allDone finished
1 0
Computer Programming 26
Variable Assignments
• An assignment statement uses the = operator to
store a value in a variable.
item = 12;
• This statement assigns the value 12 to the item
variable.
• The variable receiving the value must appear on
the left side of the = operator.
• This will NOT work:
// ERROR!
12 = item;
Computer Programming 27
Variable Initialization
• To initialize a variable means to assign it a
value and automatically defined with
appropriate data type
Computer Programming 28
Multiple Variable Assignment in the Same Line
Computer Programming 29
Arithmetic Operators
Computer Programming 30
Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF
ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Computer Programming 31
A Closer Look at the / Operator
Computer Programming 32
Program Documentation / Comments
Computer Programming 33
Computer Programming 34
Computer Programming 35
Computer Programming 36
Program to calculate area of rectangular
37
And now??
Computer Programming 38
Computer Programming 39
Let’s go for extra miles!
Change to calculate the width required
for an area of rectangular
Hint: ask the area and length required
from user.
Area = length*width
Computer Programming 40