Upcoming SlideShare
×

# OOP Chapter 6: Making Decisions

674 views

Published on

Chapter 6 : Making Decisions

Published in: Education, Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

Views
Total views
674
On SlideShare
0
From Embeds
0
Number of Embeds
99
Actions
Shares
0
11
0
Likes
1
Embeds 0
No embeds

No notes for slide

### OOP Chapter 6: Making Decisions

1. 1. Object-Oriented Programming Language Chapter 6 : Making Decisions Atit Patumvan Faculty of Management and Information Sciences Naresuan University
2. 2. 2 Contents • The if statement • The switch statement • The conditional operatorAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
3. 3. 3 The if Statement if ( expression ) program_statement [ expression ] [ !expression ] program_statement if ( it is not raining ) i will go to swimmingAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
4. 4. 4 The if StatementProgram 6.101: #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; -10019: } The absolute value is 100 Type in your number: 2000 The absolute value is 2000Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
5. 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. 6 The if StatementProgram 6.237: -(double) convertToNum38: {39:! if(denominator != 0)40:! ! return (double) numerator/denominator;41:! else42:! ! 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/459:! NSLog(@" %g", [aFraction convertToNum]); =60: 0.2561:! [bFraction print];! / never assigned a value / 0/062:! NSLog(@" ="); =63:! NSLog(@" %g", [bFraction convertToNum]); nan64:! !65:! [aFraction release];66:! [bFraction release];Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
7. 7. 7 Triangular Number ExampleProgram 6.3, Program 6.401: #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) ! else19:! ! 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. 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. 9 Compound Relational TestsProgram 6.501: #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(@"Its a leap year.");19:! else20:! ! NSLog(@"Nope, Its 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. 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. 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 ﬁnish];Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
12. 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 ﬁnish]; [chessGame ﬁnish]; if ( [chessGame isOver] == NO ) { if ( [chessGame whoseTurn] == YOU ) [chessGame yourMove]; } else [chessGame ﬁnish];Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
13. 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_3Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
14. 14. 14 The else if ConstructProgram 6.601: #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 positive16:! else17:! ! 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. 15 The else if ConstructProgram 6.701: #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(@"Its a alphabetic character.");14:! else if (c >=0 && c<=9)15:! ! NSLog(@"Its a digit.");16:! else17:! ! NSLog(@"Its 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. 16 The else if ConstructProgram 6.8, Program 6.8A01: 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:! ! else23:! [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. 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. 18 The else if ConstructProgram 6.8, Program 6.903:! 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. 19 Boolean VariableProgram 6.10, Program 6.10A01: #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