Topic2-Variable and Basic Data Type
Topic2-Variable and Basic Data Type
programming i
Topic 2: Variable, Basic Data Type,
Operators and Statements
1
Variable/Identifier
2
Variable/Identifier
What and Why?
▪ A variable (also called identifier) is a holder of value which can be used and
changed throughout the program
▪ It is used to store value and its value can be changed during the program run
4
Variable/Identifier
Basic types of variables
▪ Number
▪ Integer : A non-fractional number, e.g: 5, 8, 9, 199
▪ Float : A non-fractional and fractional number (real number), e.g: 4, 4.0 or 5.8
▪ Character : A single letter. It is placed inside a single quote, e.g: ‘M’
▪ Sequence of character : A string ( a set of letters). It is placed inside a double
quote, e.g: “Sok”
▪ NOTE: Different types of variables consume different memory space
▪Rules of giving
Rule for namingaavariable
variable name
✓ Start with a letter (small or capital is okay) or underscore
✓ Not start with number or any symbol (+, -, @, #, !, &, *, /, ., …. etc)
✓ Don’t use name that already exist with reserved keywords
✓ No space is allowed.
▪ If the name is long, use underscore ( _ , person_weight) or camel case (myVar, personWeight)
▪ Naming convention (good practice)
▪ Name of a variable should reflect the value it will store
▪ The name should be readable and not too short
▪ Examples: Var age: Integer Var 7age: Integer
Var price: Float Var @price: Float
Var name: Sequence of character Var +name: Sequence of character
Var _tel: Integer Var *tel: Integer
Var studentName: Sequence of character Var &student Name: Sequence of character
Var work_position: Sequence of character Var work Position: Sequence of character
6
Variable or Identifier
NOTE: Naming a variable
▪ Name of a variable is case sensitive (Ex: age, Age, AGE are different names)
7
Variable or Identifier
Assign value to a variable
▪ Assign value to a variable means that you give a value to that variable
▪ Syntax for assigning a value to a variable:
▪ <variable> <value or other variable (same type) or expression>
▪ The type of value assigning to the variable must be the same as variable’s type
▪ A variable can be used once a value was assigned to that variable
▪ Examples:
▪ Var name, name2 : Sequence of integer
Var name, name2 : Sequence of character Var name, name2 : Sequence of character
Var val▪ :Var val : Integer
Integer Var val : Integer
name ▪ name
“Sok” “Sok” name “Sok”
name2 ▪ name2
“Sao” “Sao” name “name2”
name ▪ name
name2 “name2” name name2
val ▪ val
5 5 val name
val ▪ val
val*2 val*2 val “Dara”
8
Algorithm
Constant
read(<list of variables>)
10
Algorithm
Displaying the information or data
Output: …? 12
More Examples of Writing Algorithms
Ex1: Calculate a given price with tax
▪ Suppose that the tax is 3% and a user is required to input a price. As a result
we calculate and display the final price including the tax.
....
End
13
More Examples of Writing Algorithms
Ex1: Calculate a given price with tax
▪ Suppose that the tax is 3% and a user is required to input a price. As a result
we calculate and display the final price including the tax.
const(TAX: int) 3
const(TITLE: string) “Result”
Var price, priceWithTax: float
Begin
write(“Give me the price exclude tax:”)
read(price)
priceWithTax price + (price*TAX)/100
write(TITLE)
write(price, “dollars exclude tax.”, priceWithTax, “dollars include tax”)
End
Output: …? 14
Examples of Algorithms
Ex2: Student information
▪ Write an algorithm for computing an exchange rate USD-Riel. Suppose that 1USD= 4100 R. A
user is required to input an amount of money in USD then a program will convert it into riel
currency and display the following information
16
Practise
Write an algorithm for each of the question below:
1. Read a last name and first name from a user. Then display a phrase as follows:
What is your last name?
What is your first name?
Welcome <lastname> <firstname>!
2. Read a number from a user and calculate square of that number then display
its result.
Enter a number:
The square of <number> is ….
3. Read two numbers (say n1 and n2) from a user then display their summation,
subtract, and multiplication. Enter the first number:
Enter the second number:
The summation of <n1> and <n2> is: …
The subtraction of <n1> and <n2> is: …
The multiplication of <n1> and <n2> is: …
17
The division of <n1> and <n2> is: …
Practise
Write an algorithm for each of the question below:
4. A program to ask user for firstname, lastname and department. Then display
this message:
Welcome to department, lastname firstname!
5. Ask a user to input height and base of a triangle. Calculate the surface of this
triangle and display.
6. Ask a user for a, b and c length of a triangle. Calculate its surface using heron
formula.
18
Types of Variables
19
Overview
Outline
▪ Integer ▪ String
▪ Boolean ▪ Operators
+, -, *, /, DIV, MOD
▪ Character
▪ Relational operators
▪ ASCI code
20
Basic data type: Integer
Integer
21
Basic data type: Integer
Operators for Integer
▪ Examples:
▪ x 10+2 => x is 12
▪ y x-7 => y is 12-7=5
▪ z y/2 => z is 5/2=2.5
▪ z y MOD 2 => z is 5 MOD 2 = 1
▪ z y DIV 2 => z is 5 DIV 2 = 2
22
Practice 1
23
Basic data type: Real number
Real number
▪ Example
27
Character
Definition
28
Function
What?
▪ A function is used to achieve/do something
▪ A function may take no input/parameter/argument
▪ A function may take one or more parameters
29
Character
ASCII CODE
http://www.asciitable.com/
31
32
String
Definition
index 0 1 2 3 4
name M a r r y
33
String
Functions
index 0 1 2 3 4
name M a r r y 34
String
Functions
35
Summary
Revision
▪ Integer : non-fractional number Integer
▪ Real : real number Float, Double
▪ Character : a single character written in a single quote ‘ ’
▪ Boolean : true or false value
▪ String : a sequence of character written in a double quote “ ”
▪ Operators :+ - * / DIV MOD
▪ Relational operators : == > < >= <= !=
▪ Functions for Character : next, prev, ord, chr
▪ Functions for String : concate, length, substring, stringcmp,
stringcopy, string2lowercase, string2uppercase
36
Q&A
37
Practice
Exercises
1. Write an algorithm to determine DIV and MOD of two input numbers from a user
2. Write an algorithm to calculate the summation of two input of real numbers from
a user and identify the integer part and fractional part
3. Suppose we have: a true, b false. What is the value for each of:
▪ NOT(a), NOT(b), a OR b , NOT(a) OR NOT (b), NOT(a) AND b, a AND b
4. Write an algorithm that allows a user to input two string. Copy the second string to the first
one then covert first string to uppercase.
38
Practice
▪ Suppose we have: a true, b false. What is the value for each of:
▪ NOT(a), NOT(b), a OR b , NOT(a) OR NOT (b), NOT(a) AND b, a AND b
39
Practice
▪ Write an algorithm that allows a user to input two string. Copy the second
string to the first one then covert first string to uppercase.
40
Practice
1. Write an algorithm to ask for firstname and lastname from a user. Then make another variable username. This
variable is the result of contatenation between firstname, underscore ( _ ), and lastname.
E.g: If input for firstname and last name are Jack and Rose, then username should be Jack_Rose
2. Write an algorithm to convert an input string, entered by a user, to uppercase and lowercase. E.g: If a user inputs the
word Apple, then the output of the program should be APPLE, apple
3. Write an algorithm to find the length of an input string. E.g: If a user inputs the word Book, then the output of the
program should be The length of the word “Book” is 4.
41
Practice
4. Write an algorithm to display the first character and the last character of an input string entered by a user. E.g: If a
user inputs the word Banana, then the output of the program should be In word “Banana”, the first character is ‘B’
and the last character is ‘a’
5. Suppose we have: a=50, b=-50, then what is the Boolean value of each expression below
▪ A) a>50, B) b>=50, C) a==50 AND b==50, D) a==50 OR b==50,
6. By taking a look at the ASCII code tables, answer the question below:
▪ What are the ASCII codes for the letters a, b, c, …., z ?
43
Examples in C programming using string functions
#include <string.h>
String functions
44
Examples in C programming using string functions
#include <string.h>
String functions
45
Assignment Deadline: 1 week
1. Write a C program to concatenate a string. The user is required to input the name of a student and the name of a
course enrolled by that student. For example, the message sample is defined as follows:
2. Write a C program to convert an input string, entered by a user, to uppercase and lowercase. E.g: If a user inputs the
word Apple, then the output of the program should be APPLE, apple
3. Write a C program to find the length of an input string. E.g: If a user inputs the word Book, then the output of the
program should be The length of the word “Book” is 4.
4. Write a C program to display the first character and the last character of an input string entered by a user. E.g: If a
user inputs the word Banana, then the output of the program should be In word “Banana”, the first character is ‘B’
and the last character is ‘a’
46