0% found this document useful (0 votes)
25 views22 pages

C++ 2 Ganjil 2018-2019

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views22 pages

C++ 2 Ganjil 2018-2019

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1

Fundamental Types

• Integral types → exact (values are stored as integers)


– Boolean type: bool
– Enumeration types: enum
– Character types: char
– Integer types: short, int, unsigned short, unsigned int

• Floating point types → approximate


– float
– double

 2003 Prentice Hall, Inc. All rights reserved.


2

Boolean Type

• Variables with 2 values→ false and true


• Values stored as integer 0 and 1
• Named: bool


int main()
{
bool flag = false;
cout<<“flag= “ <<flag<<endl;
flag = true;
cout<<“flag= “ <<flag<<endl;

 2003 Prentice Hall, Inc. All rights reserved.


3

Character Types

• Variables that represent characters


• Characters are delimited by apostrophe (‘)
• Details about characters
– Single characters typically stored in a char data type
• char a 1-byte integer, so chars can be stored as ints
– Can treat character as int or char
• 97 is the numerical representation of lowercase ‘a’ (ASCII)
• Use single quotes to get numerical representation of character
cout << "The character (" << 'a' << ") has the value "
<< static_cast< int > ( 'a' ) << endl;
Prints
The character (a) has the value 97

 2003 Prentice Hall, Inc. All rights reserved.


4

Character Types


int main ()
{
char c=‘A’;
cout<<“c = “<< c << “, int(c) = “ << int (c) <<endl;
char c=‘a’;
cout<<“c = “<< c << “, int(c) = “ << int (c) <<endl;
char c=‘\t’;
cout<<“c = “<< c << “, int(c) = “ << int (c) <<endl;
char c=‘!’;
cout<<“c = “<< c << “, int(c) = “ << int (c) <<endl;

 2003 Prentice Hall, Inc. All rights reserved.


5

Number of Bytes Used

• char :1
• short :2
• int :4
• unsigned short :2
• unsigned int :4
• float :4
• double :8

 2003 Prentice Hall, Inc. All rights reserved.


6

Integer Types

• short→ -32.768 to 32.767


• int→-2.147.483.648 to 2.147.483.647
• unsigned short → 0 to 65.535
• unsigned int → 0 to 4.294.967.295

 2003 Prentice Hall, Inc. All rights reserved.


7

Types Conversion

int main()
{
char c = 'A';
cout<<"char c =" << c <<endl;
short k = c;
cout<<"short k =" << k<<endl;
int m = k;
cout<<"int m =" << m <<endl;
float x = m;
cout<<"float x =" << x <<endl;
double y = x;
cout<<"double y =" << y <<endl;
cout<<endl;

double a = 1234.5678;
cout<<"double a =" << a <<endl;
int b = a;
cout<<"int b =" << b <<endl;

 2003 Prentice Hall, Inc. All rights reserved.


8

Types Conversion

 2003 Prentice Hall, Inc. All rights reserved.


9

Arithmetic

• Rules of operator precedence


– Operators in parentheses evaluated first
• Nested/embedded parentheses
– Operators in innermost pair first
– Multiplication, division, modulus applied next
• Operators applied from left to right
– Addition, subtraction applied last
Operator(s) • Operators applied fromOrder
Operation(s) leftoftoevaluation
right (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.

 2003 Prentice Hall, Inc. All rights reserved.


10

Arithmetic

• Arithmetic calculations
– *
• Multiplication
– /
• Division
• Integer division truncates remainder
– 7 / 5 evaluates to 1
– %

• Modulus operator returns remainder


– 7 % 5 evaluates to 2

 2003 Prentice Hall, Inc. All rights reserved.


11

Square Root
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

int nilai;
cout<<"Masukkan sebuah nilai: ";
cin >> nilai;
int hasil = sqrt(nilai);
cout<< "Hasil akar pangkat dua dari nilai tersebut = " << hasil << endl;

system ("pause");
return 0;

 2003 Prentice Hall, Inc. All rights reserved.


12

More on Arithmetic

• Pangkat
– a2 → pow (a,2)

• Rounding the decimal


– #include<iomanip>
– cout << fixed;
• scientific notification into decimal
– 4/1000000000 → 4e-9 → 0.000000004
– cout << setprecision (3);
• Round up the result into 3 numbers behind decimal

 2003 Prentice Hall, Inc. All rights reserved.


13

Exercise

 2003 Prentice Hall, Inc. All rights reserved.


14

Composite Assignment Operators

• Assignment expression abbreviations


– Addition assignment operator
c = c + 3; abbreviated to
c += 3;
• Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator= expression;

• Other assignment operators


d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
 2003 Prentice Hall, Inc. All rights reserved.
15

Increment and Decrement Operators

• Increment operator (++)


– Increment variable by one
– c++
• Same as c += 1

• Decrement operator (--) similar


– Decrement variable by one
– c--
• Same as c -= 1

 2003 Prentice Hall, Inc. All rights reserved.


16

Increment and Decrement Operators

• Preincrement
– Variable changed before used in expression
• Operator before variable (++c or --c)
• Postincrement
– Incremented changed after expression
• Operator after variable (c++, c--)

 2003 Prentice Hall, Inc. All rights reserved.


17

Increment and Decrement Operators

• If c = 5, then
– cout << ++c;
• c is changed to 6, then printed out

– cout << c++;


• Prints out 5 (cout is executed before the increment)
• c then becomes 6

 2003 Prentice Hall, Inc. All rights reserved.


18

Increment and Decrement Operators

• When variable not in expression


– Preincrementing and postincrementing have same effect
++c;
cout << c;
and
c++;
cout << c;

are the same

 2003 Prentice Hall, Inc. All rights reserved.


19
1 // Fig. 2.14: fig02_14.cpp
2 // Preincrementing and postincrementing.
Outline
3 #include <iostream>
4
fig02_14.cpp
5 using std::cout;
6 using std::endl;
(1 of 2)
7
8 // function main begins program execution
9 int main()
10 {
11 int c; // declare variable
12
13 // demonstrate postincrement
14 c = 5; // assign 5 to c
15 cout << c << endl; // print 5
16 cout << c++ << endl; // print 5 then postincrement
17 cout << c << endl << endl; // print 6
18
19 // demonstrate preincrement
20 c = 5; // assign 5 to c
21 cout << c << endl; // print 5
22 cout << ++c << endl; // preincrement then print 6
23 cout << c << endl; // print 6

 2003 Prentice Hall, Inc.


All rights reserved.
20
24
25 return 0; // indicate successful termination
Outline
26
27 } // end function main
fig02_14.cpp
(2 of 2)
5
5
6
fig02_14.cpp
output (1 of 1)
5
6
6

 2003 Prentice Hall, Inc.


All rights reserved.
21

Increment and Decrement Operators


#include <iostream>
using namespace std;

int main()
{
int m, n;
m =44;
n = ++m;
cout<< "m = " << m << " n = " << n <<endl;

m =44;
n = m++;
cout<< "m = " << m << " n = " << n <<endl;

system("pause");
return 0;
}

 2003 Prentice Hall, Inc. All rights reserved.


22

Scope


int main ()
{
x=11; //error
int x;
{
x=22; //ok
y=33; //error
int y;
x=44; //ok
y=55; //ok
}
x=66; //ok
y=77; //error


}

 2003 Prentice Hall, Inc. All rights reserved.

You might also like