Penjelasan dan Contoh dari Operator Penaikan dan Penurunan

  • Post
    codeorayo
    Keymaster
    none
    [img width=605,height=455]https://www.belajarcpp.com/wp-content/uploads/2018/01/4.-Post-Image-2.png[/img]Increment And Decrement Operator atau dalam bahasa Indonesia adalah “Operator penaikan dan penurunan”. Dalam operator ini terdapat dua bentuk operator yaitu penaikan (

    Code:

    ++

    ) dan penurunan (

    Code:

    ).Tabel Operator penaikan dan penurunanOperatorKeteranganPenjabaranOperator penaikan (

    Code:

    ++

    ) berfungsi untuk menambahkan 

    Code:

    1

     nilai kepada nilai tujuan dan Operator penurunan (

    Code:

    ) berfungsi untuk mengurangi 

    Code:

    1

     nilai pada nilai tujuan.Contoh :

    [table]

    [tr]

    [/tr]

    [/table]

    [td]12[/td][td]

    Code:

    int

    Code:

    x = 1 ;

    Code:

    x++;

    [/td]

    Contoh di atas adalah contoh bagaimana operator penaikan di gunakan, nilai 

    Code:

    x

     yang telah diinisialisasi dengan nilai 

    Code:

    1

     akan ditambahkan dengan nilai 

    Code:

    1

     dan menjadi 

    Code:

    2

     setelah menggunakan operator penaikan, dan juga sebaliknya jika mnggunakan operator penurunan.Cara kerja dari operator penaikan dan penurunan akan berbeda tergantung dari peletakanya, tanda operator penaikan atau penurunan dapat diletakan di depan operand (Prefix) atau di belakang operand (Postfix).

    Code:

    ++X

     (Prefix) adalah Nilai variabel 

    Code:

    X

     dinaikan dahulu sebelum diproses.

    Code:

    X++

     (Postfix) adalah Nilai variabel 

    Code:

    X

     diproses dahulu sebelum dinaikan.Contoh Program :

    [table]

    [tr]

    [/tr]

    [/table]

    [td]
    [/td]
    [td]

    Code:

    #include

    Code:

    using

    Code:

    namespace

    Code:

    std;

     

    Code:

    int

    Code:

    main(){

    Code:

       

    Code:

    int

    Code:

    x=5;

    Code:

       

    Code:

    cout<<

    Code:

    “Penaikan ++x”

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “++x = “

    Code:

    <<++x<

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

    Code:

       

    Code:

    x=5;

    Code:

       

    Code:

    cout<<

    Code:

    “Penaikan x++”

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x++ = “

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

     

    Code:

       

    Code:

    x=5;

    Code:

       

    Code:

    cout<<

    Code:

    “Pengurangan –x”

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “–x = “

    Code:

    <<--x<

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

    Code:

       

    Code:

    x=5;

    Code:

       

    Code:

    cout<<

    Code:

    “Pengurangan x–“

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x– = “

    Code:

    <

    Code:

       

    Code:

    cout<<

    Code:

    “x = “

    Code:

    <

     

    Code:

       

    Code:

    return

    Code:

    0;

    Code:

    }

    [/td]

  • You must be logged in to reply to this topic.