[go: up one dir, main page]

0% found this document useful (0 votes)
52 views96 pages

Topic 3.0-3.3 Fundamentals of Programming Language

This document provides information about program design and problem solving using programming languages. It outlines 3 topics: data and identifiers, problem solving using operators, and control structures for problem solving. The first topic defines data types, variables, constants and identifiers. The second discusses arithmetic, relational, and logical operators and how to use them to solve problems. The third describes different control structures for programming including sequence, selection using if/else statements, and repetition. Examples and exercises are provided to help explain each concept.

Uploaded by

lokesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views96 pages

Topic 3.0-3.3 Fundamentals of Programming Language

This document provides information about program design and problem solving using programming languages. It outlines 3 topics: data and identifiers, problem solving using operators, and control structures for problem solving. The first topic defines data types, variables, constants and identifiers. The second discusses arithmetic, relational, and logical operators and how to use them to solve problems. The third describes different control structures for programming including sequence, selection using if/else statements, and repetition. Examples and exercises are provided to help explain each concept.

Uploaded by

lokesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 96

DFC10042

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 fundamentals of programming


languages in a given scenario.

2) Demonstrate effective communication both on orally or in writing


about problem solving skills by using different types of
programming tools to solve a given problem.
Topic Specification
3.1 Explain data and identifier

3.2 Show a problem solving skills using


operators in a program.

3.3 Describe control structures in problem


solving
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


Numeric Integer Integers only -5, -90, 5, 0, 34

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 Numeric, integer

 0.56 Numeric, floating


 –4.7 Numeric, floating
 –2 Numeric, integer
 ‘%’ Non-numeric
 ‘/’ Non-numeric
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

Declaration of variables

a = 5; int a;
b = 2; int b;
a = a + 1; int c;
result = a - b;
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 :- 25, 30, 15

Value : - 3.2 Variables name: Cash


Value :- 2.5, 3.0, 1.5

Value :- Kangar 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:


valid
1. my_school
invalid
2. 100students
3. Vari,ables invalid

4. PoliteknikBalikPulau valid

5. PLAYINGFOOTBALL valid

invalid
6. Playing Football
valid
7. Saya_suka_membaca
invalid
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
3
5
3

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

4
4 17
16

1
BALANCE IS 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


x = ( 5 + 2 ) * ( 4 - 1) priority!!

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;


a) x = ( a != 0 ) | | ( b != 0 )
b) x = ( a == b ) | | (b==0)
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 3
  b++  b value is 6
b--  b value is 5
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
Selection

 The expression which result on TRUE or


FALSE.
 Consists 2 keywords:
 if
 else
Selection
 The selection control structure can be
categorized into 4 groups :
1. if statement
2. if-else statement
3. if-else statement (nested)
4. Switch statement
IF Statement

 The IF statement will perform an action if


the condition is true and ignore the
action if the condition is false.
Example : IF Statement

if speed > 110 print “penalty”


Answer : Algorithm

1. Read speed
2. If speed more than 110, print “penalty”
Answer: Pseudo Code

Begin
Read speed
if speed> 110, print “penalty”
end if
End
Answer: Flow Chart
Begin

Read speed

Speed True
Print
> 110 “Penalty”

False

End
EXERCISE..

 If marks < 40 print “fail”.


IF-ELSE Statement
 The IF statement will perform an action if the
condition is true and perform another action if
the condition is false.
Example: if-else
statement
If speed > 110 print “penalty”
Else print “no penalty”
Answer : Algorithm

1. Read speed
2. If speed more than 110, print “penalty”
3. Else, print “no penalty”
Answer : Pseudo Code

Begin
Read speed
if speed> 110, print “penalty”
else print “no penalty”
end if
End
Answer : Flow Chart
Start

Read speed

Print “No False Speed True


Print
Penalty” > 110 “Penalty”

End
EXERCISE..

If marks < 40 print “fail”


Else print “pass”
IF-ELSE Statement
(Nested)
 The IF-ELSE nested statement is a condition
when we have an ‘if’ in another ‘if’ body.
 We use this control structure if we have many
selection to be handle with.
 Syntax:
if ….
else if …
else …
Example : If-Else Nested
Statement
Create a program that can recognize and print
the price base on user prompt code. If user
enter a non-available value, the program will
print “Code Not Recognize”.
Code Price (RM)

1 2.00

2 4.00

3 6.00

4 8.00
Example: Algorithm

1.Read code
2. If code equal to one, then print “RM2.00”
3. Else if code equal to 2, then print “RM4.00”
4. Else if code equal to 3, then print “RM6.00”
5. Else if code equal to 4, then print “RM8.00”
6. Else print “Code Not Recognize”
 
 
Example: Pseudo Code
Begin
Read code
if code = 1
print “RM2.00”
else if code = 2
print “RM4.00”
else if code = 3
print “RM6.00”
else if code = 4
print “RM8.00”
else
Print “Code Not Recognized”
End
Example: Flow Chart
Start
Print “Code
Not
Recognized”
Read
code
F

Code F Code F Code F Code


=1 =2 =3 =4

T T T T

Print Print Print Print


“RM2.00” “RM4.00” “RM6.00” “RM8.00”

End
EXERCISE..
Develop a program which can determine
a driver expertise by count their training
day.
Training day Expertise

0 None

1-3 Weak

4 - 10 Average

>10 Expert
Switch Statement

Limitation Keyword
Add Your Text

Can only
use for 2 Switch
data type,
integer and break
character
ONLY. default
Switch Statement
switch ( Variable)
{ case <caseValue> : void main ( )
Statement1; {
Statement2; int Num;
- cin>>Num;
StatementN; switch (Num)
break ; { case 1 :
case <CaseValue> : cout<<”You choose 1.”;
statement1; cout<<Thank you!”;
statement2; break ;
break; case 2 :
- cout<<” You choose 2.”;
- break ;
default : default :
statement1 cout<< “ choose 1 or 2 only.”;
statement2; cout<< “understand!);
} }
}
When user choose 1, system will display “ You choose 1”,” Thank you”.

When user choose 2, system will display “ You choose 2”

When user choose 88, system will display “ choose 1 or 2 only”.” understand!”.
#include <iostream>
using namespace std;
EXAMPLE 1
int main ()
{
// local variable declaration:
char grade = 'D';
 
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;

return 0;
}
REPETITION
Repetition

 Using loop structure.


 3 types of loop:
 While
 Do .. While
 For
‘While’ Loop

 Repeat as pre-condition is true


while (condition)
{
loop body
}
‘While’ Loop
 The while construct consists of a block of code
and a condition.
 The condition is evaluated, and if the condition
is true, the code within the block is executed.
 This repeats until the condition becomes false.
 Because while loops check the condition before
the block is executed, the control structure is
often also known as a pre-test loop.
‘While’ General

True
Condition

Loop body
False
‘While’ Example

 Create a program that can print the value


which is less than 5.
‘While’ Pseudo Code

Begin
Read value= 0
while value < 5
Print value
value=value+1
end while
End
‘While’ Flow Chart
Start

Value = 0

Value =
Value + 1
True
Value < 5 ?

Print Value
False

End
‘While’ Source Code
‘Do-While’ Loop
 Repeat as post-condition is true
do
{
loop body
}
while (condition)
‘Do-While’ Loop
 The do while construct consists of a block of code
and a condition.
 First, the code within the block is executed, and
then the condition is evaluated.
 If the condition is true the code within the block is
executed again. This repeats until the condition
becomes false.
 Because do while loops check the condition after
the block is executed, the control structure is often
also known as a post-test loop.
‘Do-While’ General

True

Loop body Condition

False
‘Do-While’ Example
 Create a program that can print the value
which is less than 5.
‘Do-While’ Pseudo code

Begin
Read value= 0
repeat
Print value
value=value+1
until value < 5
End
‘Do-While’ Flow Chart
Start

Value = 0

True

Print value Value =


Value < 5 ?
Value+1

False

End
‘Do-While’ Source Code
‘For’ loop
 Suitable if we know the number of iterations.
for (expr1;expr2;expr3)
{
        s1;
        s2 ;
    }

 expr1 is executed only once before looping.


 expr2 is a Boolean expression. If not given, it is
assumed to be true.
 If expr2 is false, the loop is terminated.
 After execution of the repeat section, expr3 is
executed.
‘For’ loop
 The for loop is often distinguished by an
explicit loop counter or loop variable.
 This allows the body of the for loop (the code
that is being repeatedly executed) to know
about the sequencing of each iteration.
 for loops are also typically used when the
number of iterations is known before entering
the loop.
‘For’ loop
1
2
5
for (i = 0, i < 5, i++)
{ 4
3
printf(“the numbers are %d \n”, i);
}
‘For’ Example
 Create a program that can print the value
which is less than 5.
‘For’ Pseudo code

Begin
Read value
For value < 5
Print value
value=value+1
End for
End
‘For’ Flow chart
Start

Value = 0

Value True
<5 Print value value ++

False

End
‘For’ Example

You might also like