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