Pert 4: Control Structure
description
Transcript of Pert 4: Control Structure
![Page 1: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/1.jpg)
Pert 4: Control Structure
![Page 2: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/2.jpg)
Tujuan
• Memahami struktur kontrol program• Dapat menerapkan:– If– While– Do while– For Dalam pembuatan program sederhana
![Page 3: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/3.jpg)
Materi Hari ini
Alur program
Terurut
Percabangan
Perulangan
![Page 4: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/4.jpg)
A control statement in a programming language is a
statement that allows the compiler to alter its normal execution of line-by-line execution of code.
![Page 5: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/5.jpg)
Mengenal Boolean
True
False
16>10
16<10
boolean benar=true;boolean lulus=false;
![Page 6: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/6.jpg)
Decision
Outcome 1 Outcome 2
Optio
n 1 Option 2
Model Keputusan I
![Page 7: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/7.jpg)
Heads or TailsAnother example of a decision is the one involved in ‘‘heads you win, tails you lose.’’ If you flip a coin and the head appears, then you have won. The otheroption is that the tail appears and you have lost.
![Page 8: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/8.jpg)
How to Spend Allowance If you spend it on a shirt you like, you will not have money for a CD. So you must make a decision.
![Page 9: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/9.jpg)
Terdapat dua option (pilihan), jika nilai>=60 maka akan dicetak “Lulus”, dan jika nilai kurang dari 65
maka dicetak “Tidak Lulus”
nilai>=65
Int nilai=60
Print “Lulus”
Yes
Print “Tidak Lulus”No
Ternyata nilai=60 maka pilihan yang ada akan jatuh pada Option 2, maka akan
dicetak “Tidak Lulus”
![Page 10: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/10.jpg)
Model Keputusan II
Decision
Outcome 1
Opti
on 1
Option 2 has no outcome
![Page 11: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/11.jpg)
nilai>=65
Int nilai=60
Print “Lulus”
Yes
Terdapat dua option (pilihan), jika nilai>=60 maka akan dicetak “Lulus”,
dan jika nilai kurang dari 65 maka dicetak “Tidak Lulus”
Ternyata nilai=60 maka pilihan yang ada akan jatuh pada Option 2, tapi option 2
tidak ada outcome
![Page 12: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/12.jpg)
Statemen if
When you buy an item from an online vendor, a $5.00 shipping fee is waived for purchases of $25.00 or more.Problem Statement Write a program that calculates the fi nal cost of an item, including sales tax and shipping, if applicable. Sales tax is 8% of the purchase price.
![Page 13: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/13.jpg)
![Page 14: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/14.jpg)
Output
sale < 25.0
total SHIPPING_FEE;System.out.println("Shipping is $5.00");
![Page 15: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/15.jpg)
if (kondisi pilihan){//statemen ketika kondisi true
}int jumlah=100;if(jumlah>50){ System.out.println(“Lebih dari 50”);}
Nilai booleanTrue False
![Page 16: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/16.jpg)
![Page 17: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/17.jpg)
Examples• If amount of the check is less than the balance,
boolean expression• subtract the amount of the check from the balance.
conclusion• If password entered at the keyboard is the same as the true
password,boolean expression
• provide access to the account.conclusion
• If your age is greater than 16,boolean expression
• apply for your driver’s license.conclusion
![Page 18: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/18.jpg)
A loop around a horse’s neck, a circus ring, and a loop around the thoughts in a cartoon character’shead are all shown.
![Page 19: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/19.jpg)
A loop brings to mind a circular
image.
a part of a program thatyou execute over and over again until you are permitted to leave
that part to get to another programming statement
![Page 20: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/20.jpg)
A person drives a car back home (‘‘loops back’’) to pick up a friend who is standing next to the house
![Page 21: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/21.jpg)
In a program, a loop describes a group of one or more lines of
code that must berepeated some number of times
![Page 22: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/22.jpg)
Computers, unlike humans, are tireless, experiencing neither boredom nor fatigue.
Repeating an operation millions of times presents no problem to a computer with an internal clock
that ticks billions of times every second
![Page 23: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/23.jpg)
Loop
While
Do While
For
![Page 24: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/24.jpg)
Whi
leEkpresii Boolean
(kondisi)
Statement 1
Statement 2
Statement n
Statement after loop
True
False
![Page 25: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/25.jpg)
Example while loop
![Page 26: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/26.jpg)
• Problem Statement Write a program that sums a list of integers supplied by a user. The list can be of any size. The program should prompt the user for the number of data.
• Java Solution The following application utilizes three variables: size, sum, and count .– size is the number of data;– sum holds a running sum of the numbers supplied by the user
so that each time the– user enters a number, that number is added to sum ; and– count keeps track of the number of data.
Variables sum and count are initialized to 0; the value of size is supplied by the user. The addition is accomplished using a while loop similar to the loop in the segment that precedes this example.
![Page 27: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/27.jpg)
![Page 28: Pert 4: Control Structure](https://reader036.fdocuments.us/reader036/viewer/2022081503/568165eb550346895dd90d17/html5/thumbnails/28.jpg)
0 0
sum count size
0 0 3
sum count size
5 1 3
sum count size
12 2 3
sum count size
21 3 3
sum count size