Data Type and Operator
description
Transcript of Data Type and Operator
![Page 1: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/1.jpg)
Data Type and Operator
By: Muhammad Zidny Naf’an
![Page 2: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/2.jpg)
Data Type
![Page 3: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/3.jpg)
Data type and variable declaration:data_type variable_name;
Set a value to variable:variable = value;
We can directly set a value to varible when declaration:
data_type variable_name = value;
Declaration
![Page 4: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/4.jpg)
Modification of Data Type
![Page 5: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/5.jpg)
Operator the sign that use for operating or manipulation
Variable or value that operated called the operand
Operator
3 + 2
operand operandoperator
![Page 6: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/6.jpg)
Arithmetic OperatorOperator Information Example
* multiply 2 * 3/ divide 7 /2
% modulus 5 % 4 = 1+ add 4 + 5- subtraction 5 – 6
![Page 7: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/7.jpg)
Using arithmetic operator
Arithmetic Operator
![Page 8: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/8.jpg)
To give a value to variable Ex:
a = 1;c = 2 + (b = 1);a = b = c = d = e = 1;
Assignment Operator
![Page 9: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/9.jpg)
Symbol of decrement operator: -- Used for subtracting by 1
The symbol of increment operator: ++ used for adding by 1Ex:
x++ like with x = x + 1.y-- like with y = y – 1.
Can be placed front (pre-) or behind (post-) operand.
Increment and Decrement Operator
![Page 10: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/10.jpg)
The effect for placing the operator:a = 7;b = 2;c = b + a++;
value of c is 9 not 10that statements above like with this statement below:
c = b + a;a = a + 1;
Increment and Decrement Operator
![Page 11: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/11.jpg)
If increment operator placed in front of operand, so operand will add by 1 first. Ex:
a = 7;b = 2;c = b + ++a;
that statements above like with this statement below:
a = a + 1;c = b + a;c = 10;
Increment and Decrement Operator
![Page 12: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/12.jpg)
For manipulating bit Bitwise operator only for int or char
Bitwise Operator
Operator Information Example
~ Bitwise NOT ~25
<< Shift bit to left 25 << 2
>> Shift bit to right 25 >> 2
& Bitwise AND 25 & 2
^ Bitwise XOR 25 ^ 2
| Bitwise OR 25 | 2
![Page 13: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/13.jpg)
Example
Bitwise Operator
![Page 14: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/14.jpg)
shorten assignment operator, Example x = x + 2 x += 2
Compound Operator
![Page 15: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/15.jpg)
Compare the relationship between two operands
Relationship Operator
Operator Information== Equal!= Not equal< Less than> More than<= Less than or equal>= More than or equal
![Page 16: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/16.jpg)
Ex
Relationship Operator
![Page 17: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/17.jpg)
Obtain the value of two possibles, ex:statement1 ? statement12: statement3
If statement1 true than the result equal with statement2, if false than statement3.
Condition Operator
![Page 18: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/18.jpg)
Contoh:
Condition Operator
![Page 19: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/19.jpg)
Placing two statement in one rule that need one statement
Example use in looping for: for(i=0, j=0; i<10; i++, j--) { ..}
Comma Operator
![Page 20: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/20.jpg)
Connecting two or more statements to make one condititon statement
Logic Operator
Operator Information Example
&& Logika AND n && m
|| Logika OR n || m
! Logika NOT !n
![Page 21: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/21.jpg)
Contoh:
Logic Operator
![Page 22: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/22.jpg)
1. If x is integer, from these values, where the value can put to x?a) 10b) 5.7c) 40000
2. Change these formulas into C++ language
Exercise
![Page 23: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/23.jpg)
3. A program to calculate the final score of a student has 5 variables for student name, student identification numbers, the value of the task, the mid term and final exam scores of the semester. The final value calculated by the formula: (task * 20%) + (midterm * 40%) + (final exam * 40%). Make the program!
Exercise
![Page 24: Data Type and Operator](https://reader036.fdocuments.net/reader036/viewer/2022062410/56816132550346895dd08871/html5/thumbnails/24.jpg)
Write the program to display the number of days, hours, minutes, and seconds of inputin the form of the length of time in seconds. For example, input 100.000 seconds will produce output 1 day, 3 hours, 46 minutes, 40 seconds.Input of the program is of type long integer.Program output is the number of days, hours, minutes, and seconds from the input.
Exercise