Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Looping for ( init_expression; loop_condition; loop_expression ) program statement #import int main (int argc, const char * argv[]) { int i, sum; sum = 0; for ( i = 0; i < 100; i = i + 1 ) sum += i; NSLog sum of %i", sum); return 0; } :02: prog2[3216:707] The sum of i i++ --i i--
== is different from = Relational operators have lower precedence than all arithmetic operators
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.
Keyboard input #import int main (int argc, const char * argv[]) { int number; NSLog number"); scanf ("%i", &number); NSLog number was %i\n", number); return 0; } :30: prog2[3816:707] Input number :30: prog2[3816:707] Input number was 3
The while Statement while ( expression ) program statement #import int main (int argc, const char * argv[]) { int count = 1; while ( count <= 5 ) { NSLog count); ++count; } return 0; } [Switching to process 3867 thread 0x0] :35: prog2[3867:707] :35: prog2[3867:707] :35: prog2[3867:707] :35: prog2[3867:707] :35: prog2[3867:707] 5
The do statement do program statement while ( expression ); #import int main (int argc, const char * argv[]) { int i; NSLog your number."); scanf ("%i", &i); do { NSLog i); i -= 1; } while ( i != 0 ); } [Switching to process 3968 thread 0x0] :45: prog2[3968:707] Enter your number :45: prog2[3968:707] :45: prog2[3968:707] :45: prog2[3968:707] :45: prog2[3968:707] :45: prog2[3968:707] 1
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;
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.
The if Statement if ( expression ) program statement The if-else Statement if ( expression ) program statement else program statement
Compound Relational Tests #import int main (int argc, const char * argv[]) { int score = 85; if ( score >= 80 && score <= 90 ) is B\n"); } :01: prog2[4094:707] grade is B || is for ‘or’
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[]) { int number, sign; NSLog type in a number: "); scanf ("%i", &number); if ( number < 0 ) sign = -1; else if ( number == 0 ) sign = 0; else sign = 1; NSLog = %i", sign); // Must be positive 1; } prog2[4155:707] Please type in a number: 5 prog2[4155:707] Sign = 1
The switch Statement switch ( expression ) { case value1: program statement... break; case value2: program statement... break;... case valuen: program statement... break; default: program statement... break; }
#import int main (int argc, const char * argv[]) { int n; printf("Please enter a number: "); scanf("%d", &n); switch (n) { case 1: { is equal to 1!\n"); break; } case 2: { is equal to 2!\n"); break; } case 3: { is equal to 3!\n"); break; } default: { isn't equal to 1, 2, or 3.\n"); break; } Please enter a number: :27: prog2[4298:707] n isn't equal to 1, 2, or 3.
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 ", p); } [Switching to process 4354 thread 0x0] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] :34: prog2[4354:707] 47
The Conditional Operator condition ? expression1 : expression2 s = ( x < 0 ) ? -1 : x * x;