Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
POLITEKNIK MALAYSIA
1. DFC1023
PROBLEM SOLVING & PROGRAM DESIGN
C H A P T E R 3 :
FUNDAMENTALS OF PROGRAMMING
LANGUAGE
S U B T O P I C 3 . 3 :
PROGRAM CONTROL STRUCTURES
2. AT THE END OF THIS CHAPTER, STUDENTS SHOULD BE ABLE
TO:
Apply program control structure
1.Control structure in problem solving :
Sequence
Selection
Repetition
1.Illustrate flow of control structure using pseudo
code and flow chart.
2.Write pseudo code and flow chart using control
structure.
3. Design the algorithm for a given case study.
3. PROGRAM CONTROL STRUCTURE
In 1966, two researchers, C. Bohn and G.Jacopini,
demonstrated that any algorithm can be described using
only 3 control structures:
1. Sequence control structure
2. Selection/decision control structure
1. If……endif
2. If……else
3. Nested if
3. Looping control structure
1. For
2. While
3. Do……while
4. SEQUENCE CONTROL STRUCTURE
• In this control, every step will be executed one by
one from top to bottom.
• Every box in control structure is a process. Every
process is done sequentially.
• The beginning and end of a block of statements
can be optionally marked with the keywords
begin/start and end.
9. SELECTION / DECISION CONTROL
STRUCTURE
Usually used in structured programming.
Control structure will execute an instruction based on result
of a condition or comparison, the result either TRUE or
FALSE.
If the condition result is true, the control program will
execute the instruction within the TRUE loop operation.
Otherwise, it will execute the next instruction or the
instruction within the FALSE loop operation.
10. SELECTION / DECISION CONTROL
STRUCTURE
Type of selection / decision control
structure:
1. If……endif
2. If……else
3. Nested if
11. SELECTION / DECISION CONTROL
STRUCTURE
1. If……endif
Rules:
If (condition)
Instruction (do this instruction if
condition is true)
Endif
If condition is not true, no instruction will
be executed
12. SELECTION / DECISION CONTROL
STRUCTURE
Pseudocode:
If (condition)
True statement
Endif
START
Condition Statement
END
False
True
• Flowchart
13. SELECTION / DECISION CONTROL
STRUCTURE
Problem
If student’s grade is greater than or equal to
60
Print “Passed”
Pseudocode:
if ( grade >= 60 )
print “ Passed”
endif
15. SELECTION / DECISION CONTROL
STRUCTURE
2) IF…….Else
• Rules:
If (condition)
True statement
Else
False statement
Endif
16. SELECTION / DECISION CONTROL
STRUCTURE
Pseudocode:
If (condition)
True statement
Else
False statement
Endif
Flowchart
START
Condition
END
False True
Statement 2 Statement 1
17. SELECTION / DECISION CONTROL
STRUCTURE
Problem
If student’s grade is greater than or equal to 60
Print “Passed” else Print “Failed”
Pseudocode:
if ( grade >= 60 )
print “ Passed”
else
print “ Failed”
endif
19. SELECTION / DECISION CONTROL
STRUCTURE
3. NESTED IF
There are 3 types:
Type 1:
If (condition1)
If (condition2)
If (condition3)
True statement
Endif
Endif
Endif
20. SELECTION / DECISION CONTROL
STRUCTURE
• Type 2:
If (condition1)
If (condition2)
If (condition3)
Statement that will be executed if condition1,
condition2 and condition3 are true
Else
Statement that will be executed if condition1,
and condition2 are true but condition3 is false
Endif
Else
Statement that will be executed if condition1 is
true but condition2 and condition3 is false
Endif
Else
Statement that will be executed if condition1 is
false
Endif
21. SELECTION / DECISION CONTROL
STRUCTURE
• Type 3:
If (condition1)
Statement that will be executed if condition 1 is true
Else
If (condition 2)
Statement that will be executed if condition2 is true but condition1 is
false
Else
If (condition3)
Statement that will be executed if condition3 is true but
condition1 and condition2 are false
Else
Statement that will be executed if condition1, condition2 and
condition3 are false
Endif
Endif
22. SELECTION / DECISION CONTROL
STRUCTURE
Problem
Pseudo code
If (grade >=90)
Print “A”
Else If (grade >=80)
Print “B”
Else If (grade >=70)
Print “C”
Else If (grade >=60)
Print “D”
Else
Print “F”
23. REPETITION CONTROL STRUCTURE
Problem : Develop a program to print “FP101 is easy” for 5
times without looping.
Solution:
Understand the problem: repeat print “DFC1023 is easy” 5
times.
1. Problem Analysis:
Input : None
Proses : None
Output : print “DFC1023 is easy” for 5 times
24. REPETITION CONTROL STRUCTURE
2. Algorithm:
1. print “DFC1023 is easy” for 5
times.
3. Pseudocode:
START
print “DFC1023 is easy” for 5
times;
END
4. Flow Chart
Print “DFC1023 is easy”
Print “DFC1023 is easy”
Print “DFC1023 is easy”
Print “DFC1023 is easy”
Print “DFC1023 is easy”
START
END
25. 3 BASIC TYPES OF REPETITION
CONTROL:
•From one number to
another number and
increases/decrease
by a specified value
each time
for loop
•The while loop can
be used if we don’t
know how many
times a loop must
run.
while loop
•DO..WHILE loops are
useful for things that
want to repeat at
least once.
do …
while loop
do
{
do repeat statement
} while ( condition );
while ( condition )
{
Process repeat
while the condition
is true
}
for ( variable initialization;
end condition; variable
update )
{
Repeat statement
process
}
26. FLOW CHART
while
is
i < 10?
print i
i = i+1
is
i < 10?
print i
i = i+1
do-while
i++
i < 10?Print i
i = 0
End
No
Yes
For
Yes
Yes
No
No
3 basic types of Repetition Control:
Start
End
Start
End
Start
i = 0 i = 0
27. CASE STUDY
Problem : Develop a program can print
“DFC1023 is easy” for 5 times using
looping.
Solution:
Understand the problem:
repeat print “DFC1023 is easy” 5 times .
28. 1.SOLUTION USING FOR CONTROL LOOP
1. Problem Analysis:
Input:
None
Process:
print “FP 101 is easy”
repeat until the condition
(5 times) to exit the loop is
met
Output:
print “FP101 is easy”
2. Algorithm:
1. Set n = 0
2. Check condition (n < 5)
If true,
print “FP101 is easy”.
Go to step 3.
If false, process will end.
3. Increase counter using
formula :
n = n + 1
Go to step 2.
29. 1. SOLUTION USING FOR CONTROL LOOP
3. Pseudo code:
Start
For (n=0; n<5; n++)
{
print “DFC1023 is easy”;
}
End
4. Flow Chart
n++
n< 5?Print “DFC1023 is
easy”
n = 0
End
Start
No
Yes
30. 2.SOLUTION USING WHILE CONTROL LOOP
1. Problem Analysis:
Input:
None
Process:
print “DFC1023 is easy”
repeat until the condition
(5 times) to exit the loop is
met
Output:
print “FP101 is easy”
2. Algorithm:
1. Set n=0
2. Check condition (n < 5)
If true,
print “DFC1023 is easy”.
Go to step 3.
If false, process will end.
3. Increase counter using
formula :
n = n+1
Go to step 2.
31. 2. SOLUTION USING WHILE CONTROL LOOP
3. Pseudo code:
Start
Set n = 0;
While (n < 5)
{
print “DFC1023 is easy”;
n++;
}
End while
End
4. Flow Chart
is
n < 5?
print “DFC1023 is easy”
n = n+1
Yes
No
End
Start
n = 0
32. 3. SOLUTION USING DO…WHILE CONTROL
LOOP
1. Problem Analysis:
Input:
None
Process:
print “DFC1023 is easy”
repeat until the condition
(5 times) to exit the loop is
met
Output:
print “FP101 is easy”
2. Algorithm:
1. Set n = 0
2. Print “DFC1023 is easy”.
3. Increase counter using
formula :
n = n+1
4.Check condition (n < 5)
If true, Go to step 2.
If false, process will end.
33. 3. SOLUTION USING DO…WHILE CONTROL
LOOP
3. Pseudo code:
Start
Set n = 0;
Do
{
print “DFC1023 is easy”;
n++;
} While (n < 5)
End
4. Flow Chart
End
Start
is
n < 5?
print “DFC1023 is
easy”
Yes
No
n = n+1
n = 0