The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Topic 3 - Fundamentals Of Programming Language (2)

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by j05stm21f504, 2022-10-01 02:47:14

Topic 3 - Fundamentals Of Programming Language (2)

Topic 3 - Fundamentals Of Programming Language (2)

DFC1023
PROBLEM SOLVING &

PROGRAM DESIGN

CHAPTER 3
FUNDAMENTALS OF
PROGRAMMING LANGUAGE

Course Learning Outcome
(CLO):

Upon completion of this course, students should be able to:

1) Explain the basic computer and programming fundamentals with
appropriate examples of languages.

2) Apply the different types of algorithm to solve problem efficiently.

3) Solve problem by applying related theories of the basic
programming technique to a given particular scenario using
programming life cycle.

Topic Specification

3.1 Understand data and identifier

3.2 Solve problem using operators in a
program

3.3 Apply program control structure

3.1 Understand Data & Identifier

Understand Data & Identifier

▪ What is data?
▪ Input for the program
▪ Pre-processing fact

▪ Data types:
▪ Numeric
▪ Non-numeric

Data Types: Numeric

▪ A type of data that use to do a calculation
only.

▪ 2 types:

▪ Integer Number (…..-2,-1,0,1,2…..)
▪ Explicit Number (Floating number)

Data Types: Non Numeric

▪ Consist character, number, words, and
specific symbol.

▪ We put them inside the apostrophe (‘ ’).

▪ Example: ‘A’, ‘task2’ , ‘&’ , ‘()’

Data Types Table :

Data Types Description Example
-5, -90, 5, 0, 34
Numeric Integer Integers only

Floating Integers + floating -8.9 , 9.6, 2
numbers 9.001, 0.7

Non-numeric Characters, numbers and ‘A’, ‘a’, ‘+’, ‘()’
symbol

EXERCISE

▪ Determine the data types:

▪ 2337
▪ 0.56
▪ –4.7
▪ –2
▪ ‘%’
▪ ‘/’

Identify Terms:

▪ Identifier
▪ Variables
▪ Constant

IDENTIFIER

▪ The given name to multiple elements in
programming such as constant, variables
and function.

VARIABLES

▪ Location memory
▪ Will keep data value
▪ The value are changeable during entire

programming execution.

VARIABLES

a = 5; Declaration of variables
b = 2;
a = a + 1; int a;
result = a - b; int b;
int c;

VARIABLES

// operating with variables
#include <iostream>
using namespace std;
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
return 0;
}

CONSTANT

▪ Constants are expressions with a fixed
value.

▪ The value are not changeable during
entire programming execution.

▪ Example:

const int days_in_year = 365;
const float ChickenPerKg= 17.5;

CONSTANT vs. VARIABLES

Constant Variables

Value :- 25 Variables name: Age
Value : - 3.2 Value :- 25, 30, 15
Value :- Kangar
Variables name: Cash
Value :- 2.5, 3.0, 1.5

Variables name: City
Value :- Ipoh, Kuala Lumpur,
Georgetown

How to write identifier?

▪ Combination of:
▪ Character (A - Z), (a - z)
▪ Digit (0 - 9)
▪ Underscore (_)

▪ Cannot start with digit
▪ Not have reserve words
▪ No blank space
▪ No limit of character usage but the system will

identify first 32 character
▪ Case sensitive

EXERCISE

Determine either this is a valid identifier:

1. my_school
2. 100students
3. Vari,ables
4. PoliteknikBalikPulau
5. PLAYINGFOOTBALL
6. Playing Football
7. Saya_suka_membaca
8. %fail

3.2 Solve problem Using
Operators in a Program

Operators in a Program

▪ What is OPERATOR?
▪ A symbol to represent particular computer
operation.

▪ Consists of:
i. Arithmetic Operators
ii. Relational Operators

iii. Logical Operators

ARITHMETIC
OPERATOR

Arithmetic Operator

▪ It have 5 basic operator in programming

language: Symbol Operator

+ Addition

_ Subtraction

* Multiplication

/ Division

% Modulus

Arithmetic Operator

▪ Modulus (%)?

▪ To get balance from two handling division.
▪ i.e.:
a) 5 % 3 is 2

1 BALANCE IS 2!
3

5
3

2

b) 17 % 4 is 1. BALANCE IS 1!

4
41

71
6
1

Operator Priority

▪ It have a basic priority that should know :

Operator Priority
( ) Highest
* / % Higher
+ - Lower

Operator Priority

a) x = 5 + 2 * 4 – 1
x = 5+2*4–1
x = 5+8–1
x = 13 - 1
x = 12

for the same priority,
start from left side.

Operator Priority

b) x = ( 5 + 2 ) * ( 4 - 1) Bracket has a highest
priority!!
x = ( 5 + 2 ) * ( 4 - 1)
x= 7*3
x = 21

EXERCISE
Get the answer for this question:

a) 9 + 1 – 2 8
b) 2 * 5 + 2 / 1 12
c) 12 % 5 2
d) 4 + (7 - 3) * 3 16
e) ( 2 * 4 ) * 2 – ( 6 + 2 ) / 8 15

RELATIONAL OPERATOR

Relational Operation

▪ To compare 2 operator.
▪ Same data type, i.e. integer, character or string.
▪ The result is either TRUE or FALSE.

Symbol Description
> Greater than
<
>= Less than
<= Greater or equal than
==
!= Less or equal than
Equal with

Not equal with

LOGICAL
OPERATOR

Logical Operator

▪ To test some operation
▪ Have 3 symbol:

Symbol Description
&& AND
|| OR
! NOT

Logical Operator

Base on TRUTH TABLE.

P Q P && Q P || Q
FALSE FALSE FALSE FALSE
FALSE TRUE FALSE TRUE
TRUE FALSE FALSE TRUE
TRUE TRUE TRUE TRUE

Logical Operator

Example:

Given a=3 and b=5;
a) x = (a > 0) && (b > 0)

The x value is TRUE because 3 is greater
than 0 AND 5 is greater than 0 as well.
b) x = ( a < b ) && ( b == 0 )
The x value is FALSE . Although 3 is less than
5, but 5 is not equal with zero.

EXERCISE

Find x value either TRUE or FALSE from the
following equation:

Given a=2 and b=4; ( b != 0 )
a) x = ( a != 0 ) | | (b==0)
b) x = ( a == b ) | |
c) x = ! ( a == b )
d) x = ! ( a < b)

Another Example:

a = ! ( 2 > 5) | | 6 + 3 >= 4 – 3;
! (FALSE) | | (9 >= 1 )
TRUE | | TRUE
a = TRUE

INCREMENT
AND

DECREMENT OPERATOR

Increment And Decrement
Operator

▪ Sometimes, we need to increment or
decrement a single value in programming.

▪ The value is as below: Symbol Operator

++ Add 1

-- Minus 1

▪ Valid for variables only i.e. a++, b++, a--, b--,
x++, y-- ……… and invalid for constant
i.e. 5-- or 9++.

Increment And Decrement
Operator

▪ The ‘++’ and ‘--’ can be put before or after
variables.

▪ i.e. we can write a++ or ++a, b-- or --b.

Example

Given; a=3
b=5

a++ a value is 4
a-- a value is 2
b++ b value is 6
b-- b value is 4

EXERCISE

Given; x = 8

y=6
Find value for:

1. x++
2. x--
3. y++
4. y–
5. ++x
6. --y

3.3 Apply Program Control
Structures

Logical Structures

▪ 3 types of program control structures:

i. Sequence
ii. Selection
iii. Repetition

SEQUENCE

Sequence

▪ Command set which is execute line by line.
▪ Follows logic flow.
Example:
▪ You need to develop the program which can

read student name and count their total mark
for one semester. Then the program can print
the marks.
▪ Formula:

Total marks = continuous evaluation + final evaluation

Answer : Algorithm

1. Read student name
2. Read continuous evaluation marks
3. Read final evaluation marks
4. Count total marks by add continuous

evaluation and final evaluation
5. Print student name and their total marks.

Answer: Pseudo Code

Begin
Read student name, continuous evaluation

marks, final evaluation marks
Count total marks=

continuous evaluation + final evaluation
Print student name and total marks
End

Answer : Flow Chart

Start

Read student name,
continuous evaluation,

final evaluation

Count
Total marks = continuous
evaluation + final evaluation

Print students name, total
marks

End

EXERCISE..

▪ Develop a program which can count a
room wide.

Wide = Length x Width

SELECTION


Click to View FlipBook Version