Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee

Similar presentations


Presentation on theme: "Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee"— Presentation transcript:

1 Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee (lygu@gist.ac.kr)

2 Looping for ( init_expression; loop_condition; loop_expression ) program statement #import int main (int argc, const char * argv[]) { @autoreleasepool { int i, sum; sum = 0; for ( i = 0; i < 100; i = i + 1 ) sum += i; NSLog (@"The sum of 0..99 %i", sum); return 0; } 2012-03-22 18:02:29.847 prog2[3216:707] The sum of 0..99 4950 ++i i++ --i i--

3 == is different from = Relational operators have lower precedence than all arithmetic operators

4 Execution of the for statement 1. The initial expression is evaluated first. This expression usually sets a variable that is used inside the loop, generally referred to as an index variable, to some initial value (such as 0 or 1). 2. The looping condition is evaluated. If the condition is not satisfied (the expression is false), the loop immediately terminates. Execution continues with the program statement that immediately follows the loop. 3. The program statement that constitutes the body of the loop is executed. 4. The looping expression is evaluated. This expression is generally used to change the value of the index variable, frequently by adding 1 to it or subtracting 1 from it. 5. Return to step 2.

5 Keyboard input #import int main (int argc, const char * argv[]) { @autoreleasepool { int number; NSLog (@"Input number"); scanf ("%i", &number); NSLog (@"Input number was %i\n", number); return 0; } 2012-03-22 19:30:46.450 prog2[3816:707] Input number 3 2012-03-22 19:30:51.931 prog2[3816:707] Input number was 3

6 The while Statement while ( expression ) program statement #import int main (int argc, const char * argv[]) { @autoreleasepool { int count = 1; while ( count <= 5 ) { NSLog (@"%i", count); ++count; } return 0; } [Switching to process 3867 thread 0x0] 2012-03-22 19:35:52.934 prog2[3867:707] 1 2012-03-22 19:35:52.937 prog2[3867:707] 2 2012-03-22 19:35:52.937 prog2[3867:707] 3 2012-03-22 19:35:52.938 prog2[3867:707] 4 2012-03-22 19:35:52.938 prog2[3867:707] 5

7 The do statement do program statement while ( expression ); #import int main (int argc, const char * argv[]) { @autoreleasepool { int i; NSLog (@"Enter your number."); scanf ("%i", &i); do { NSLog (@"%i", i); i -= 1; } while ( i != 0 ); } [Switching to process 3968 thread 0x0] 2012-03-22 19:45:16.956 prog2[3968:707] Enter your number. 5 2012-03-22 19:45:21.317 prog2[3968:707] 5 2012-03-22 19:45:21.318 prog2[3968:707] 4 2012-03-22 19:45:21.319 prog2[3968:707] 3 2012-03-22 19:45:21.320 prog2[3968:707] 2 2012-03-22 19:45:21.321 prog2[3968:707] 1

8 The break Statement Sometimes when executing a loop, you’ll want to leave the loop as soon as a certain condition occurs—for instance, maybe you detect an error condition or find the data you’re looking for in a list of data. You can use the break statement for this purpose. Execution of the break statement causes the program to immediately exit from the loop it is executing, whether it’s a for, while, or do loop. Subsequent statements in the loop are skipped and execution of the loop is terminated. Execution continues with whatever statement follows the loop. If a break is executed from within a set of nested loops, only the innermost loop in which the break is executed is terminated. The format of the break statement is simply the keyword break followed by a semicolon, like so: break;

9 The continue Statement The continue statement is similar to the break statement, except that it doesn’t cause the loop to terminate. At the point that the continue statement is executed, any statements that appear after the continue statement up to the end of the loop are skipped. Execution of the loop otherwise continues as normal.

10 The if Statement if ( expression ) program statement The if-else Statement if ( expression ) program statement else program statement

11 Compound Relational Tests #import int main (int argc, const char * argv[]) { @autoreleasepool { int score = 85; if ( score >= 80 && score <= 90 ) NSLog(@"grade is B\n"); } 2012-03-22 20:01:26.503 prog2[4094:707] grade is B || is for ‘or’

12 The else if Construct if (expression 1 ) program statement 1 else if ( expression 2 ) program statement 2 else program statement 3 #import int main (int argc, const char * argv[]) { @autoreleasepool { int number, sign; NSLog (@"Please type in a number: "); scanf ("%i", &number); if ( number < 0 ) sign = -1; else if ( number == 0 ) sign = 0; else sign = 1; NSLog (@"Sign = %i", sign); // Must be positive 1; } prog2[4155:707] Please type in a number: 5 prog2[4155:707] Sign = 1

13 The switch Statement switch ( expression ) { case value1: program statement... break; case value2: program statement... break;... case valuen: program statement... break; default: program statement... break; }

14 #import int main (int argc, const char * argv[]) { @autoreleasepool { int n; printf("Please enter a number: "); scanf("%d", &n); switch (n) { case 1: { NSLog(@"n is equal to 1!\n"); break; } case 2: { NSLog(@"n is equal to 2!\n"); break; } case 3: { NSLog(@"n is equal to 3!\n"); break; } default: { NSLog(@"n isn't equal to 1, 2, or 3.\n"); break; } Please enter a number: 5 2012-03-22 20:27:40.776 prog2[4298:707] n isn't equal to 1, 2, or 3.

15 Boolean Variables A couple of built-in features in Objective-C make working with Boolean variables a little easier. One is the special type BOOL, which can be used to declare variables that will contain either a true or a false value. The other is the built-in values YES and NO. Using these predefined values in your programs can make them easier to write and read. #import int main (int argc, const char * argv[]) { int p, d; BOOL isPrime; for ( p = 2; p <= 50; ++p ) { isPrime = YES; for ( d = 2; d < p; ++d ) if ( p % d == 0 ) isPrime = NO; if ( isPrime == YES ) NSLog (@"%i ", p); } [Switching to process 4354 thread 0x0] 2012-03-22 20:34:21.558 prog2[4354:707] 2 2012-03-22 20:34:21.562 prog2[4354:707] 3 2012-03-22 20:34:21.563 prog2[4354:707] 5 2012-03-22 20:34:21.564 prog2[4354:707] 7 2012-03-22 20:34:21.564 prog2[4354:707] 11 2012-03-22 20:34:21.565 prog2[4354:707] 13 2012-03-22 20:34:21.565 prog2[4354:707] 17 2012-03-22 20:34:21.566 prog2[4354:707] 19 2012-03-22 20:34:21.566 prog2[4354:707] 23 2012-03-22 20:34:21.567 prog2[4354:707] 29 2012-03-22 20:34:21.567 prog2[4354:707] 31 2012-03-22 20:34:21.568 prog2[4354:707] 37 2012-03-22 20:34:21.568 prog2[4354:707] 41 2012-03-22 20:34:21.569 prog2[4354:707] 43 2012-03-22 20:34:21.569 prog2[4354:707] 47

16 The Conditional Operator condition ? expression1 : expression2 s = ( x < 0 ) ? -1 : x * x;


Download ppt "Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee"

Similar presentations


Ads by Google