Kumpulan Program Sederhana

Jumat, 22 April 2011

JENIS-JENIS OPERATOPR DALAM PEMOGRAMAN C++

OPERATOR

In the language of C + + a lot of offers to perform operator functions solving certain problems in the program.
Opertor-operators will be grouped into 4 parts, namely assignment, unary, binary, and ternary. as an introduction
for you, before entering into the discussion further, I would like to introduce the terms that must be known in the work with the operator. Examples are as follows:


P=30 / 6

P               Variables defined as
 =              defined as the Operator Assignment
 5 dan 6     defined as an Operand
 30 / 6       defined as the expression
 /               Arithmetic operators are defined as (Distribution)
 P=30 / 6   defined as Arithmetic Statement

1. Assignment Operators

Assignment operator is an operator that serves to insert (assign) values ​​to dalma a variable or constant.
This operator is represented by an equal sign (=). Example program using the assignment operator.


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

int main() {
/ / declaring constants
/ / by doing an assignment with a value of 3.14

const PI=3.14
/ / declare variables
char  MyChar;        / / declare a variable of type char
char* MyString;        / / declare a variable of type char *
int MyInterger;        / / declare a variable of type int
double My Double;   / / declare a variable of type double

/ / perform assignment to the variables above
MyChar = 'F';  
MyString="Mohammad Fajar 10018040";
MyInterger=05;
MyDouble=05.12.1992;
/ / display the values ​​of the variables above
cout<<"MyChar:"<<MyChar<<endl;
cout<<"MyString:"<<MyString<<endl;
cout<<"MyInterger:"<<MyInterger<<endl;
cout<<"MyDouble:"<<MyDouble<<endl;


return 0;
getch ();

 
view results

MyChar:F 
MyString:Mohammad Fajar 10018040
MyInterger:05
MyDouble:
05.12.1992

2. Unary Operators

In mathematics called unary operator is an operator which involves only an Operand.
Included in Unary Operators are as follows:


Operator|    Type of Operation    | examples
   +        | make a positive value   | +4
   -         |
create a negative value | -4
   --       | decrement                    | i--
   ++      | increment                     | i++


a. example programs with (+)(-)

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

int main(){

int A;
float B;

A = +8;   
/ / can be written with A8  
               / / which means entering a positive value 8

B = -5.12; 
/ / insert a negative value 5.12

/ / display the value stored in variable A and B
cout<<"nilai A :"<<A<<endl;
cout<<"nilai B :"<<B<<endl;

A = -A;  
/ / change the value of A becomes negative
B = -B;   / / change the value of B becomes negative

/ / display the return value stored
/ / in the variables A and B

cout<<"nilai A :"<<A<<endl;
cout<<"nilai B :"<<B<<endl;

getch ();
return 0;
}


view result


nilai A : 8
nilai B :-5.12


nilai A :-8
nilai B :5.12

b. Increment
increment is a value addition that occurs in a variable. As for operators who used to do is increment operator + +. This operator will add the value of a variable with a value of 1. there are 2 types of increments contained in c + +: 1. pre-increment of adding value before a variable is processed, and 2. post-increment that is the opposite of the pre-increment of the process first before doing penmbahan values​​, a common form of pre-increment and post-increment, namely:


/ / perform pre-increment
++nama_variabel;
/ / perform pre-increment
nama_variabel++;

example program increments 

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

int main(){

int A;       
/ / declare variables A

/ / fill values ​​into the variable A with values ​​11
A = 11;
/ / perform pre-increment
cout<<"nilai A awal    :"<<A<<endl;
cout<<"nilai ++A    :"<<++A<<endl;
cout<<"nilai A akhir    :"<<A<<endl;
/ / change the value contained in variable A
/ / with the value 21

A = 21;
/ / perform post-increment
cout<<"nilai A awal    :"<<A<<endl;
cout<<"nilai A++    :"<<A++<<endl;
cout<<"nilai A akhir    :"<<A<<endl;

return 0;
getch ();
}



view result 

nilai A awal   : 11
nilai ++A      : 12
nilai A akhir  : 12

nilai A awal  : 21
nilai A++     : 21
nilai A akhir : 22


C. Decrement

Decrement is the opposite of the increment, namely reducing the value of a variable. as well as the increment,
Decrement was also divided into 2 types of pre-decrement and post-decrement. sample program decrements




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

int main (){

int B      
/ / declare variables B

//
fill values ​​into the variable B with the value 2

B = 2;
/ / perform pre-decrement
cout<<"nilai B awal    :"<<B<<endl;
cout<<"nilai --B    :"<<--B<<endl;
cout<<"nilai B akhir    :"<<B<<endl;
/ / change the value contained in variable B
/ / with a value of 4

B = 4;

/ / perform post-decrement
cout<<"nilai B awal    :"<<B<<endl;
cout<<"nilai B--    :"<<B--<<endl;
cout<<"nilai B akhir    :"<<B<<endl;

return 0;
getch ();
}



view result
nilai B awal  : 2 
nilai --B       : 1
nilai B akhir  : 1

nilai B awal :  4
nilai B--      :  4
nilai B akhir : 3


3. Binary Operators

a. Arithmetic

Arithmetic operator is an operator that is used to perform arithmetic operations like addition, subtraction, multiplication, division and others. As for which include arithmetic operators available in C + + is
as follows:


Operators | Types of Operation | Sample
         +     | Addition                 | 2 + 4 = 6
         -      | Disposals               | 4-2 = 2
         *     | Multiplication          | 4 * 2 = 8
         /      | Distribution             | 4 / 2 = 2
       %      | Modulus                 | 10% 3 = 1
 

examples of programs that use Operator (+)

#Include <iostream>
#Include <conio.h>

int main () {
/ / declare variables a and b (filled with values ​​up to you)
int a=5, b=5;
/ / declares a variable c as a container for the final result
int c;
/ / perform the addition operation
c = a + b;
/ / displays the sum of
cout<<a<<" + "<<b<<" = "<<c;

return 0;
getch ();
}



view result


5 + 5 = 10

examples of programs that use Operator (-)

#Include <iostream>
#Include <conio.h>

int main () {
/ / declare variables a and b (filled with values ​​up to you)
int a=5, b=5;
/ / declares a variable c as a container for the final result
int c;
/ / perform the subtraction operation
c = a - b;

/ / display the results of reduction
cout<<a<<" - "<<b<<" = "<<c;

return 0;
getch ();
 

view result


5 - 5 = 0


examples of programs that use Operator (*)

# Include <iostream>
# Include <conio.h>

int main () {

/ / declare variables a and b (filled with values ​​up to you)
int a = 5, b = 5;

/ / declares a variable c as a container for the final result
int c;

/ / perform multiplication operations
c = a * b;

/ / display the result of multiplication
court <<a <<"*" <<b <<"=" <<c;

return 0;
getch ();
}



view result


5 * 5 =25

examples of programs that use Operator (%)

# Include <iostream>
# Include <conio.h>

int main () {

/ / declare variables a and b (filled with values ​​up to you)
int a = 10, b = 3;

/ / declares a variable c as a container for the final result
int c;

/ / perform modulus operation
c = a% b;

/ / display the results of the modulus (remainder of)
court <<a <<"%" <<b <<"=" <<c;

return 0;
getch ();
}




10 % 3 =1


examples of programs that use Operator (/)

# Include <iostream>
# Include <conio.h>

int main () {

/ / declare variables a, b, c of type int
int a = 15, b = 3;
int c;

/ / declare variables a, b, c of type float
int i = 15.0, j = 4.0;
int k;

/ / perform operations on integer division
c = a / b;

/ / perform operations on decimal division
c = i / j;

/ / display the results of the division on integers and decimals
court <<a <<"/" <<b <<"=" <<c;
court <<i <<"/" <<j <<"=" <<k;

return 0;
getch ();
}


view result

15 / 3 =5
15.0 / 4.0 = 3.75

b. Logic Operators

Operators Logical operators are used to perform the operation in which the values ​​resulting from the operation a value of true or false. This value is called a Boolean value. Boolean itself invented by British mathematician named George Bool. In the language of c + + value true is represented with a value of 1 and one 0.yang included in the Operator The logic is as follows:


| Operators | Types of Operation | Sample
|    & &      |            AND           |  1 & & 1 = 1
|      | |        |             OR             |  1 | | 0 = 1
|       !        |            NOT           |   ! 1 = 0
 

1. Operators & & (AND)
    AND operator will only generate a value of 1 if all its operands is true, but if not then the operation
    would be worth 0.


  | X | Y | X&&Y |
  | 1 | 1    |   1  |
  | 1 | 0    |   0  |
  | 0 | 1    |   0  |
  | 0 | 0    |   0  |



example program:
   #include <iostream>
   #include <conio.h>
   int main () {
   cout <<" 1 && 1 ="<<(1 && 1)<<endl;
   cout <<" 1 && 0 ="<<(1 && 0)<<endl;
   cout <<" 0 && 1 ="<<(0 && 1)<<endl;
   cout <<" 0 && 0 ="<<(0 && 0)<<endl;
   return 0;
   getch ();
   }


view result

1 && 1 = 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0  




2. The operator | | (Or)
    Operation Or will only mengahsilkan value 0 if all its operands is false, but if not, then
    operation will be worth 1.


| X | Y | X||Y |
| 1 | 1    |   1  | 
| 1 | 0    |   1  |
| 0 | 1    |   0  |
| 0 | 0    |   1  |


    example program:
   #include <iostream>
   #include <conio.h>
   int main () {
   cout <<" 1 || 1 ="<<(1 || 1)<<endl;
   cout <<" 1 || 0 ="<<(1 || 0)<<endl;
   cout <<" 0 || 1 ="<<(0 || 1)<<endl;
   cout <<" 0 || 0 ="<<(0 || 0)<<endl;
   return 0;
   getch ();
   }


view result

1 || 1 = 1
1 || 0 = 0
0 || 1 = 0
0 || 0 = 0 


3. Operator! (Not)
    Operation notes is the opposite of the value contained in it. if the initial value is 1, then after the operation notes hence its value to 0, and vice versa.



    | X | !x |
    | 0 |  1 | 
    | 1 |  0 |   



  
    example program:
   #include <iostream>
   #include <conio.h>
   int main () {
   cout <<" ! 1 ="<<(! 1)<<endl;
   cout <<" ! 0 ="<<(! 0)<<endl;
   return 0;
   getch ();
   }



view result


!1 = 0
!0 = 1


4. Ternary Operator
    Ternary operators are operators that are used in operations involving the 3 pieces of operands. As for the operator to use to express it is the operator?: The concept underlying this operation is a branching which is based on certain conditions. The condition is generally as below,

Ekspresi1? Ekspresi2: Ekspresi3;

    if Ekspresi1 is true, then the program will execute ekspresi2. Whereas if ekspresi1 is false ekspresi3 then executed. examples like this program


   #include <iostream>
   #include <conio.h>
  
   int main () {
   
 
/ / ask user to input a value from the keyboard
   cout<<"masukan nilai a :";
   cin>>a;
   cout<<'\n';

   a= (a<0) ? -a: a;
/ / to check the value of a

 
/ / display the value of a after the checking process
   cout<<"|a|="<<a;

   return 0;
   getch ();
   }

view result
  
    masukan nilai a : -10
    |a|=10






 











 

 






 

Tidak ada komentar:

Posting Komentar