0% found this document useful (0 votes)
14 views

Lecture 1c Update

The document discusses programming fundamentals including input, processing, output, variables, data types, operators, and identifiers. It provides examples and explanations of these core programming concepts.

Uploaded by

Kaptain Suraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture 1c Update

The document discusses programming fundamentals including input, processing, output, variables, data types, operators, and identifiers. It provides examples and explanations of these core programming concepts.

Uploaded by

Kaptain Suraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

PRINCIPLES OF

COMPUTER
PROGRAMMING
Lecture 1c
Lecture Outline
■ Programming Fundamentals
– Input, Processing and Output
– Variables
– Data types
■ Basic Operators
– Arithmetic
– Comparison
– Assignment
– Logical
– Bitwise
Programming Fundamentals: Input
■ Data entered into a program, either by the programmer or
digitally, are referred to as inputs .

■ These inputs are stored in variables and used to run the


program

■ In order to keep the user informed about what is happening


inside the program, a programmer may choose to include
outputs.

■ This is where the data from the program is shown to the user,
either on screen or in the form of a physical output such as
printouts or signals.
Programming Fundamentals: Input
■ The input operation in a program transmits data from an
outside source to the program.
■ Often this data is typed at the computer keyboard by the
person using the program. The program waits until the
user types something.
■ This action is referred to as entering data from the
keyboard.
– We will use a statement that begins with the word
Input to allow the user to enter data from the keyboard
Obtaining user input from a keyboard
■ A program will require a computer system to perform four
main actions in order to communicate with a user:

1. the user must input data into the system


2. this data must be processed
3. the outcome is output to the user
4. the data is stored for later use
Example
■ Identify the input statements in the following:
Statement 1: Write "Enter the circle radius: "
Input Radius
Set Circumference = 2 * 𝜋 * Radius
Write Circumference

Statement 2: Write "Enter three numbers.”


Input Number1, Number2, Number3
Obtaining user input from a keyboard
■ Entering data into a program is referred to as data input.
■ Input can come from many automated sources, but the
most common is keyboard input from the user.

■ Programs interact with users by requesting input using


the keyboard.

■ Most data in a program is stored in a variable which can


be used to hold data inputted by the user.
Obtaining user input from a keyboard
■ In the example below, the program asks the user to enter
their name, which is stored in the variable called ‘name’:
■ OUTPUT “Please enter your name:“
■ name ← INPUT
■ Inputted data can be joined with an output to create a single
string. This process of joining strings together is called
concatenation, for example:

OUTPUT “Please enter your name:”


name ← INPUT
OUTPUT “Hello” + name
Variables
■ A variable is any characteristics, number, or quantity that
can be measured or counted.
■ A variable may also be called a data item. Age, sex,
business income and expenses, country of birth, capital
expenditure, class grades, eye colour and vehicle type
are examples of variables.
Variables
■ Variable is basically nothing but the name of a memory
location that we use for storing data.

■ We can change the value of a variable in C or any other


language, and we can also reuse it multiple times.
Variables
■ When we write programs most times we don’t know the
actual numbers that the user will enter while running
(executing) the program.
– A computer program should always be written so that
the user doesn’t have to re-enter all the steps to get
another result.
■ A variable is quantity that can change value during
execution of a program.
■ Whenever we need to refer to that data in a subsequent
program statement, we simply refer to its variable name
Variables
■ Technically, a variable is the name for a storage location
in the computer’s internal memory, and the value of a
variable is the contents of that location.
Why are variables used in programming?
■ Variables are needed to run all but the most simple
computer programs. As a program runs, it needs to hold
information in its memory.

■ This may be a number, the answer to a question or


something else. Variables allow us to store, change and
access this information as the program runs.
Processing
■ With a computer processor, processing is the actions a
processor performs when it receives information and is
part of the IPOS (input, processing, output, and storage)
steps.

■ 2. Processing describes software manipulating or


extracting data from a stored file.
Programming Fundamentals: Processing
■ The processing operation in a program executes the
statements that cause the program to do something.
■ Consider the statement:
Set Sum = varNumber1 + varNumber2
Programming Fundamentals: Output
■ Outputting data and information from a program to a
display
■ A program may need to communicate with a user. This
could be to show the outcome of the program, or to
request more information to allow the program to run.

■ This is often shown as text on the user’s screen and is


known as output. For example:

■ OUTPUT “Welcome to GCTU Computer/Telecom


Engineering Faculty“
Programming Fundamentals: Output
■ Input is the user giving something to the program, while
output is the program giving something to the user.
Programming: Output
■ Any information processed by and sent out from a
computer or other electronic device is considered output.

■ An example of output is anything viewed on your


computer screen, such as the words you type from your
keyboard
Programming: Output
■ A program’s output is result sent by the program to the
screen, printer, or another destination such as a file.
■ We will use the Write statement to display messages on
the screen.
■ A statement like the following to display the value of a
variable:
Write Circumference
Write FirstName
Programming Terminologies
■ As it is the case for any skill, it is essential to learn the
basic terms used in that domain before going full-fledged
into practice mode.

■ Knowing terms help you understand your domain better


and faster. If you are learning to code or new to computer
science, we bring a few important computer science and
programming terms to you that can act as your learning
Programming Terminologies
■ Reserved words are also called keywords.
– Reserved words cannot be redefined by the user within
any program, i.e. they cannot be used for anything other
than their intended use.
■ A named constant is an identifier that represents a
permanent value that never changes.

■ The ways that specific words and symbols are used by each
language is called its syntax.
– The actual code depends on what language you are using.
Every language has its specific syntax
The difference between syntax and
semantics in computer programming
■ Syntax refers to the structure of a program written
in a programming language.

■ Semantics describes the relationship between the


sense of the program and the computational
model.

■ Syntactic errors are handled at the compile time.


Identifier Names
■ Identifiers are names given to different entities
such as constants, variables, structures, functions,
etc.

■ Example: int amount; double totalbalance; In the


above example, amount and totalbalance are
identifiers and int, and double are keywords.
Identifier Names
■ Identifiers are names of things, such as variables, constants,
and methods, that appear in programs.
■ All identifier names must obey the programming language
rules for identifiers.
■ These rules are different for every language but the following
is generally accepted:
– An identifier name consists of letters, digits, the
underscore character (_), and the dollar sign ($) and must
begin with a letter, underscore, or the dollar sign.
– Identifier names can be any length but should be
meaningful.
Examples of Identifiers
■ Identifiers can be made of only letters, digits, the
underscore, and the dollar sign, no other symbols are
permitted to form an identifier.
■ Legal/acceptable identifier names include:
– first, NUMBER, Conversion, NaMe, …
– payRate, ComputeAverage, FIRSTname, Lastname, …
– counter1, number1, num2, f2name, …
– $Amount, _amount, _Amount, _2place, first_name, …
Try Exercise
Indicate whether the following are valid/invalid names
Operations on Data
■ All programming languages support some form of
arithmetic operation.
■ The basic arithmetic operators include addition,
subtraction, multiplication, division exponentiation and
modulus.
Example
■ To convert a temperature in degrees Fahrenheit to
degrees Celsius, we use the following formula:
5 𝐹 − 32
𝐶=
9
■ However, in a programming language, the formula is
written as follows:
C = 5 * (F – 32) / 9
Hierarchy of Operations
■ The rules of arithmetic tell us that the order in which
arithmetic operations is performed is as follows:
– Perform the operations in parentheses
– Perform exponentiations
– Do multiplications, divisions, and modulus
– Do additions and subtractions
■ The best way to write a mathematical expression is to put
parentheses around parts of the expression that you want
evaluated together
PEMDAS Rules
■ In Mathematics, we do operations like addition,
subtraction, multiplication and division.
■ These operations are performed by a certain rule or say
there is an order of operation.

■ PEMDAS rule is one of the rules which is exactly equal to


BODMAS rule.

■ The PEMDAS rule can be remembered using the acronym “Please


Excuse My Dear Aunt Sally”.
PEMDAS Rules
The full form of PEMDAS is given below:

■ P – Parentheses [{()}]
■ E – Exponents (Powers and Roots)
■ MD- Multiplication and Division (left to right) (× and ÷)
■ AS – Addition and Subtraction (left to right) (+ and -)

whereas the full form of BODMAS is – Brackets Order


Division Multiplication Addition and Subtraction.
Example
■ Given the arithmetic expression:
Example 1: Solve 58÷ (4 x 5) + 32
Solution:
■ 58 ÷ (4 x 5) + 32
■ As per the PEMDAS rule, first, we have to perform the operation which is in the
parentheses.
■ = 58 ÷ 20 + 32
■ Now perform the exponent/power operation
■ = 58 ÷ 20 + 9
■ The division should be performed.
■ = 2.9 + 9
■ And the last, addition.
■ = 11.9
Therefore, 58 ÷ (4 x 5) + 32 = 11.9
Example 2:

Solution:
As per the PEMDAS rule, first we need to perform the operation of exponent,
i.e. square root
For this first we need to add the numbers under the square root.
Example 4:
Calculate: [25 + {14 – (3 x 6)}]

■ Solution:
■ Given,
■ [25 + {14 – (3 x 6)}] As per PEMDAS, here we have perform the
operations within the parentheses, first (), second {} and finally []
■ =[25 + {14 – 18}]
■ = [25 +{-4}]
■ Here, we have to perform multiplication for the signs
■ = 25 – 4
■ = 21
Example
■ Given the arithmetic expression: 6 + 8 / 2 * 2ˆ2

1. 6 + 8/(2 * 2ˆ2)
= 6 + 8/8 = 6 + 1 = 7

2. (6 + 8)/2 * 2ˆ2
= 14/2 * 4 = 7 * 4 = 28

3. (6 + 8)/(2 * 2ˆ2)
= 14/8 = 1.75
PEMDAS: Order of Operations
■ P: Solve the calculation or equation which are present in the
parentheses or brackets like small brackets( ), curly brackets{
} or big brackets[ ]. Priority is given to brackets first.
■ E: Exponential expressions should be calculated first before
the operations of multiplication, division, addition and
subtraction. Usually, they are expressed in power or roots, like
22 or √4.
■ MD: Then perform multiplication or division from left to right,
whichever comes first in the equation.
■ AS: At last, perform addition or subtraction whichever comes
first while moving from left to right.
PEMDAS Vs BODMAS
■ There is only an abbreviation difference between them.

In Canada, this order of operation is also mentioned as BEDMAS(Brackets, exponents, division,


multiplication, addition and subtraction).
Self Assessment Questions
1. Simplify: (19 - 7) ÷ 4 + 11
2. Evaluate: 5 x (7 -2) + (8+1)
3. Simplify the expression: 2(9 - 2 + 3) ÷ 4
4. Compute: (31 - 11) ÷ 5 + 2
5. In the expression 7 x 8 + (24/ 8 x 4), which part has to
be solved first?
Q1 : Answer is C : Correct Answer is 14
Q2 : Answer is B : Correct Answer is 34
Q3 : Answer is C : Correct Answer is 5
Q4 : Answer is B : Correct Answer is 6
Q5 : Answer is B : Correct Answer is 24/8
Comparison/Relational Operation
■ A relational operator is a programming language
construct or operator that tests or defines some kind of
relation between two entities.

■ These include numerical equality (e.g., 5 = 5) and


inequalities (e.g., 4 ≥ 3)

■ This type of expression is also known as a Boolean


expression because they create a Boolean answer or
value when evaluated.
Comparison/Relational Operation
■ A condition is an expression that can be true or false.
■ Relational operators in programming evaluate to true or
false.
Assignment Operator
■ An assignment statement designates a value for a
variable. The equal sign (=) is used as the assignment
operator.
■ The syntax for assignment statements is as follows:
variable = expression
■ Example: Sum = 2
totalAmount = costPrice + TaxAmount
– It assigns the resulting value of the expression on the
right of the equals sign to the variable
Logical Operations
■ To test multiple conditions in decision making, we
performed these tests in separate statements
■ Sometimes control statements require more complex
conditions to determine a program’s flow of control
■ logical operators enable you to form more complex
conditions by combining simple conditions.
■ The logical operators are AND, OR, & NOT.
Logical Operations
■ If one of the operands or expressions is true, it will return 1.
■ If all of them are false, it will return 0.
Logical Operations: AND
■ Used when we wish to ensure that two conditions are
both true before we choose a certain path of execution.
■ Example: gender == FEMALE AND age >= 65
■ The truth table shows all four possible combinations of
false and true values
Value1 Value2 Value1 AND Value2
False False False
False True False
True False False
True True True
Logical Operations: OR
■ Used when we wish to ensure that either or both of two
conditions are true before we choose a certain path of
execution.
■ Example: gender == FEMALE OR age >= 65
■ The truth table shows all four possible combinations of
false and true values
Value1 Value2 Value1 OR Value2
False False False
False True True
True False True
True True True
Logical Operations: NOT
■ The ! (logical NOT, also called logical negation or logical
complement) operator “reverses” the meaning of a
condition.
■ Example: value = true
NOT(value)

Value Not( Value)


True False
False True
Increment or Decrement Operators
■ The increment operator (++) and decrement operator (––
) are for incrementing and decrementing a variable by 1.
■ For example:
■ The following code increments i by 1 and decrements j by
1 if i = 3, j = 3
𝑖++
𝑗−−
Increment or Decrement Operators
■ In addition to the arithmetic assignment operators, C++
also provides two unary operators for adding 1 to or
subtracting 1 from the value of a numeric variable.
■ These are the unary increment operator, ++, and the
unary decrement operator, --, which are summarized in
Fig. below.
Increment or Decrement Operators
.
Increment or Decrement Operators
■ The effects are different when they are used in the
beginning or at the end of statements.
Data Types
■ The computer allocates a certain amount of space in its
memory for data.
– But some data requires more space than other data.
■ All data requires memory space and the type of data
determines how much memory is needed.
■ For this reason, when we write programs, we must tell the
computer what type of data we are dealing with.
■ The programmer must define the data type of the
variables being used.
Data Types
■ Computer languages make use of three fundamental
data types: numeric data, character string (or
alphanumeric) data and Boolean type data.
– Numeric data can be further divided into two major
types: integer and floating point.
– Character data can be further divided into single
character data and string data.
– Boolean data variables can only have a value of either
true or false
Data Types
■ Before you begin to use a variable, you must explain to
the computer that you want a storage location set aside
and you want to give that location a name.
■ In other words, you must declare your variable
■ Different programming languages use different syntax to
declare variables.
Character Data Type
■ A character is any symbol that can be typed at the
keyboard.
■ This includes the letters of the alphabet (uppercase and
lowercase), numbers, punctuation marks, spaces, and
some of the extra characters that a computer keyboard
contains, such as the pipe key ( | ), the various types of
brackets (curly brackets { } or square brackets [ ]), and
other special characters like $, &, <, >, and so forth.
■ Example: Grade = ‘A’
String Data Type
■ A string is a sequence of characters.
■ In most programming languages, strings are enclosed in
quotation marks (“ ”).
■ Example: val = "Enter the first number:”
ans = “My name is Peter.”
■ A single character is also considered to be a string, so "B"
and "g" are strings.
■ A string may be void of any characters, it’s called the null
string
Operations on Strings
■ In many programming languages, concatenation, takes
two strings and joins them to produce a string result.
■ The symbol that is often used to concatenate two strings
is the plus sign, +.
■ For example, if String1 = "Part" and String2 = "Time", then
the statement
Set NewString = String1 + String2
■ The value of NewString is “Part Time”
Example
■ What is displayed on the screen when the following is
executed:
Declare ItemName, TextString, ItemCost As String
ItemName = "Cashmere sweater ”
TextString = "will cost $ ”
ItemCost = 125
Write ItemName + TextString + ItemCost
Integer Data Type
■ Most programming languages allow at least two types of
numeric data to be used in programs: integers and
floating point numbers.
■ Integer data consists of all the whole numbers, negative,
zero, and positive.
■ Floating point data consists of all numbers that include
an integer part and a decimal part.
– While the number 8 is an integer, the number 8.0 is
considered as a float in programming. It has a decimal
part
Operations on Integer Data
■ The six arithmetic operators (+, –, *, /, %, ˆ) may be used
on calculations with integers.
■ The result of adding, subtracting, exponentiation,
multiplying, or taking the modulus of a pair of integers is
another integer.
■ Example: 5 + 2 = 7 5 – 2 = 3 5 * 2 = 10 5ˆ2 = 25
5%2=1
■ The result of the division operator on two integers is not
normally an integer: 5 / 2 = 2.5
Declare Statement
■ When we declare variables in our pseudo-code we will use
statements like the following:
Declare Number As Integer
Declare Number As Float
■ Many programming languages also allow you to declare
several variables in one statement, so long as all the
variables in that statement have the same data type.
Declare Num1, Num2 As Integer
Declare FirstName, LastName, FullName As String
Declare Price, DiscountRate As Float
Boolean Data Type
■ A Boolean variable can only have a value of either true or
false.
■ Most programming languages have a data type that
allows for either true or false as the only options. Some
names of this data type are Boolean, boolean, or bool.
■ Example: Declare Flag As Boolean
Flag = false
Try
■ If X = 2 and Y = 3, give the value of each of the following
expressions:
1. (2 * X – 1) ˆ 2 + Y
2. X * Y + 10 * X / (7 – Y)
3. (4 + (2 ˆ Y) ) * (X + 1) / Y
4. (19 % 5) * Y / X * 2
Next topic
■Algorithms
■Pseudocode
■Flowcharts
■Types of Errors
References

1. https://www.bbc.co.uk/bitesize/guides/zfnny4j/revision/9
2. https://www.computerhope.com/jargon/p/processi.htm
3. https://byjus.com/maths/pemdas/

You might also like