Latihan
• Buat algoritma untuk menampilkan output berikut:*****
• Buat algoritma untuk menampilkan tulisan berikut:
iteration number 1iteration number 2iteration number 3iteration number 4iteration number 5after loop ends
• Buat algoritma untuk menampilkan output berikut
student no-1 is : student no-2 is : student no-3 is :
• Buat algoritma untuk menerima inputan tiga buah bilangan real, lalu menghitung rata-ratanya, dan menampilkan hasilnya ke layar
Write the algorithm to calculate cost of electricitywith following constraints :
(KWHs) Rate 0 - 500 2000501 - 1000 2000 + 0.03 * per KWH over 1001 3500 + 0.02 * per KWH
Enter the values of previous month meters, current month meter, and then displays the total cost which would exist.
7
The structure of the FOR-TO-DO statement
FOR counter variable initial_value TO final_value program statement
ENDFOR
8
Flowchart FOR-TO-DO
No
yes
STARTSTART
ENDEND
Counter >= initial_value AND
counter <= final_value ?
Counter >= initial_value AND
counter <= final_value ?
actionaction
{initialize}{initialize}
9
Example 1
Pseudocode
FOR x1 TO 5 DO
output (x)
ENDFOR
Flowchart
x >=1 AND x <= 5 ?
x >=1 AND x <= 5 ?
START
ENDoutput (x)
No
yes
10
Example 2
PseudocodeOUTPUT('before loop starts') FOR i = 1 TO 5 DO
OUTPUT('iteration number ',i) ENDFOR OUTPUT('after loop ends')
11
Execution
• generates the output
Iteration no.
i i >=1 AND
i<= 5 ?
Output
before loop starts
1 1 True iteration number 1
2 2 True iteration number 2
3 3 True iteration number 3
4 4 True iteration number 4
5 5 True iteration number 5
6 6 False after loop ends
12
Example 3
FOR i 1 TO 5 DO Output (‘*’) {action statement}
ENDFOR {end loop}The result of program :
*****
13
Example 4• Program with enter the value of 3 student’s scores :
Pseudocode
FOR i 1 TO 3 DOOutput (‘Score student no-‘,i,’ is:’)Input (score)
ENDFOR• Execution :
– Score student no-1 is : __– Score student no-2 is : __– Score student no-3 is : __
14
Example 5
• Average of 3 numbersnumber 0 {initialize}FOR i 1 TO 3 DO
Input (x) {input value of x}number number + x {sum of x variables}
ENDFOR average number/ 3 {calculate the average}Output (average)
Top Related