Topic 3.0-3.3 Fundamentals of Programming Language
Topic 3.0-3.3 Fundamentals of Programming Language
CHAPTER 3
FUNDAMENTALS OF
PROGRAMMING LANGUAGE
Course Learning Outcome
(CLO):
Upon completion of this course, students should be able to:
What is data?
Input for the program
Pre-processing fact
Data types:
Numeric
Non-numeric
Data Types: Numeric
2 types:
Integer Number (…..-2,-1,0,1,2…..)
Explicit Number (Floating number)
Data Types: Non Numeric
Identifier
Variables
Constant
IDENTIFIER
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
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)
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
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
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
Count
Total marks = continuous
evaluation + final evaluation
End
EXERCISE..
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..
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
End
EXERCISE..
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
T T T T
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 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
True
Condition
Loop body
False
‘While’ Example
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
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
False
End
‘Do-While’ Source Code
‘For’ loop
Suitable if we know the number of iterations.
for (expr1;expr2;expr3)
{
s1;
s2 ;
}
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