Presentation 2nd

30
DataType,Loop and Making decision Chorn Charly Leang Bunrong Cheng Udam Kan Vichhai Srou Chanthorn Em Vy Seth Sambo Chea Socheat

Transcript of Presentation 2nd

Page 1: Presentation 2nd

DataType,Loop and Making decision

Chorn Charly

Leang Bunrong

Cheng Udam

Kan Vichhai

Srou Chanthorn

Em Vy

Seth Sambo

Chea Socheat

Page 2: Presentation 2nd

Content• Primitive Data type

• Object Type

• Id pointer type

• For Loop

• Do while

• While

• Continues and break

• If , else if , switch , unary operation

Page 3: Presentation 2nd

Primitive Data Type

In objective-C programming language provide four basic primitive data types:

• Type int: can be used to contain integral values only and cannot contain decimal digits.

• Type float: can be used to contain decimal digits.

• Type double: is the same type float, typically with roughly twice the range of decimal.

• Type char: can be used to store a single character.

And more is Qualifiers: long, long long, short, unsigned, and signed.

Page 4: Presentation 2nd

Type int

In general, this data type cannot contain decimal so the amount of the values has defined some rule below:

• No embedded spaces between the digits. (ex: 12 000)

• Comma can’t be used. (ex: 12,000)

• Integer variable might take 32bits on your computer, but if your computer were used 64bits, much larger number can be stored inside integer variables than 32bits were used instead. Therefore they called implementation or machine dependent.

Page 5: Presentation 2nd

Type float & Type double

• As describe above the double type is the same as type float, only with roughly twice the range.

• When a variable is assigned a number entered with decimal place, such as 4.2 the float or double have real value below:

• This is the problem when you want to make comparison.

Page 6: Presentation 2nd

Type float & Type double(cont.)

• The problem come when you write something like this:

• If instead you compare against the single precision literal “4.2” like this:

• The comparison succeeds, because the values are exactly equal.

Page 7: Presentation 2nd

Type char

• You can use a char variable to store single character, such as letter or digit character.

• Defining char character by enclosing the character within a pair of single quotation marks.

• Example: char charVar = ‘a’;

Page 8: Presentation 2nd

Qualifiers

• If the qualifier long is placed directly before the any data type declaration such as int , short , the declared integer or short variable is of extended range on some computer system. (Example: long int var1;)

• You can also have a long long int variable, or even a long double variable to hold a floating point number with greater range.

Page 9: Presentation 2nd

Qualifiers

• If the qualifier long is placed directly before the any data type declaration such as int , short , the declared integer or short variable is of extended range on some computer system. (Example: long int var1;)

• You can also have a long long int variable, or even a long double variable to hold a floating point number with greater range.

Page 10: Presentation 2nd

Object type in Objective-C• There 2 Class of object type in Objective-C:

• NSNumber

• NSString

• NSNumber, it can contain all kind of number in Objective-C such as: floating-point, double, boolean, char, short, and int. and it contain a lot of method also.

• Example: NSNumber *n = [NSNumber numberWithfloat:10.2f];

NSNumber *n = [NSNumber numberWithint:10];

NSNumber *n = [NSNumber numberWithBOOL:YES];

....................................................................................

Page 11: Presentation 2nd

Object type in Objective-C

• NSString, It can contain only string like in java also. and it have a lot method that allow its object can use.

• it can not use init method.

• Example: NSString *s = @”Hello”;

• NSMutableString is a subclass of NSString

• it have a lot of method and it can use init method when

object was created;

• Example: NSMutableString *s = [[NSMutableString alloc]initWithString:@”Hi”];

NSString

NSMutableString

Page 12: Presentation 2nd

Object type in Objective-C

• Note: all of them stay in Foundation/Foundation.h framework.

• NSString vs NSMutableString

NSString NSMutableString

can not use init method can use init method

Super class of NSMutableString Sub class of NSString

object type object type

storing letter storing letter

Page 13: Presentation 2nd

Object type in Objective-C

• Note: all of them stay in Foundation/Foundation.h framework.

• NSString vs NSMutableString

NSString NSMutableString

can not use init method can use init method

Super class of NSMutableString Sub class of NSString

object type object type

storing letter storing letter

Page 14: Presentation 2nd

ID• Id is pointer type can points to any object type of class . The id type is the basis for very

important features in Objective-C known as polymorphism and dynamic binding.

• Generally this pointer type use for assign or return new object from unknow type of class to another type of class.

Example

• -(id) newObject: (int) type;

• Id *a[ClassB alloc] init];

• ClassA *a [[Class B alloc]init]; //Error

+Id with Method :This declares an instance method called newObject that takes a single integer argument called type and returns a value of type id .

+Id with instance variable : This case id is auto turn to ClassB type because id is use to dynamic binding with unknow of type

+instance variable with unknow blinding : error case

Page 15: Presentation 2nd

For loop

• Let’s imagine that we want to NSLog(@”Hello World”) 10 time without using for loop, so our program will be looked like:#import <Foundation/Foundation.h>int main(int argc, char * argv[] ){@autoreleasepool{NSLog(@”Hello, World!”);NSLog(@”Hello, World!”);NSLog(@”Hello, World!”);NSLog(@”Hello, World!”);NSLog(@”Hello, World!”);}return 0;}

Result after debug and run

Page 16: Presentation 2nd

For loop

• Components in the for loop:

init_expression, is used to set the initial values before the loop begins.

loop_condition, specifies the condition or conditions necessary for the loop to continue.

loop_expression, change the value of the index variable, frequently by adding 1 to it or subtracting 1 from it.

• Now let rewrite our code with for loop in order to NSLog(@”Hello, World!”) 5 times:

#import <Foundation/Foundation.h>int main(int argc, char * argv[] ){@autoreleasepool{for(int i = 0 ; i < 5 ; i ++)NSLog(@”Hello, World”);}return 0;}

Page 17: Presentation 2nd

For loop

• For loop is a kind of loop that we usually use when we want to execute our program statements (code) again and again within a specify number of time.

• For loop systax: for ( init_expression; loop_condition; loop_expression )

program statement

for ( init_expression; loop_condition; loop_expression ) {program statements

}

Page 18: Presentation 2nd

Program Looping

• While Loop: a while loop statement in Objective-C programming language repeatedly

execute a target statement as long as given condition is true;

Syntax : While (expression){ program statement;}

• Remark: expression inside the parentheses is evaluated.

a. If expression after evaluated is true the program statement is executed. It will do again and again

until expression execution is false;

b. if expression after evaluated is false then the program will be terminated;

Page 19: Presentation 2nd

Programming Looping

Example

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){ @autoreleasepool { int i; while(i<5){ NSLog(@"%i",i); i++; } } return 0;}

Page 20: Presentation 2nd

Program Looping

• Do Loop :

A do...while loop is similar to a while loop, except that a do...while loop is

guaranteed to execute at least one time.

Syntax :do{ program statement;

}while(expression);

Page 21: Presentation 2nd

Program Looping

Example

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){ @autoreleasepool { int i; do{ NSLog(@"%i",i); i++; }while(i<5); } return 0;}

Page 22: Presentation 2nd

Program Looping• Continue

In Objective-C Continue statement is used :

- force the next iteration of the loop to take place, skipping any code in between.

Syntax:continue;

• Break :

In Objective-C , break statement has two usages:

1. When it is inside a loop, then the loop is immediately terminated.

2. It’s can be use to terminate case in switch statement

Syntax:

break;

Page 23: Presentation 2nd

Making Decision If Statement

If statement is used for making decision when conditions are not specific value. Conditions are in range (1 to 15,5 to 10…so on).

There are two types of if statement in objective-C:

1. If ……else ..statement.

2. If….else if….. Else ..statement.

Page 24: Presentation 2nd

Making Decision (cont.) If …else statement

If ….else statement is used when has two decision. If one decision is executed so anther decision is not executed.

Syntax:

If (expression){

// statements will executed when expression is true.

}

Else{

// statements will execute when expression is not true.

}

Page 25: Presentation 2nd

Making Decision (cont.) If …else statement process.

Page 26: Presentation 2nd

Making Decision (cont.) If …else if …else statement

If ….else if…else statement is used when we have decision more than two. Syntax:

If (expression){

// statements will executed when expression is true.

}

Else if (expression){

// statements will execute when expression is true.}

…………………………………….

Else{

//statements will execute when all above expression is not true.}

Page 27: Presentation 2nd

Making Decision (cont.) If …else if …else statement process.

expr1

expr2

else

False

False

True

True

statement1

statement2

statement3

Page 28: Presentation 2nd

The Switch Statement

• Switch statement is used for making decision when value is specific.

• It execute only the block that case value is match, after that it won’t execute any case and stop the switching.

• Switch is useful when we have variety of specific values to make decision.

Page 29: Presentation 2nd

Switch (expression)

{

case value1:

statement(s);

break;

case value2:

statement(s);

break;

default:

statement(s);

break;

}

Page 30: Presentation 2nd

The Conditional Operator

• The conditional operator is a ternary operator, which takes 3 operands.

• The first operand is placed before “?” the second between “?” and “:” and the third after the “:”

• Syntax:

condition? expression 1: expression2;

• If the condition is true (non zero) it will evaluate the expression1

• Else if the condition is false (zero) it will evaluate the expression2