Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 38

CS101 - PROBLEM SOLVING &

PROGRAMMING

Lecture 5 (online)
Introduction to Computers & Programming
Week 18 May – 22 May 2020
Zoubeir Aungnoo
Lecture Aims

■ The C Character Set


■ Data Types
■ Variables
– Declaration, Assignment & Initialization
■ Symbolic constants
■ Operators & Expressions
– Arithmetic operators
– Operators Precedence & Associativity
– Unary operators
– Relational & logical operators
– Assignment operator
– Conditional operator
C Character Set
■ C uses the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to
9, and certain special characters
■ as building blocks to form basic program elements (e.g., constants, variables,
operators, expressions, etc.).
■ The special characters are listed below

■ C uses certain combinations of these characters, such as \b, \n and \t, to


represent special conditions such as backspace, newline and horizontal tab,
respectively.
■ These character combinations are known as escape sequences.
Data Types
■ C supports several different types of data, (represented differently within the
computer’s memory)

■ If char holds a single character, so how do we store multiple characters, i.e.


words and sentences??
– Which datatype do we use in this case?
A p p l e

■ Apple

■ Create an array to hold the multiple characters


Data Types
■ C compilers generally represent a word as 4 bytes (32 bits)
■ The basic data types can be augmented by the use of the data type qualifiers
short, long, signed and unsigned.
– For example, integer quantities can be defined as short int, long int or
unsigned int
– These data types are usually written simply as short, long or unsigned,
and are understood to be integers
■ The interpretation of a qualified integer data type will vary from one C
compiler to another, though there are some common-sense relationships.
– Thus, a short int may require less memory than an ordinary int or it
may require the same amount of memory as an ordinary int, but it will
never exceed an ordinary int in word length.
– Similarly, a long int may require the same amount of memory as an
ordinary int or it may require more memory, but it will never be less
than an ordinary int
■ int can vary from -32,768 to +32,767 (for a 2-bytes int)
Variables
■ Variable:
– a container which holds information which can change/vary
– a variable must have a name., i.e. given an identifier (remember the
rules of identifier – refer to Lecture 04 Slide 19)
– A variable is used to represent a single data item of a specific type
– The data item must be assigned (saved) to the variable at some point in
the program
– and then can be accessed in the program by referring to the variable
name
– The data item can change during the program but not the data type
Variables - Declaration
■ A declaration associates one or more variables to a data type. All variables
must be declared before they can be used.
■ Syntax to declare a variable:
■ <datatype> <identifier>;
■ E.g.

Single variable declaration

multiple variables declaration on one


line.
Note: all must be of same data type
Variables – Assignment
■ Declaration creates the container to hold values while Assignment takes the
value and place it into the variable.
■ In programming, we use the equals ( = ) symbol for assignment:
– Note you can only assign values after you have declared a variable
– So we are considering the variables we declared in the previous slide
Variables – Assignment
■ NOTE:
■ In programming, the = symbol does not mean equality
■ It’s just a symbol which can be interpreted differently
■ In programming, the = symbol we call it the ‘Assignment Operator’
■ As we have seen, it takes a value and save it in a variable.

■ noOfStudents = 50;

■ The above statement means: take 50 and store it in the variable


noOfStudents
■ It DOES NOT MEAN: noOfStudents is the same as 50 or noOfStudents
is equal to 50
■ We will learn later how to test for equality
Variable Declaration & Initialization

■ Now, what if you want to assign a value to a variable the moment you
declare it?
– This is possible and we call it Initialization

■ Syntax to declare and initialize a variable:


■ <datatype> <identifier> = initialValue;

■ Here are some examples:


How to know which variable(s) will
be needed?
■ As a good practice, you declare all your variables at the very beginning of
your program.
– Right after main()

■ This implies that you need to know in advance which variables you will need
in your program
– That’s why you should have done pseudocodes & flowcharts
– From the pseudocodes & flowcharts you are able to identify variables
you will need in your program
Symbolic Constants
■ A symbolic constant is a name given to some numeric value or a character
constant or a string constant.
■ They are written as pre-processor directives as follows:
■ #define SYMBOLIC_CONST_NAME value_of_the_constant
Symbolic Constants
■ E.g.

■ Output:
OPERATORS &
EXPRESSIONS
Operators & Operands
■ The data items that operators act upon are called operands
■ Some operators require two operands while others act upon only one
operand
■ E.g.
Operator

■ sum = number1 + number2;

Operands

– The + symbol is called the operator


– number1 and number2 are called the operands
– As we know to perform addition we need 2 values, i.e. 2 operands
Arithmetic Operators
■ There are five arithmetic operators in C.

■ Operands must be numeric values


■ Thus, the operands can be integers, floating-point values or characters
– remember that character constants represent integer values, as
determined by the computer’s character set – ASCII
Arithmetic Operators
■ The remainder operator (%) requires that both operands be integers and the
second operand be nonzero.

■ The division operator (/) requires that the second operand be nonzero.
– We can’t divide by 0!
■ Division of one integer quantity by another is referred to as integer division
– Always results in a truncated quotient (i.e., the decimal portion of the
quotient will be dropped).
– E.g. on next slide
■ If a division operation is carried out with: (i) two floating-point numbers, or
(ii) with one floating-point number and one integer, the result will be a
floating-point quotient.
– E.g. on next slide
Arithmetic Operators
■ Integer and floating point division:
Arithmetic Operators
■ Example 1: suppose that a and b are integer variables whose values are 10
and 3 respectively

Expression Value
a+b 13
a–b 7
b–a -7
a*b 30
a/b 3 (integer division)
a%b 1
Arithmetic Operators
■ Example 2: suppose that v1 and v2 are floating-point variables whose values
are 12.5 and 2.0 respectively

Expression Value
v1 + v2 14.5
v1 – v2 10.5
v2 – v1 -10.5
v1 * v2 25.0
v1 / v2 6.25
v1 % v2 INVALID
Arithmetic Operators
■ Example 3: suppose that c1 and c2 are character type variables that represent
characters ‘P’ and ‘T’ respectively:
■ Note: from the ASCII table (Lecture 01 Slide 16), character ‘P’ has a value
of 80, ‘T’ has a value of 84 and ‘5’ has a value of 53

Expression Value
c1 80
c1 + c2 164
c1 + c2 + 5 ??
c1 + c2 + ‘5’ ??
Arithmetic Operators
■ Example 4: suppose that a and b are integer variables whose values are 11
and -3 respectively

Expression Value
a+b 8
a–b 14
b–a -14
a*b -33
a/b -3
a%b 2
Arithmetic Operators
■ Example 5: suppose that a and b are integer variables whose values are ---11
and 3 respectively

Expression Value
a+b -8
a–b -14
b–a 14
a*b -33
a/b -3
a%b -2

■ What can you deduce about the modulus (%) operator when there’s one
or both of the operands are negative value?
Arithmetic Operators
■ The interpretation of the modulus/remainder (%) operator is unclear when
one of the operand is negative.
■ Most versions of C assign the sign of the first operand to the remainder
Type Casting
■ Suppose that i is an integer variable whose value is 7 and f is a floating point
variable whose value is 8.5
■ What is the output of this expression:
– (i + f) % 4
Type Casting
■ Suppose that i is an integer variable whose value is 7 and f is a floating point
variable whose value is 8.5
■ What is the output of this expression:
– (i + f) % 4

■ This results in an error. It is an invalid statement

■ Remember (slide 17): The remainder operator (%) requires that both
operands be integers and the second operand be nonzero.
Type Casting
■ So, we can cast (convert) to a different type
■ To do so, the expression must be preceded by the name of the desired data
type enclosed in parenthese
■ Syntax:
■ (datatype) expression

■ Suppose that i is an integer variable whose value is 7 and f is a floating point


variable whose value is 8.5
■ What is the output of this expression:
– (i + f) % 4 >>> INVALID
■ Solution
– ((int) (i + f)) % 4 >>> VALID (you try it)
Precedence
■ Evaluate:
– 2*((i%5)*(4+(j–3)/(k+2)))
– Where i = 8, j = 15 and k = 4

■ Forget BODMAS, in programming we use the rules of Precedence &


Associativity
– * , / and % have higher precedence than
– + and –
■ But how to evaluate:
– 8 * 7 / 3 % 2 ???

– Here we use associativity to determine which operator to carry out first


when all of them have the same precedence

■ Find out the full table of Precedence and Associativity for C. (Lab question)
Unary Operator
■ Unary operator means that the operator acts on a single operand to produce a
new value
■ We have 3 common unary operator:

Operator Purpose

- Minus: convert from positive to negative.


E.g. –789, -0.2, - ( b * c)
++ Increment: increase by 1

Decrement: decrease by 1
--
Unary Operator
■ ++ operator means increase by 1
■ E.g. Suppose that i = 5.
– ++i is equivalent to i = i + 1
– So the new value is 6

■ -- operator means decrease by 1


■ E.g. Suppose that i = 5.
– --i is equivalent to i = i - 1
– So the new value is 4

■ The increment and decrement operators can be used in two ways: pre and post.
– ++i (pre – increment): means increment the value of i by 1 and then use
that value
– i++ (post – increment): means use the value of i first and then increment
it
Unary Operator
■ E.g.

■ Output:
Relational Operator
■ There are 4 relational operators:
– Their precedence is lower than arithmetic and unary operators

Operator Meaning

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to


Equality Operator
■ There are 2 equality operators:
– Their precedence is lower than relational operators

Operator Meaning
== Equal to
!= Not equal to

■ So, remember that we use == operator to check if two expressions are same
and not the = operator


Relational & Equality Operators
■ Suppose that i, j, and k are integer variables whose values are 1, 2 and 3
respectively

Expression Interpretation Value

i<j true 1

(i + j) >= k true 1

(j + k) > (i + 5) false 0

k != 3 false 0

j == 2 true 1
Logical Operator
■ We have 2 logical operators (logical connectives)
■ They connect conditions together to evaluate the truth or falsehood of groups
of expressions

Operator Meaning

&& and

|| (pipe) or

■ &&
– ALL the conditions must be true for the output to be true
■ ||
– At least one condition must be true for the output to be true
Relational & Logical Operators
■ Suppose that i is an integer variable whose value is 7, f is a floating point
variable whose value is 5.5 and ch is a character variable that represents the
character ‘w’

Expression Interpretation Value


(i >= 6) && (ch == ‘w’) True 1
(i >= 10) && (ch == ‘w’) False 0

(i >= 6) || (ch == ‘w’) True 1


(i >= 10) || (c == ‘w’) True 1
(i >= 6) || (c == 119) True 1

(f < 11) && (i > 100) False 0

(ch != ‘p’) || ((i + f) <= 10) True 1


QUESTIONS?

You might also like