Pert 4: Control Structure
description
Transcript of Pert 4: Control Structure
Pert 4: Control Structure
Tujuan
• Memahami struktur kontrol program• Dapat menerapkan:– If– While– Do while– For Dalam pembuatan program sederhana
Materi Hari ini
Alur program
Terurut
Percabangan
Perulangan
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.
Mengenal Boolean
True
False
16>10
16<10
boolean benar=true;boolean lulus=false;
Decision
Outcome 1 Outcome 2
Optio
n 1 Option 2
Model Keputusan I
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.
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.
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”
Model Keputusan II
Decision
Outcome 1
Opti
on 1
Option 2 has no outcome
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
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.
Output
sale < 25.0
total SHIPPING_FEE;System.out.println("Shipping is $5.00");
if (kondisi pilihan){//statemen ketika kondisi true
}int jumlah=100;if(jumlah>50){ System.out.println(“Lebih dari 50”);}
Nilai booleanTrue False
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
A loop around a horse’s neck, a circus ring, and a loop around the thoughts in a cartoon character’shead are all shown.
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
A person drives a car back home (‘‘loops back’’) to pick up a friend who is standing next to the house
In a program, a loop describes a group of one or more lines of
code that must berepeated some number of times
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
Loop
While
Do While
For
Whi
leEkpresii Boolean
(kondisi)
Statement 1
Statement 2
Statement n
Statement after loop
True
False
Example while loop
• 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.
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