290 likes | 421 Views
Pert 4: Control Structure. Tujuan. Memahami struktur kontrol program Dapat menerapkan: If While Do while For Dalam pembuatan program sederhana. Materi Hari ini.
E N D
Tujuan • Memahami struktur kontrol program • Dapat menerapkan: • If • While • Do while • For Dalam pembuatan program sederhana
A control statement in a programming language is a statement that allows thecompiler to alter its normal execution of line-by-line execution of code.
Mengenal Boolean 16>10 16<10 boolean benar=true; boolean lulus=false;
Model Keputusan I Decision Option 2 Option 1 Outcome 1 Outcome 2
Heads or TailsAnother example of a decision is the one involved in ‘‘heads you win, tails youlose.’’ 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 ashirt 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 nilai>=65 No Print “Tidak Lulus” Yes Ternyata nilai=60 maka pilihan yang ada akan jatuh pada Option 2, maka akan dicetak “Tidak Lulus” Print “Lulus”
Model Keputusan II Option 2 has no outcome Decision Option 1 Outcome 1
Terdapat dua option (pilihan), jika nilai>=60 maka akan dicetak “Lulus”, dan jika nilai kurang dari 65 maka dicetak “Tidak Lulus” Int nilai=60 nilai>=65 Ternyata nilai=60 maka pilihan yang ada akan jatuh pada Option 2, tapi option 2 tidak ada outcome Yes Print “Lulus”
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, includingsales 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 if (kondisi pilihan){ //statemen ketika kondisi true } 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’shead 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 getto 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. Repeatingan operation millions of times presents no problem to a computer with an internal clock that ticks billions of times every second
While Ekpresii Boolean (kondisi) False True Statement 1 Statement 2 Statement n Statement after 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 21 0 5 12 0 3 0 2 1 3 3 3 3 size size size size size sum sum sum sum sum count count count count count