Tuesday, August 28, 2012

Operator Increment dan Decrement

Kode Direktori - Operator Increment dan Decrement

Hallo teman-teman, sekarang saya mau memberikan pembahasan tentang operator dasar aritmatika, Java juga terdiri atas operator unary increment (++) dan operator unary decrement (--). Operator increment dan decrement menambah dan mengurangi nilai yang tersimpan dalam variabel angka terhadap nilai 1.

Ini sebagai contoh nya teman-teman:
count = count + 1; // increment nilai count dengan nilai 1

jadi pernyataanya sama dengan kayak gini
count++; // + itu samadengan 1nya. 

 
Operator increment dan decrement dapat ditempatkan sebelum atau sesudah operand.
Ketika digunakan sebelum operand, akan menyebabkan variabel diincrement atau didecrement dengan nilai 1, dan kemudian nilai baru digunakan dalam pernyataan dimana dia ditambahkan. 
Sebagai contoh seperti ini teman-teman:

int x = 120;
     int y = 80;
     int z = 0;

     z = ++y + x;

Sintak di atas, akan menghasilkan nilai z = 81+120 = 201. "Kenapa jadi 81? Ya, karena ++y nya ditambah '1'. y punya nilai 80 ditambah 1 jadi 81. lalu di tambah nilai x = 120 jadi hasilnya 201."

Ketika operator increment dan decrement ditempatkan setelah operand, nilai variabel yang lama akan digunakan lebih dulu dioperasikan, dan lebih dulu terhadap pernyataan dimana dia ditambahkan.
Sebagai contoh :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package belajar;

/**
 *
 * @author Rosadi
 */
public class OperatorIncrementDecrement {
     /**   
     * @param args the command line arguments   
     */ 
    public static void main(String[] args) {
        int x = 120;
        int y = 190;
        int z = 0;
        
        z = ++ y + x;
        System.out.println("Nilai z = " + z );
    }
    
}
Nah ingat ketika operator increment dan decrement ditempatkan setelah operan, nilainya tidak berubah, Tetap...

Okai mungkin segitu tentang operator Increment dan Decrement yang telah kami bahas. Terimakasih.

5 comments

Anonymous
May 26, 2013 at 5:08 PM

Playing with kids in the neighborhood keeps fresh competition for any gamer which makes
playing black ops wii much more challenging. Both these games are role-playing games; that is, you control a small
handful of characters, manage their statistics and
weapons, character cameos and, of course, dismember each other
in that respect! Mega Man 2 is my favorite sport by a landslide.
On campus and online education.

Take a look at my web-site - xbox kinect bundles

Anonymous
July 14, 2013 at 11:44 AM

Research has shown that women's mate preferences change over the course of the disease, which may in turn cause a host of gorgeous women to glittering parties on both sides of the same activities. The weight loss spell between Black men and weight loss spell are in jeopardy.

My blog post ... entity relationship diagrams **

Anonymous
October 13, 2017 at 12:14 AM

I have been browsing online more than 3 hours today, but I by no means discovered any fascinating article
like yours. It is pretty worth enough for me.

In myy view, if all web owners and bloggers made xcellent colntent material as you probably did, the internet can be much more useful than ever before.

Anonymous
November 17, 2017 at 12:49 PM

Thank you for any other magnificent article. Where else could anyone get that type of information in such a
perfect means of writing? I've a presentation subsequent week,
and I am at the look for such info.

Anonymous
May 4, 2018 at 2:49 AM

Howdy! I know this is somewhat off topic but I wwas wondering whhich blog platform are you using for this website?
I'm getting sick and tired of Wordpress because
I've had issues with hackers and I'm lookinmg at options for another platform.

I would be awesome if you could piint me in the direction of a good platform.

Post a Comment