This PowerPoint helps students to consider the concept of infinity.
OOP Chapter 4: Data Type and Expressions
1. Object-Oriented Programming Language
Chapter 4 : Data Types and Expressions
Atit Patumvan
Faculty of Management and Information Sciences
Naresuna University
Monday, February 20, 12
2. 2
Contents
• Data Types and Constants
• Arithmetic Expressions
• Assignment Operators
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
3. 3
Data Type and Constant
data_type identifier = value;
identifier
value
data_type
int number = 25; copy 25
number
25
int
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
4. 4
Data Types in Objective-C
Type Descriptions Size
char A character of the local character set 1 bytes
int An integer (whole numbers) 4 bytes
float Floating point number 4 bytes
short A short integer 2 bytes
long A double short 4 bytes
long long A double long 8 bytes
double Double precision float 8 bytes
BOOL
id An object 4 bytes
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
5. 5
Uses the basic Objective-C data types
Program 4.1
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, char *argv[]) {
04:! @autoreleasepool {
05:! ! int integerVar = 100;
06:! ! float floatingVar = 331.79;
07:! ! double doubleVar = 8.44e-11;
08:! ! char charVar = 'w';
09:! !
10:! ! NSLog(@"integerVar = %i", integerVar);
11:! ! NSLog(@"floatVar = %f", floatingVar);
12:! ! NSLog(@"doubleVar = %e", doubleVar);
13:! ! NSLog(@"doubleVar = %g", doubleVar);
14:! ! NSLog(@"charVar = %c", charVar);
15: }
integerVar = 100
floatVar = 331.790009
doubleVar = 8.440000e-11
doubleVar = 8.44e-11
charVar = w
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
6. 6
Basic Data Types
Type Constant Example NSLog chars
char a’, ‘n’ %c
short int - %hi, %hx, %ho
unsigned short int - %hu, %hx, %ho
int 12, -97, 0xFFE0, 0177 %i, %x, %o
unsigned int 12u, 100U, 0xFFu %u, %x, %o
long int 12L, -2001l, 0xffffL %li, %lx, %lo
unsigned long int 12UL, 100ul, 0xffeeUL %lu, %lx, %lo
long long int 0xe5e5e5e5LL, 500ll %lli, %llx, %llo
unsigned long long 12ull, 0xffeeULL %llu, % llx,
int
float 12.43f, 3.1e-5f, 0x1.5p10, %llo
%f, %e, %g, %a
double 0x1p-1
12.34, 3.1e-5, 0x.1p3 %f, %e, %g, %a
long double 12.34L, 3.1e-5l %Lf, %Le, %Lg
id nil %p
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
7. 7
Arithmetic Expression
Program 4.3
05:
06: int a = 2; a - b = 98
07:! ! int b = 2; b * c = 50
08:! ! int c = 25; a / c = 4
09:! ! int d = 4; a + b * c = 150
10:! ! int result; a * b + c * d = 300
11:! !
12:! ! result = a - b;!// subtraction
13:! ! NSLog(@"a - b = %i", result);
14:
15:! ! result = b * c;!// multiplication
16:! ! NSLog(@"b * c = %i", result);
17:! !
18:! ! result = a / c;!// division
19:! ! NSLog(@"a / c = %i", result);
20:
21:! ! result = a + b * c;! // precedence
22:! ! NSLog(@"a + b * c = %i", result);
23:! !
24:! ! NSLog(@"a * b + c * d = %i", a * b + c * d);
25:!
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
8. 8
Integer Arithmetic and Unary Minus Operator
Program 4.3
05:
06:! int a = 25;
07:! ! int b = 2;
08:! ! float c = 25.0;
09:! ! float d = 2.0;
10:! !
11:! ! NSLog(@"6 + a / 5 * b = %i", 6 + a / 5 * b);
12:! ! NSLog(@"a / b * b = %i", a / b * b);
13:! ! NSLog(@"c / d * d = %f", c / d * d);
14:! ! NSLog(@"-a = %i", -a);
15:!
6 + a / 5 * b = 16
a / b * b = 24
c / d * d = 25.000000
-a = -25
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
9. 9
The Modulus Operator
Program 4.4
05:
06:! int a = 25, b = 5, c = 10, d =7;
07:!
08:! NSLog(@"a %% b = %i", a % b);
09:! NSLog(@"a %% c = %i", a % c);
10:! NSLog(@"a %% d = %i", a % d);
11:! NSLog(@"a / d * d + a %% d = %i", a / d * d + a % d);
12:!
a % b = 0
a % c = 5
a % d = 4
a / d * d + a % d = 25
a /d * d+ a % d = ((a / d) * d)+(a % d)
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
10. 10
Integer and Floating-Point Conversion
Program 4.5
05:
06:! float f1 = 123.125, f2;
07:! int i1, i2 = -150;
08:!
09:! i1 = f1; // floating to integer convision
10:! NSLog(@"%f assigned to an int produces %i", f1, i1);
11:!
12:! f1 = i2; // integer to floating convision
13:! NSLog(@"%i assigned to a float produces %f", i2, f1);
14:!
15:! f1 = i2 / 100; // integer divided by integer
16:! NSLog(@"%i divided by 100 produces %f", i2, f1);
17:!
18:! f2 = i2 /100.0; // integer divided by float
19:! NSLog(@"%i divided by 100.0 produce %f", i2, f2);
20:!
21:! f2 = (float) i2 / 100; // type cast operator
22:! NSLog(@"(float) %i divided by 100 produces %f", i2, f2);
123.125000 assigned to an int produces 123
-150 assigned to a float produces -150.000000
-150 divided by 100 produces -1.000000
-150 divided by 100.0 produce -1.500000
(float) -150 divided by 100 produces -1.500000
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
11. 11
The Type Case Operator
int i2 = 25;
float f2 = (float) i2 / 5; // type cast operator
int s1 = (int) 29.55 + (int) 21.99;
int s2 = 29 + 21;
float f3 = (float) 6 / (float) 4
float f4 = (float) 6 / 4
id myNumber;
Fraction * myFraction;
myFraction = (Fraction *) myNumber;
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
12. 12
Assignment Operator
count += 10;
count = count +10;
counter -= 5;
counter = counter - 5;
a /= b + c;
a = a / (b + c);
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
13. 13
Increment and Decrement Operator
count++;
count = count + 1;
counter--;
counter = counter - 1;
a /= b + c;
a = a / (b + c);
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12
14. 14
Desk Calculator
Program 4.6
48:
49:! int main(int argc, char *argv[]) {
50:! ! @autoreleasepool {
51:! ! ! Calculator * deskCalc = [[Calculator alloc] init];
52:! ! ! [deskCalc setAccumulator: 100.0];
53:! ! !
54:! ! ! [deskCalc add: 200.];
55:! ! ! [deskCalc divide: 15.0];
56:! ! ! [deskCalc subtract: 10.0];
57:! ! ! [deskCalc multiply: 5]; Calculator
58:! ! !
59:! ! ! NSLog(@"The result is % g", [deskCalc accumulator]);
60:! ! ! accumulator:double
61:! ! ! [deskCalc release];
62:! ! }
63:! } setAccumulator(double):void
64:! clear:void
accumulator:double
add(double):void
The result is 50 subtract(double):void
multiply(double):void
divide(double):void
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Monday, February 20, 12