SlideShare a Scribd company logo
Object-Oriented Programming Language
                                Chapter 6 : Making Decisions


                          Atit Patumvan
          Faculty of Management and Information Sciences
                        Naresuan University
2



                                                                                     Contents



             •        The if statement

             •        The switch statement

             •        The conditional operator




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University              Object-Oriented Programming Language
3



                                                                           The if Statement


                 if ( expression )
                    program_statement

                                                                                              [ expression ]   [ !expression ]


                                                                                     program_statement




                 if ( it is not raining )
                    i will go to swimming
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                   Object-Oriented Programming Language
4



                                                                            The if Statement

Program 6.1
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:
07:! int number;
08:
09:! NSLog(@"Type in your number: ");
10:! scanf("%i", &number);
11:
12:! if( number < 0 )
13: ! ! number = -number;
14:
15:! NSLog(@"The absolute value is %i", number);
16:
17:! [pool drain];                                 Type in your number:
18:! return 0;                                     -100
19: }                                              The absolute value is 100

                                                                                      Type in your number:
                                                                                      2000
                                                                                      The absolute value is 2000

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                 Object-Oriented Programming Language
5



                                                                           The if Statement


            -(double) convertToNum
            {
               return (double) numerator / denominator;
            }



             [ condition ]                                      [ ! condition ]


         process 1                                                           process 2
                                                                                         -(double) convertToNum
                                                                                         {
                                                                                            if (denominator != 0 )
                                                                                                return numerator / denominator;
                                                                                            else
                                                                                                return NAN;
                                                                                         }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                    Object-Oriented Programming Language
6



                                                                            The if Statement

Program 6.2
37: -(double) convertToNum
38: {
39:! if(denominator != 0)
40:! ! return (double) numerator/denominator;
41:! else
42:! ! return NAN;
43: }

51:!      Fraction * aFraction = [[Fraction alloc] init];
52:!      Fraction * bFraction = [[Fraction alloc] init];
53:!
54:!      [aFraction setNumerator: 1];! /1st fraction is 1/4
                                      /
55:!      [aFraction setDenominator: 4];
56:
57:!      [aFraction print];
58:!      NSLog(@" =");                                                                        1/4
59:!      NSLog(@" %g", [aFraction convertToNum]);                                             =
60:                                                                                            0.25
61:!      [bFraction print];! / never assigned a value
                            /                                                                  0/0
62:!      NSLog(@" =");                                                                        =
63:!      NSLog(@" %g", [bFraction convertToNum]);                                             nan
64:!      !
65:!      [aFraction release];
66:!      [bFraction release];

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                    Object-Oriented Programming Language
7



                                                       Triangular Number Example

Program 6.3, Program 6.4
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:
07:! int number_to_test, remainder;
08:
09:! NSLog(@"Enter your number to ne tested: ");
10:
11:! scanf("%i", &number_to_test);
12:
13:! remainder = number_to_test % 2;
14:
15:! if( remainder == 0 )
16: ! ! NSLog(@"The number is even.");                if( remainder == 0 )
17:                                                    ! ! NSLog(@"The number is even.");
18:! if( remainder != 0)                              ! else
19:! ! NSLog(@"The number is odd.");                  ! ! NSLog(@"The number is odd.");
20:
21:! [pool drain];
22:! return 0;
23: }


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
8



                                                         Compound Relational Test


                             Logical Operator                                        Descriptions

                                            &&                                          AND

                                              ||                                         OR

                                               !                                        NOT



               if ( grade > 70 && grade <= 79 )
                  ++grades_70_to79;

               if ( index < 0 || index > 99 )
                  NSLog (@”Error - index out of range”);
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                  Object-Oriented Programming Language
9



                                                        Compound Relational Tests

Program 6.5
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:
07:! int year, rem_4, rem_100, rem_400;
08:
09:! NSLog(@"Enter the year to be tested: ");
10:
11:! scanf("%i", &year);
12:
13:! rem_4 = year % 4;
14:! rem_100 = year % 100;
15:! rem_400 = year % 400;
16:
17:! if( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 )
18:! ! NSLog(@"It's a leap year.");
19:! else
20:! ! NSLog(@"Nope, It's not a leap year.");
21:
22:! [pool drain];
23:! return 0;
24: }

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
10



                                                                   Nested if Statements


               if ( [chessGame isOver] == NO )
                  if ( [chessGame whoseTurn] == YOU )
                     [chessGame yourMove];

               if ( [chessGame isOver] == NO && [chessGame whoseTurn] == YOU )
                     [chessGame yourMove];




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University        Object-Oriented Programming Language
11



                                                                   Nested if Statements


               if ( [chessGame isOver] == NO )
                  if ( [chessGame whoseTurn] == YOU )
                     [chessGame yourMove];
                  else
                     [chessGame myMove];

               if ( [chessGame isOver] == NO )
                  if ( [chessGame whoseTurn] == YOU )
                     [chessGame yourMove];
                  else
                     [chessGame myMove];
               else
                  [chessGame finish];




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University        Object-Oriented Programming Language
12



                                                                                Dangling Else


            if ( [chessGame isOver] == NO )                                              if ( [chessGame isOver] == NO )
               if ( [chessGame whoseTurn] == YOU )                                          if ( [chessGame whoseTurn] == YOU )
                  [chessGame yourMove];                                                        [chessGame yourMove];
            else                                                                            else
               [chessGame finish];                                                              [chessGame finish];



                if ( [chessGame isOver] == NO ) {
                   if ( [chessGame whoseTurn] == YOU )
                      [chessGame yourMove];
                }
                else
                   [chessGame finish];




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                 Object-Oriented Programming Language
13



                                                                   The else if Construct




                if ( expression_1 )
                                                                                     if ( expression_1 )
                   program_statement_1
                                                                                        program_statement_1
                else
                                                                                     else if ( expression_2 )
                  if ( expression_2 )
                                                                                        program_statement_2
                     program_statement_2
                                                                                     else
                  else
                                                                                        program_statement_3
                     program_statement_3




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                         Object-Oriented Programming Language
14



                                                                    The else if Construct

Program 6.6
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04:{
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:
07:! int number, sign;
08:
09:! NSLog(@"Please type in a number: ");
10:! scanf("%i", &number);
11:
12:! if (number <0)
13:! ! sign = -1;
14:! else if ( number == 0 )
15:! ! sign = 0; // Must be positive
16:! else
17:! ! sign = 1;
18:!
19:! NSLog(@"Sign = %i", sign);
20:
21:! [pool drain];
22:! return 0;
23:


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University          Object-Oriented Programming Language
15



                                                                    The else if Construct

Program 6.7

01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:
07:! char c;
08:
09:! NSLog(@"Enter a single character: ");
10:! scanf(" %c", &c);
11:
12:! if (( c>= 'a' && c<='z') || (c>='A' && c<='Z'))
13:! ! NSLog(@"It's a alphabetic character.");
14:! else if (c >='0' && c<='9')
15:! ! NSLog(@"It's a digit.");
16:! else
17:! ! NSLog(@"It's a special character.");
18:
19:! [pool drain];
20:! return 0;
21: }



Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University          Object-Oriented Programming Language
16



                                                                    The else if Construct

Program 6.8, Program 6.8A
01: int main(int argc, const char * argv[])
02: {
03:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
04:! double! value1, value2;
05:! char! ! operator;
06:! Calculator * deskCalc = [[Calculator alloc] init];
07:! NSLog(@"Type in your expression. ");
08:! scanf("%lf %c %lf", &value1, & operator, &value2);
09:
10:! [deskCalc setAccumulator: value1];
11:! if (operator == '+')                       11:! ! if (operator == '+')
12:! ! [deskCalc add: value2];                  12:! ! ! [deskCalc add: value2];
13:! else if (operator == '-')                  13:! ! else if (operator == '-')
14:! ! [deskCalc subtract: value2];             14:! ! ! [deskCalc subtract: value2];
15:! else if (operator == '*')                  15:! ! else if (operator == '*')
16:! ! [deskCalc multiply: value2];             16:! ! ! [deskCalc multiply: value2];
17:! else if (operator == '/')                  17:! ! else if (operator == '/')
18:! ! [deskCalc divide: value2];               18:! ! ! if( value2 == 0)
19:!                                            19:! ! ! ! NSLog(@"Division by zero.");
20:! NSLog(@"%.2f", [deskCalc accumulator]);    20:! ! ! else!
21:                                             21:! ! ! ! [deskCalc divide: value2];
22:! [deskCalc release];                        22:! ! else
23:! [pool drain];                              23:! ! ! NSLog(@"Unknown operator.");
24:! return 0;
25: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University          Object-Oriented Programming Language
17



                                                                  The switch Statement


                 if ( expression == value1)
                                                                                     switch ( expression )
                 {
                                                                                     {
                       program statement;
                                                                                       case value1:
                       program statement;
                                                                                         program statement;
                        …
                                                                                         program statement;
                 }
                                                                                          …
                 else if ( expression == value2)
                                                                                         break;
                 {
                                                                                       case value2:
                       program statement;
                                                                                         program statement;
                       program statement;
                                                                                         program statement;
                        …
                                                                                          …
                 }
                                                                                         break;
                 else if ( expression == value3)
                                                                                       case value3:
                 {
                                                                                         program statement;
                       program statement;
                                                                                         program statement;
                       program statement;
                                                                                          …
                        …
                                                                                         break;
                 }
                                                                                       default:
                 else
                                                                                         program statement;
                 {
                                                                                         program statement;
                       program statement;
                                                                                          …
                       program statement;
                                                                                         break;
                        …
                                                                                     }
                 }


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                            Object-Oriented Programming Language
18



                                                                    The else if Construct

Program 6.8, Program 6.9
03:!      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
04:!      double! value1, value2;
05:!      char! ! operator;
06:!      Calculator * deskCalc = [[Calculator alloc] init];
07:!      NSLog(@"Type in your expression. ");
08:!      scanf("%lf %c %lf", &value1, & operator, &value2);
09:
10:!      ! if (operator == '+')                                                     10:!   !   switch (operator) {
11:!      ! ! [deskCalc add: value2];                                                11:!   !   ! ! case '+':
12:!      ! else if (operator == '-')                                                12:!   !   ! ! ! [deskCalc add: value2];
13:!      ! ! [deskCalc subtract: value2];                                           13:!   !   ! ! ! break;
14:!      ! else if (operator == '*')                                                14:!   !   ! ! case '-':
15:!      ! ! [deskCalc multiply: value2];                                           15:!   !   ! ! ! [deskCalc subtract: value2];
16:!      ! else if (operator == '/')                                                16:!   !   ! ! ! break;
17:!      ! ! if( value2 == 0)                                                       17:!   !   ! ! case '*':
18:!      ! ! ! NSLog(@"Division by zero.");                                         18:!   !   ! ! ! [deskCalc multiply: value2];
19:!      ! ! else!                                                                  19:!   !   ! ! ! break;
20:!      ! ! ! [deskCalc divide: value2];                                           20:!   !   ! ! case '/':
21:!      ! else                                                                     21:!   !   ! ! ! [deskCalc divide: value2];
22:!      ! ! NSLog(@"Unknown operator.");!                                          22:!   !   ! ! ! break;
23:!      NSLog(@"%.2f", [deskCalc accumulator]);                                    23:!   !   ! ! default:
24:                                                                                  24:!   !   ! ! ! NSLog(@"Unknown operator.");
25:!      [deskCalc release];                                                        25:!   !   ! }
26:!      [pool drain];
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                    Object-Oriented Programming Language
19



                                                                           Boolean Variable

Program 6.10, Program 6.10A
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:
07:! int p, d, isPrime;
08:!
09:! for (p =2; p<=50; ++p){                    08:! BOOL isPrime;
10:! ! isPrime =1;                              09:! !
11:! !                                          10:! ! for (p =2; p<=50; ++p){
12:! ! for ( d = 2; d < p; ++d)                 11:! ! ! isPrime = YES;
13:! ! ! if ( p % d == 0)                       12:! ! !
14:! ! ! ! isPrime = 0;                         13:! ! ! for ( d = 2; d < p; ++d)
15:! ! ! !                                      14:! ! ! ! if ( p % d == 0)
16:! ! if( isPrime != 0 )                       15:! ! ! ! ! isPrime = NO;
17:! ! ! NSLog(@"%i ", p);                      16:! ! ! ! !
18:! }                                          17:! ! ! if( isPrime == YES )
19:!                                            18:! ! ! ! NSLog(@"%i ", p);
20:! [pool drain];                              19:! ! }
21:! return 0;
22: }


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University            Object-Oriented Programming Language

More Related Content

PDF
OOP Chapter 4: Data Type and Expressions
PDF
OOP Chapter 3: Classes, Objects and Methods
PDF
OOP Chapter 7 : More on Classes
PDF
OOP: Chapter 2: Programming in Objective-C
PDF
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
PDF
An Alignment-based Pattern Representation Model for Information Extraction
PDF
Statistically adaptive learning for a general class of..
PPT
JavaYDL5
OOP Chapter 4: Data Type and Expressions
OOP Chapter 3: Classes, Objects and Methods
OOP Chapter 7 : More on Classes
OOP: Chapter 2: Programming in Objective-C
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
An Alignment-based Pattern Representation Model for Information Extraction
Statistically adaptive learning for a general class of..
JavaYDL5

Viewers also liked (6)

PDF
Chapter 01 mathmatics tools (slide)
PDF
Chapter 0 introduction to theory of computation
PDF
Pat1 set1
PDF
An Overview of eZee Burrp! (Philus Limited)
PDF
คณิตศาสตร์คืออะไร
PDF
How to Study and Research in Computer-related Master Program
Chapter 01 mathmatics tools (slide)
Chapter 0 introduction to theory of computation
Pat1 set1
An Overview of eZee Burrp! (Philus Limited)
คณิตศาสตร์คืออะไร
How to Study and Research in Computer-related Master Program
Ad

Similar to OOP Chapter 6: Making Decisions (20)

PPTX
Embedded systems
PPTX
Final exam
PPTX
Final exam
PDF
C++ Chapter II
DOC
Lab 5 2012/2012
DOC
Lab sheet 1
PDF
C programs
PPTX
Final Exam in FNDPRG
DOCX
OverviewThis hands-on lab allows you to follow and experiment w.docx
DOCX
Cs291 assignment solution
PPTX
Programming fundamentals lecture 4
PDF
Decision control
PPT
Mesics lecture 6 control statement = if -else if__else
DOC
Unit 3
PDF
Fundamentals of programming)
PPT
L4 functions
PDF
03 expressions.ppt
PDF
Javascript Refererence
PPT
C tutorial
DOC
Unit 3
Embedded systems
Final exam
Final exam
C++ Chapter II
Lab 5 2012/2012
Lab sheet 1
C programs
Final Exam in FNDPRG
OverviewThis hands-on lab allows you to follow and experiment w.docx
Cs291 assignment solution
Programming fundamentals lecture 4
Decision control
Mesics lecture 6 control statement = if -else if__else
Unit 3
Fundamentals of programming)
L4 functions
03 expressions.ppt
Javascript Refererence
C tutorial
Unit 3
Ad

More from Atit Patumvan (20)

PDF
Iot for smart agriculture
PDF
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
PDF
Chapter 1 mathmatics tools
PDF
Chapter 1 mathmatics tools
PDF
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
PPTX
Media literacy
PDF
การบริหารเชิงคุณภาพ ชุดที่ 8
PDF
การบริหารเชิงคุณภาพ ชุดที่ 7
PDF
การบริหารเชิงคุณภาพ ชุดที่ 6
PDF
Computer Programming Chapter 5 : Methods
PDF
Computer Programming Chapter 4 : Loops
PDF
Introduction to Java EE (J2EE)
PDF
การบริหารเชิงคุณภาพ ชุดที่ 5
PDF
การบริหารเชิงคุณภาพ ชุดที่ 4
PDF
การบริหารเชิงคุณภาพ ชุดที่ 3
KEY
การบริหารเชิงคุณภาพ ชุดที่ 2
PDF
Computer Programming: Chapter 1
PDF
การบริหารเชิงคุณภาพ ชุดที่ 1
PDF
Write native iPhone applications using Eclipse CDT
PDF
OOP Chapter 8 : Inheritance
Iot for smart agriculture
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
Chapter 1 mathmatics tools
Chapter 1 mathmatics tools
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
Media literacy
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 6
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 4 : Loops
Introduction to Java EE (J2EE)
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 2
Computer Programming: Chapter 1
การบริหารเชิงคุณภาพ ชุดที่ 1
Write native iPhone applications using Eclipse CDT
OOP Chapter 8 : Inheritance

Recently uploaded (20)

PPTX
Presentation on HIE in infants and its manifestations
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Lesson notes of climatology university.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Cell Structure & Organelles in detailed.
Presentation on HIE in infants and its manifestations
Complications of Minimal Access Surgery at WLH
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Final Presentation General Medicine 03-08-2024.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Lesson notes of climatology university.
Microbial disease of the cardiovascular and lymphatic systems
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
Computing-Curriculum for Schools in Ghana
Cell Structure & Organelles in detailed.

OOP Chapter 6: Making Decisions

  • 1. Object-Oriented Programming Language Chapter 6 : Making Decisions Atit Patumvan Faculty of Management and Information Sciences Naresuan University
  • 2. 2 Contents • The if statement • The switch statement • The conditional operator Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 3. 3 The if Statement if ( expression ) program_statement [ expression ] [ !expression ] program_statement if ( it is not raining ) i will go to swimming Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 4. 4 The if Statement Program 6.1 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06: 07:! int number; 08: 09:! NSLog(@"Type in your number: "); 10:! scanf("%i", &number); 11: 12:! if( number < 0 ) 13: ! ! number = -number; 14: 15:! NSLog(@"The absolute value is %i", number); 16: 17:! [pool drain]; Type in your number: 18:! return 0; -100 19: } The absolute value is 100 Type in your number: 2000 The absolute value is 2000 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 5. 5 The if Statement -(double) convertToNum { return (double) numerator / denominator; } [ condition ] [ ! condition ] process 1 process 2 -(double) convertToNum { if (denominator != 0 ) return numerator / denominator; else return NAN; } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 6. 6 The if Statement Program 6.2 37: -(double) convertToNum 38: { 39:! if(denominator != 0) 40:! ! return (double) numerator/denominator; 41:! else 42:! ! return NAN; 43: } 51:! Fraction * aFraction = [[Fraction alloc] init]; 52:! Fraction * bFraction = [[Fraction alloc] init]; 53:! 54:! [aFraction setNumerator: 1];! /1st fraction is 1/4 / 55:! [aFraction setDenominator: 4]; 56: 57:! [aFraction print]; 58:! NSLog(@" ="); 1/4 59:! NSLog(@" %g", [aFraction convertToNum]); = 60: 0.25 61:! [bFraction print];! / never assigned a value / 0/0 62:! NSLog(@" ="); = 63:! NSLog(@" %g", [bFraction convertToNum]); nan 64:! ! 65:! [aFraction release]; 66:! [bFraction release]; Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 7. 7 Triangular Number Example Program 6.3, Program 6.4 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06: 07:! int number_to_test, remainder; 08: 09:! NSLog(@"Enter your number to ne tested: "); 10: 11:! scanf("%i", &number_to_test); 12: 13:! remainder = number_to_test % 2; 14: 15:! if( remainder == 0 ) 16: ! ! NSLog(@"The number is even."); if( remainder == 0 ) 17: ! ! NSLog(@"The number is even."); 18:! if( remainder != 0) ! else 19:! ! NSLog(@"The number is odd."); ! ! NSLog(@"The number is odd."); 20: 21:! [pool drain]; 22:! return 0; 23: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 8. 8 Compound Relational Test Logical Operator Descriptions && AND || OR ! NOT if ( grade > 70 && grade <= 79 ) ++grades_70_to79; if ( index < 0 || index > 99 ) NSLog (@”Error - index out of range”); Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 9. 9 Compound Relational Tests Program 6.5 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06: 07:! int year, rem_4, rem_100, rem_400; 08: 09:! NSLog(@"Enter the year to be tested: "); 10: 11:! scanf("%i", &year); 12: 13:! rem_4 = year % 4; 14:! rem_100 = year % 100; 15:! rem_400 = year % 400; 16: 17:! if( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 ) 18:! ! NSLog(@"It's a leap year."); 19:! else 20:! ! NSLog(@"Nope, It's not a leap year."); 21: 22:! [pool drain]; 23:! return 0; 24: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 10. 10 Nested if Statements if ( [chessGame isOver] == NO ) if ( [chessGame whoseTurn] == YOU ) [chessGame yourMove]; if ( [chessGame isOver] == NO && [chessGame whoseTurn] == YOU ) [chessGame yourMove]; Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 11. 11 Nested if Statements if ( [chessGame isOver] == NO ) if ( [chessGame whoseTurn] == YOU ) [chessGame yourMove]; else [chessGame myMove]; if ( [chessGame isOver] == NO ) if ( [chessGame whoseTurn] == YOU ) [chessGame yourMove]; else [chessGame myMove]; else [chessGame finish]; Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 12. 12 Dangling Else if ( [chessGame isOver] == NO ) if ( [chessGame isOver] == NO ) if ( [chessGame whoseTurn] == YOU ) if ( [chessGame whoseTurn] == YOU ) [chessGame yourMove]; [chessGame yourMove]; else else [chessGame finish]; [chessGame finish]; if ( [chessGame isOver] == NO ) { if ( [chessGame whoseTurn] == YOU ) [chessGame yourMove]; } else [chessGame finish]; Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 13. 13 The else if Construct if ( expression_1 ) if ( expression_1 ) program_statement_1 program_statement_1 else else if ( expression_2 ) if ( expression_2 ) program_statement_2 program_statement_2 else else program_statement_3 program_statement_3 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 14. 14 The else if Construct Program 6.6 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04:{ 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06: 07:! int number, sign; 08: 09:! NSLog(@"Please type in a number: "); 10:! scanf("%i", &number); 11: 12:! if (number <0) 13:! ! sign = -1; 14:! else if ( number == 0 ) 15:! ! sign = 0; // Must be positive 16:! else 17:! ! sign = 1; 18:! 19:! NSLog(@"Sign = %i", sign); 20: 21:! [pool drain]; 22:! return 0; 23: Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 15. 15 The else if Construct Program 6.7 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06: 07:! char c; 08: 09:! NSLog(@"Enter a single character: "); 10:! scanf(" %c", &c); 11: 12:! if (( c>= 'a' && c<='z') || (c>='A' && c<='Z')) 13:! ! NSLog(@"It's a alphabetic character."); 14:! else if (c >='0' && c<='9') 15:! ! NSLog(@"It's a digit."); 16:! else 17:! ! NSLog(@"It's a special character."); 18: 19:! [pool drain]; 20:! return 0; 21: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 16. 16 The else if Construct Program 6.8, Program 6.8A 01: int main(int argc, const char * argv[]) 02: { 03:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 04:! double! value1, value2; 05:! char! ! operator; 06:! Calculator * deskCalc = [[Calculator alloc] init]; 07:! NSLog(@"Type in your expression. "); 08:! scanf("%lf %c %lf", &value1, & operator, &value2); 09: 10:! [deskCalc setAccumulator: value1]; 11:! if (operator == '+') 11:! ! if (operator == '+') 12:! ! [deskCalc add: value2]; 12:! ! ! [deskCalc add: value2]; 13:! else if (operator == '-') 13:! ! else if (operator == '-') 14:! ! [deskCalc subtract: value2]; 14:! ! ! [deskCalc subtract: value2]; 15:! else if (operator == '*') 15:! ! else if (operator == '*') 16:! ! [deskCalc multiply: value2]; 16:! ! ! [deskCalc multiply: value2]; 17:! else if (operator == '/') 17:! ! else if (operator == '/') 18:! ! [deskCalc divide: value2]; 18:! ! ! if( value2 == 0) 19:! 19:! ! ! ! NSLog(@"Division by zero."); 20:! NSLog(@"%.2f", [deskCalc accumulator]); 20:! ! ! else! 21: 21:! ! ! ! [deskCalc divide: value2]; 22:! [deskCalc release]; 22:! ! else 23:! [pool drain]; 23:! ! ! NSLog(@"Unknown operator."); 24:! return 0; 25: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 17. 17 The switch Statement if ( expression == value1) switch ( expression ) { { program statement; case value1: program statement; program statement; … program statement; } … else if ( expression == value2) break; { case value2: program statement; program statement; program statement; program statement; … … } break; else if ( expression == value3) case value3: { program statement; program statement; program statement; program statement; … … break; } default: else program statement; { program statement; program statement; … program statement; break; … } } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 18. 18 The else if Construct Program 6.8, Program 6.9 03:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 04:! double! value1, value2; 05:! char! ! operator; 06:! Calculator * deskCalc = [[Calculator alloc] init]; 07:! NSLog(@"Type in your expression. "); 08:! scanf("%lf %c %lf", &value1, & operator, &value2); 09: 10:! ! if (operator == '+') 10:! ! switch (operator) { 11:! ! ! [deskCalc add: value2]; 11:! ! ! ! case '+': 12:! ! else if (operator == '-') 12:! ! ! ! ! [deskCalc add: value2]; 13:! ! ! [deskCalc subtract: value2]; 13:! ! ! ! ! break; 14:! ! else if (operator == '*') 14:! ! ! ! case '-': 15:! ! ! [deskCalc multiply: value2]; 15:! ! ! ! ! [deskCalc subtract: value2]; 16:! ! else if (operator == '/') 16:! ! ! ! ! break; 17:! ! ! if( value2 == 0) 17:! ! ! ! case '*': 18:! ! ! ! NSLog(@"Division by zero."); 18:! ! ! ! ! [deskCalc multiply: value2]; 19:! ! ! else! 19:! ! ! ! ! break; 20:! ! ! ! [deskCalc divide: value2]; 20:! ! ! ! case '/': 21:! ! else 21:! ! ! ! ! [deskCalc divide: value2]; 22:! ! ! NSLog(@"Unknown operator.");! 22:! ! ! ! ! break; 23:! NSLog(@"%.2f", [deskCalc accumulator]); 23:! ! ! ! default: 24: 24:! ! ! ! ! NSLog(@"Unknown operator."); 25:! [deskCalc release]; 25:! ! ! } 26:! [pool drain]; Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 19. 19 Boolean Variable Program 6.10, Program 6.10A 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06: 07:! int p, d, isPrime; 08:! 09:! for (p =2; p<=50; ++p){ 08:! BOOL isPrime; 10:! ! isPrime =1; 09:! ! 11:! ! 10:! ! for (p =2; p<=50; ++p){ 12:! ! for ( d = 2; d < p; ++d) 11:! ! ! isPrime = YES; 13:! ! ! if ( p % d == 0) 12:! ! ! 14:! ! ! ! isPrime = 0; 13:! ! ! for ( d = 2; d < p; ++d) 15:! ! ! ! 14:! ! ! ! if ( p % d == 0) 16:! ! if( isPrime != 0 ) 15:! ! ! ! ! isPrime = NO; 17:! ! ! NSLog(@"%i ", p); 16:! ! ! ! ! 18:! } 17:! ! ! if( isPrime == YES ) 19:! 18:! ! ! ! NSLog(@"%i ", p); 20:! [pool drain]; 19:! ! } 21:! return 0; 22: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language