Understanding C++ Operators (UCO)

Operators are symbols that are usually involved in creating programs to perform an operation or manipulation. In C++, there are operators that are classified as binary operators (operators that are applied to two values ​​(operands), and there are operators that are classified as unary operators (operators that are applied to one value (operand). Example:

Binary >> a + b
Unary >> - c

1. Arithmetic Operators

Is an operator used to perform mathematical calculations. In the previous material we have learned the definition of Variables, so now let's assume that the variables a = 30 and b = 10, then the results of the implementation of Arithmetic Operators are as follows:

| Operator | Keterangan     | Contoh             |
|----------|----------------|--------------------|
| +        | Penjumlahan    | a + b hasilnya 40  |
| -        | Pengurangan    | ab hasilnya 20  |
| *        | Perkalian      | a * b hasilnya 300 |
| /        | Pembagian      | a / b hasilnya 3   |
| %        | Sisa Pembagian | a % b hasilnya 0   |

Example program:

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

/**
*bundet.com
*penggunaan operator aritmatika
*/

void main() {
clrscr();
int a = 30;
int b = 10;
cout << "a + b = ";
cout << a + b << endl;
cout << "a - b = ";
cout << a - b;
cout << "a x b = ";
out << a * b;
cout << "a / b = ";
cout << a / b;
cout << "a % b = ";
cout << a % b;
getch();
}

The next example is the use of operators to obtain the discriminant value of the following equation: d = b2 -- 4ac. So, to implement the example above into C++ programming is:

d = b * b - 4 * a * c

Example program:

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

/**
*bundet.com
*Menyelesaiakan persamaan diskriminan
*/

void main() {
clrscr();
int a, b, c, d; a = 5;
b = 600;
c = 5;
d = b * b - 4 * a * c;
cout << " d = " << d << '\n';
getch();
}

Execution result:

d = 32220

In C++ arithmetic operators have the highest or lowest priority, this is determined by how they are written or placed.

1. The priority position in writing is listed as in the table.

| Operator                                        | Prioritas |
|-------------------------------------------------|-----------|
| ++ -- (khusus yang berkedudukan sebagai awalan) | Tertinggi |
| -  (Unary Minus)                                |           |
| * / %                                           |           |
| + -                                             | Terendah  |

2. The highest priority position in terms of placement is determined by the LEFTmost position of the program line.

So if operators have the same priority position (in writing) then their main priority can be determined again in terms of placement.

For example, consider the following equation:

x = ( 2 + 3) * 2

In ordinary mathematical logic, we can determine the value of x, which is 10, because 2 + 3 is done first and the result is then multiplied by 2, but you all need to know that this is very different if we implement it in C++ programming, for more details, see the example program below:

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

/**
*bundet.com
*Penggunaan kurung() untuk mengatur prioritas
*Pembuktian prioritas suatu operator
*/

void main() {
clrscr()
int x ;
x = 2 + 3 * 2 ;
cout << " x = " << x << '\n';
x = (2 + 3) * 2 ;
cout << " x = " << x << '\n';
getch();
}

Execution result:

8
12

2. Increment and Decrement Operators

The increment operator is an operator used to increase the value of a variable by 1. The decrement operator is an operator used to decrease the value of a variable by 1.

Example:

x = x + 1 ;
y = y - 1 ;

Can be written as

++ x ; -- y ;

or:

x ++ ; y -- ;

3. Determining the Priority of Increment and Decrement Operators

Now we will try to determine the priority of the increment and decrement operators, then what is the effect of the results of this experiment? For more details, please see the example program below:

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

/**
*bundet.com
*Penggunaan operator penaikan di belakang variabel
*/

void main() {
int r = 10;
int s; s = 10 + r++ ;
cout << "r = " << r << '\n' ;
cout << "s = " << s << '\n' ;
getch();
}

Execution result:

r = 11
s = 20

Explanation:

In the example above, s is filled with the sum of the values ​​10 and r. Thus, s will have a value of 20. After s is filled with 20, the value of r is increased because the operator ++ is written behind r. It is called post-increment, which means it is increased behind after the summation between r and 10 is carried out. Another example:

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

/**
*bundet.com
*Penggunaan operator penaikan di depan variabel
*/

void main() {
int r = 10;
int s;
s = 10 + ++r ;
cout << "r = " << r << '\n' ;
cout << "s = " << s << '\n' ;
getch();
}

Execution result:

r = 11
s = 20

Explanation:

In this example, the value of r is initially increased because the operator ++ is placed in front of r. It is called pre-increment and then its value is added to 10 and given to s. Thus s has a value of 21 and r is equal to 11.

4. Assignment Operators

Assignment Operator is an operator used to assign values ​​to certain variables. Assume variable a has a value of 50 and variable b has a value of 30, then observe the following table:

| Operator | Keterangan                                                                                | Contoh                                                                    |
|----------|-------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| =        | Memberikan nilai dari operand sisi kanan untuk sisi kiri                                  | c = a + b hasilnya c diberi nilai 80 c = a = b Hasilnya c,a,b bernilai 30 |
| +=       | Menambahkan operand kiri dengan operand kanan dan menugaskan hasilnya untuk operand kiri  | c += a sama dengan c = c + a                                              |
| -=       | Mengurangi operand kanan dari operand kiri dan menugaskan hasilnya untuk operand kiri     | c -= a sama dengan c = c - a                                              |
| *=       | Mengalikan operand kanan dengan operand kiri dan menugaskan hasilnya untuk operand kiri   | c *= a sama dengan c = c * a                                              |
| /=       | Membagi operand kiri dengan operand kanan dan menugaskan hasil untuk operand kiri         | c /= a sama dengan c = c / a                                              |
| %=       | Menghitung sisa pembagian menggunakan dua operand dan memberikan hasilnya ke operand kiri | c %= a sama dengan c = c %  a                                             |

Example program:

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

/**
*bundet.com
*Penggunaan operator penugasan
*/

void main() {
int a = 50;
int b = 30;
int c = a + b;
cout << "Nilai c adalah : " << c; c = a = b;
cout << "Nilai c sekarang : " << c;
cout << "Nilai a sekarang : " << a;
c += a;
cout << "Nilai c sekarang : " << c;
c *= a;
cout << "Nilai c sekarang : " << c;
c -= a;
cout << "Nilai c sekarang : " << c;
c /= a;
cout << "Nilai c sekarang : " << c;
c %= a;
cout << "Nilai c sekarang : " << c;
getch();
}

5. Condition Operator

The Conditional Operator is a simplification of the if..else form where each block of if and else only consists of one statement/command.

General form:

(ekspresi)? (jika benar) : (jika salah);

Example Program:

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

/**
*bundet.com
*Penggunaan operator kondisi untuk memperoleh
*bilangan terkecil diantara dua buah bilangan
*/

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

Execution Results:

Bilangan terkecil = 6

6. Relational / Comparison Operators

Relational Operators are used to test the relationship between values ​​and/or variables. These operators are used in a conditional statement that always results in a true or false value.

The types of relational operators can be seen in the following table:

| Operator | Keterangan                    |
|----------|-------------------------------|
| ==       | Sama dengan (bukan penugasan) |
| !=       | Tidak sama dengan             |
| >        | Lebih dari                    |
| <        | Kurang dari                   |
| >=       | Lebih dari atau sama dengan   |
| <=       | Kurang dari atau sama dengan  |

Example program:

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

/**
*bundet.com
*Penggunaan operator relasi
*/

void main() {
int nilai;
nilai = 3 > 2 ; // hasil ungkapan : benar
cout << "Nilai = " << nilai << endl;
nilai = 2 > 3 ; // hasil ungkapan : salah
cout << "Nilai = " << nilai << endl;
getch();
}

Execution result:

Nilai = 1
Nilai = 0

7. Logical Operators

Logical operators are used to compare two or more variable values. The result of this operation is a boolean value of true or false. Assume variable a is true, b is false and c is true, then observe the following table:

| Operator | Keterangan                                                                                                                  | Contoh                                                       |
|----------|-----------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------|
| &&       | Jika semua operand bernilai benar (TRUE) maka kondisi bernilai benar.                                                       | a && b hasilnya false a && c hasilnya true                   |
| \|\|     | Jika salah satu dari operand bernilai benar (TRUE) maka kondisi bernilai benar.                                             | a \|\| b hasilnya true a \|\| c hasilnya true                |
| !        | Digunakan untuk membalik kondisi. Jika kondisi benar (TRUE) maka akan berubah menjadi salah (FALSE), begitu pula sebaliknya | !a hasilnya false !b hasilnya true !( b && a ) hasilnya true |

Example Program:

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

/**
*bundet.com
*Penggunaan operator logika
*/

void main() {
bool a = true;
bool b = false;
bool c = true;
cout<< "(a && b) : " << (a && b);
cout<< "\n (a && c) : " << (a && c);
cout<< "\n (a && b && c) : " << (a && b && c);
cout<< "\n (a || b) : " << (a || b);
cout<< "\n (a || c) : " << (a || c);
cout<< "\n (a || b || c) : " << (a || b || c);
cout<< "\n !a : " << !a;
cout<< "\n !b : " << !b;
cout<< "\n !(b && a) : " << !(b && a);
getch();
}

Execution result:

(a && b) : 0
(a && c) : 1
(a && b && c) : 0
(a || b) : 1
(a || c) : 1
(a || b || c) : 1
!a : 0
!b : 1
!(b && a) : 1

Hope this is useful & happy learning!

Source:

© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs,

Comment 1

Hi, Mr. Wawan, I really want to ask something again, hehe.  😃 I have a program with a log-in feature, Mr.

#include
#include
#include
using namespace std;
void main()
{
string username = "";
string password = "";
bool loginsuccess = false;

cout<<"==============================================================="<>username;
cout<<"password :"; cin>>password;
cout<

Well, I've been fiddling around here and there but it still doesn't work, bro. I want the password to be * * * * when I enter it in the program. How do I do that, bro? Thanks,  🙂 I hope it helps.

No sir, if I want to program it to appear * * * * like that when I enter a password, how do I do it? Because the program can do it, but when I run it, when I enter a password, numbers still appear. So I want the program to appear * * * * like that when I run it, sir. Thanks

HAYATUN NISA Mar 28, 2017, 22:13:00 clrscr(); if I may ask, what is the use of this, sis? hehe

Response 1

I suggest you to learn the basics of using loops first, here is the link:  GETTING TO KNOW REPETITION IN C++

Maybe the program you mean is something like this:  Hide String Like Password - C++

Hi HAYATUN NISA, that is a function/method in the conio.h library, its use is to clear the screen display.

C++ Basic Output Operations

The standard output commands provided by Borland C++ include: 

  1. cout()
  2. printf() 
  3. puts()
  4. putchar()

1. cout() function

The cout() function is an object in C++ that is used to display information to the screen. To use the cout() function requires a header file.

Example Program:

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

/**
*bundet.com
*Perintah keluaran dengan fungsi cout()
*/

void main() {
cout << "STMIK El Rahma \n Yogyakarta";
cout << "Qur'ani, lulus jadi jutawan" << endl;
cout << "Inssyaallah";
getch();
}

2. printf() function

To use this function, the stdio.h header file is required. The general form of writing this function is as follows:

printf("penentu-format", argumen1, argument-n);

To determine the format, please see the following table:

| Tipe Data                                                                     | Penentu Format |
|-------------------------------------------------------------------------------|----------------|
| Integer                                                                       | %d             |
| Floating point Bentuk desimal Bentuk berpangkat Bentuk desimal dan berpangkat |   %f %e %g     |
| Double precision                                                              | %lf            |
| Character                                                                     | %c             |
| String                                                                        | %s             |
| Unsigned integer                                                              | %u             |
| Long integer                                                                  | %ld            |
| Long unsigned integer                                                         | %lu            |
| Unsigned hexadesimal integer                                                  | %x             |
| Unsigned octal integer                                                        | %o             |

Example Program:

#include <stdio.h>
#include <conio.h>

/**
*bundet.com
*Perintah keluaran dengan fungsi printf()
*/

void main() {
int a = 10;
char b = 'J';
printf("%c merupakan Abjad yang ke - %d", b, a);
getch();
}

3. puts() function

The puts() function is used specifically to print a string to the screen. Puts() comes from the word PUT STRING. The difference between the printf() and puts() functions can be seen in the table below:

| printf()                                                | puts()                                                                                   |
|---------------------------------------------------------|------------------------------------------------------------------------------------------|
| Harus menentukan tipe data untuk data string yaitu %s   | Tidak perlu penentu tipe data string, karena fungsi ini khusus untuk tipe data string    |
| Untuk mencetak pindah baris (end line) perlu notasi  \n | Untuk mencetak pindah baris tidak perlu notasi \n karena sudah diberikan secara otomatis |

Example Program:

#include <stdio.h>
#include <conio.h>

/**
*bundet.com
*Perintah keluaran dengan fungsi puts()
*/

void main() {
char kampusQ[30] = "STMIK El Rahma";
puts("Saya kuliah di");
puts(kampusQ);
getch();
}

Hope this is useful & happy learning!

Source:

© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs,

Basic Input Operations in C++

The standard input commands provided by Borland C++ include the following:

  1. cin()
  2. scanf() 
  3. gets() 
  4. getch() 
  5. getche()

1. cin() function

The cin() function is an object in C++ that is used to input data. To use the cin() function, you must include the iostream.h header file. The general form of the cin() function is as follows:

cin >> nama_variabel;

Example Program:

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

/**
*bundet.com
*Perintah masukan dengan fungsi cin()
*/

void main() {
float alas, tinggi;
cout << "Masukkan nilai Alas : ";
cin >> alas; cout << "Masukkan nilai Tinggi : ";
cin >> tinggi;
cout << "Luas segi tiga adalah : " << 0.5 * alas * tinggi;
getch();
}

2. scanf() function

To use the scanf() function, you must include the stdio.h header file. The general form of the scanf() function is as follows:

scanf("penentu format", nama_variabel)

Information:

  • The & symbol is a pointer used to point to the memory address of the target variable.
  • Format determinants can be seen in the following table:
| Tipe Data                                    | Penentu Format          |
|----------------------------------------------|-------------------------|
| Integer                                      | %d                      |
| Float Point Bentuk desimal Bentuk berpangkat |   %e atau %f %e atau %f |
| Double precision                             | %lf                     |
| Character                                    | %c                      |
| String                                       | %s                      |
| Unsigned integer                             | %u                      |
| Long integer                                 | %ld                     |
| Long unsigned integer                        | %lu                     |
| Unsigned hexadesimal integer                 | %x                      |
| Unsigned octal integer                       | %o                      |

Example Program:

#include <stdio.h>
#include <conio.h>

/**
*bundet.com
*Perintah masukan dengan fungsi scanf()
*/

void main() {
char nama[30];
int nilai;
printf("Masukkan Nama : ");
scanf("%s", &nama);
printf("Masukkan Nilai : ");
scanf("%d", &nilai);
printf("Mahasiswa dengan nama %s nilainya %d", nama, nilai);
getch();
}

3. gets() function

The gets() function is used specifically for string data input. To use this function must include the stdio.h header file. The general form of this function is:

gets(nama_variabel_string)

The difference between the scanf() and gets() functions can be seen in the following table:

| scanf()                                                                                       | gets()                                                                                                     |
|-----------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| Tidak dapat menerima string yang mangandung spasi atau tab dan dianggap sebagai data terpisah | Dapat menerima string yang mengandung spasi atau tab dan masing-masing dianggap sebagai satu kesatuan data |

4. getch() command

The getch() function is used to read a character with the nature of the character entered does not need to be ended by pressing the Enter key, and the entered character will not be displayed on the screen. The header file included to use this function is conio.h.

Example Program:

#include <stdio.h>
#include <conio.h>

/**
*bundet.com
*Perintah masukan dengan fungsi getch()
*/

void main() {
printf("Ketik sembarang karakter ! ");
char kar = getch();
print("\nTaddi Anda memasukakan karakter %c", kar);
getch();
}

5. getche() command

The getche() function is used to read a character with the nature of the character entered does not need to be ended by pressing the Enter key, and the entered character is displayed on the screen. The header file included to use this function is conio.h.

Example Program:

#include <stdio.h>
#include <conio.h>

/**
*bundet.com
*Perintah inputan dengan fungsi getche()
*/

void main() {
printf("Ketik sembarang karakter ! ");
char kar = getche();
print("\nTaddi Anda memasukakan karakter %c", kar);
getch();
}

Hope this is useful & happy learning!

Source:

© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs,


Post a Comment

Previous Next

نموذج الاتصال