Dasar-Dasar Pemrograman : Percabangan I ( Branching ) dalam Java
One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks depending on how the switches are set, a program can take different actions depending on inputs and other circumstances.
Example :
The university bookstore has a Kilobyte Day sale every October 24, giving an 8 percent discount on all computer accessory purchases if the price is less than $128, and a 16 percent discount if the price is at least $128. Write a program that asks the cashier for the original price and then prints the discounted price.
Solution :
Check whether a condition is true or false (using relational & logical expressions) => Is the price less than $128? (the answer is true or false). Decide to choose only an alternative to be done and ignore any other alternative(s): branching.
Relational Operator
In order to make a decision, computer needs to know a condition that makes an alternative is chosen. In many cases, the condition involves comparing values. A relational operator tests the relationship between two values. A relational expressions is an expression using relational operator, the result of this expression is a boolean value.
Example in Codepad
Logical Expressions
Logical/boolean value: TRUE or FALSE
Logical Expression: an arrangement of identifier, literal (TRUE or FALSE), logical operators and/or relational expressions that can be evaluated to compute a logical value
Logical Expression: an arrangement of identifier, literal (TRUE or FALSE), logical operators and/or relational expressions that can be evaluated to compute a logical value
Logical Operator
Logical operator takes logical expressions as operands. Logical operator in Java : && (AND), || (OR) & ! (NOT)
Exercise: what are the values of these expressions ?
Branching
Sequential control structure is a computer executes each statements one after another. Conditional control structure allows computer to choose between (two) alternative executions. Use a condition/boolean expression: a statement (logical expression) that is either true or false. Java uses if, if-else, multiway-if-else and/or switch statements with an assertion to alter the flow of the program and create conditional control structure
If statement
The if statement is used to implement a decision. When a condition is fulfilled, one set of statements is executed.
Syntax :
The length is converted to 0 only if length < 0
Block Statements
Compound/Block Statement: A list of statements enclosed in a pair of braces. Java treats compound statement as a single statement. It is very useful to be used with different control structure
The if...else...Statement
The if-else statement chooses between two alternatives statements
Syntax :
Conditional Operator
Conditional operator: ternary operator or arithmetic if. It takes three operand to run.
- if Expression1 is TRUE then the computer will execute Expression2,
- but if Expression1 is FALSE then the computer will execute Expression3
Exercise
Buatlah program untuk menyelesaikan permasalahan berikut ini !
1. The university bookstore has a Kilobyte Day sale every October 24, giving an 8 percent discount on all computer accessory purchases if the price is less than $128, and a 16 percent discount if the price is at least $128. Write a program that asks the cashier for the original price and then prints the discounted price.
2.Memeriksa apakah sebuah huruf kecil adalah huruf vokal/konsonan! True jika vokal, false jika konsonan.
3.Membandingkan dua buah lingkaran. True jika lingkaran 1 lebih besar dari lingkaran 2, false jika tidak.
4.Memeriksa apakah sebuah segitiga merupakan segitiga siku-siku atau bukan. Input akan menerima tiga buah bilangan yang merupakan sisi segitiga. Pada input, sisi-sisi segitiga sudah terurut menaik. Keluarkan true bila segitiga merupakan segitiga siku-siku, dan false jika bukan.
5.Diberikan posisi dari dua buah ratu pada papan catur, dalam nomor baris dan nomor kolom. Buatlah program untuk memeriksa apakah kedua buah ratu dapat saling menyerang atau tidak. Program akan menerima 4 buah input, yaitu noBaris1 dan noCol1 yang merupakan posisi dari Ratu 1, serta noBaris2 dan noCol2 yang merupakan posisi dari Ratu 2.
•
Posting Komentar