Tuesday, October 30, 2018

Dasar-Dasar Pemrograman : Percabangan I ( Branching ) Dalam Java

Dasar-Dasar Pemrograman : Percabangan I ( Branching ) dalam Java



Intro



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 kegiatan 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 kegiatan 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 Operator

Logical operator takes logical expressions as operands. Logical operator in Java : && (AND), || (OR) & ! (NOT)



Example in Codepad


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 kegiatan 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 : 





If Statement example



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

Syntax of if statement using block statements :


Can you determine the differences between these codes ?






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.

Syntax : 



  • if Expression1 is TRUE then the computer will execute Expression2, 
  • but if Expression1 is FALSE then the computer will execute Expression3


Conditional operator example



Exercise




Buatlah kegiatan untuk menuntaskan 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 kegiatan that asks the cashier for the original price and then prints the discounted price.

2.Memeriksa apakah sebuah abjad kecil yaitu abjad vokal/konsonan! True kalau vokal, false kalau konsonan.

3.Membandingkan dua buah lingkaran. True kalau bulat 1 lebih besar dari bulat 2, false kalau tidak.

4.Memeriksa apakah sebuah segitiga merupakan segitiga siku-siku atau bukan. Input akan mendapatkan 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 kalau bukan.
5.Diberikan posisi dari dua buah ratu pada papan catur, dalam nomor baris dan nomor kolom. Buatlah kegiatan untuk menyidik apakah kedua buah ratu sanggup saling menyerang atau tidak. Program akan mendapatkan 4 buah input, yaitu noBaris1 dan noCol1 yang merupakan posisi dari Ratu 1, serta noBaris2 dan noCol2 yang merupakan posisi dari Ratu 2. 



Sumber http://wikiwoh.blogspot.com

No comments:

Post a Comment

Laptop Graphic Terbaik Untuk Desain Grafis 2014

Mereview Laptop Desain Grafis tahun 2014 OPOSIP - Ketika saya bekerja dari rumah saya mempunyai sebuah PC yang didedikasikan yang sang...