Pert 4: Control Structure

Download Report

Transcript 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
Model Keputusan I
Decision
Outcome 1
Outcome 2
Heads or Tails
Another 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 other
option 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”
Int nilai=60
No
nilai>=65
Print “Tidak Lulus”
Yes
Print “Lulus”
Ternyata nilai=60 maka
pilihan yang ada akan jatuh
pada Option 2, maka akan
dicetak “Tidak Lulus”
Model Keputusan II
Option 1
Decision
Outcome 1
Option 2 has no outcome
Int nilai=60
Terdapat dua option (pilihan), jika
nilai>=60 maka akan dicetak “Lulus”,
dan jika nilai kurang dari 65 maka
dicetak “Tidak Lulus”
nilai>=65
Yes
Print “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");
Nilai boolean
True
False
int jumlah=100;
if(jumlah>50){
System.out.println(“Lebih dari 50”);
}
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’s
head are all shown.
A loop brings to
mind a circular
image.
a part of a program that
you 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 be
repeated 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
Ekpresii Boolean
(kondisi)
While
True
Statement 1
Statement 2
Statement n
Statement after loop
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
0
0
sum
5
sum
12
sum
21
sum
count
1
count
2
count
3
count
size
3
size
3
size
3
size
3
size