Sabtu, 25 Februari 2017

Operator dan Ungkapan (Part 2)

Operator dan Ungkapan (Part 2)
Operator Majemuk

C++ menyediakan operator yang dimaksudkan untuk memendekkan operasi penugasan semacam :

                  x = x + 2;
                  y = y * 4;
Menjadi :

                  x += 2;
                  y *= 4;




Contoh Program

//program16.cpp
#include <iostream.h>
#include <conio.h>

void main()
{
 int x = 2;
    cout<<"x = "<<x<<endl;
    x += 3;
    cout<<"x = "<<x<<endl;
 getch();
}

Ungkapan Kondisi
Ungkapan kondisi adalah ungkapan yang menjadi dasar bagi pernyataan berkondisi (misalnya if).
Ungkapan ini memberikan nilai benar atau salah.
Hasil ungkapan berupa :

          - 0 kalau ungkapan bernilai salah
          - 1 kalau ungkapan bernilai benar
Adapun elemen yang dapat membentuk ungkapan ini adalah :

          - Operator Relasi
          - Operator Logika

Operator Relasi
Operator relasi biasa digunakan untuk membandingkan dua buah nilai.



Contoh Program

//program17.cpp

#include <iostream.h>
#include <conio.h>

void main()
{
 int nilai;
 nilai = 3 > 2;
    cout<<"Nilai = "<<nilai<<endl;
    nilai = 2 > 3;
    cout<<"Nilai = "<<nilai<<endl;
 getch();
}

Operator Logika
Operator logika biasa digunakan untuk menghubungkan dua buah ungkapan kondisi menjadi sebuah ungkapan kondisi. Operator-operator ini berupa :

          &&  -> operator logika dan / AND
          ||   -> operator logika atau / OR
          !  -> operator logika bukan / NOT

Contoh Program

//program18.cpp
#include <iostream.h>
#include <conio.h>

void main()
{
 int x = 200;
    int nilai;
    nilai = (x > 1) && (x <= 50);
    cout<<"Nilai = "<<nilai<<endl;
    nilai = (x > 1) || (x <= 50);
    cout<<"Nilai = "<<nilai<<endl;
 getch();
}

Operator Kondisi
Operator kondisi biasa dipakai untuk mendapatkan sebuah nilai dari dua buah kemungkinan, berdasarkan suatu kondisi.
Ada tiga ungkapan yang dilibatkan.
Oleh karena itu operator ?: tergolong operator ternary.
Format pemakaiannya :

           ungkapan1?ungkapan2:ungkapan3
Hasil dari ungkapan tersebut adalah :

          - Kalau ungkapan1 bernilai benar (1) maka hasilnya adalah ungkapan2.
          - Kalau ungkapan1 bernilai salah (0) maka hasilnya adalah ungkapan3.

Contoh Program

//program19.cpp

#include <iostream.h>
#include <conio.h>

void main()
{
   int bil1, bil2, minim;
   bil1 = 53;
   bil2 = 6;
   minim = bil1 < bil2 ? bil1 : bil2;
   cout<<"Bilangan terkecil = "<<minim;
   getch();
}

1 komentar:

  1. I have to say it was an interesting post to read. I learned new information from your article, you are doing a great job. Let’s continue. By the way, we would like to let you know about our company: Premium Travel Vietnam (PTV) is a professional, leading tour operator based in Da Nang city – central Vietnam. Our office is at 21 Che Lan Vien Str, Ngu Hanh Son Dist, Da Nang city. We have been in the tourism industry for many years and with a comprehensive experience in this field, we are proud to be one of the best travel agents in Da Nang in particular and in Vietnam in general. Everyday,we provide tourists with all tour packages, holiday deals, vacations, private trips to all sightseeings, historical sites in Vietnam such as Ha Long bay, Thang Long Citadel, Ho Dynasty Citadel, Trang An Landscape Complex, Hoi An ancient town, My Son Holyland, Hue ancient capital, Phong Nha National Park. Besides, Vietnam is not only well known on the world map but the people are so friendly, warm hearted and welcoming. Today, there are more and more people coming to visit Vietnam by airplane but also by cruise ship. In order to meet a rising demand of customers coming to sightseeing Vietnam by cruise ship, we, PTV, therefore organise Vietnam Shore Excursion. Joining in this tour, PTV helps you explore all beautiful cities in Vietnam: from vibrant, dynamic city of Ho Chi Minh to a coastal, white sand beaches of Nha Trang or worth living city of Da Nang or Hue ancient capital with all relics and splendor left by Nguyen Dynasty or the charming, serene, beautiful city of Hoi An. All will give you unforgettable memories in one day exploring cities from seaport. By the way, you will have an insight into local culture, knowing customs, culture, religions, way of life of Vietnamese people. Furthermore, you can taste some of local food to know more Vietnamese food. It will be an outstanding trip from seaport. Let’s book this tour and we will make your trip a special one. Besides, our company also arranges package tours, private tours or day tours to Phong Nha National Park. A new tourist destination of central Vietnam in Quang Binh province. So, let’s come and explore amazing, fantastic caves and impressive nature as well as spectacular stalactities, stalagmites at Phong Nha Park with Phong Nha Pioneer Travel. For more information, details and assistance, kindly contact us at premiumvietnamtravel@gmail.com or whatsapp us at 0084979613777. We are very pleased to serve you and be a reliable partner to you.



    BalasHapus

luvne.com ayeey.com cicicookies.com mbepp.com kumpulanrumusnya.com.com tipscantiknya.com