Monday, August 31, 2009

looping and decision making

the for loop

#import <Foundation/Foundation.h>

int main(int char agrc,char *argv)

{

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

int number,rightmost;                                                          NSLog(@”enter your number”);                                        scanf(“%i”,&number);                                                         for(;number!=0;number/=10)                                   {rightmost=number%10;}                                                        NSLog(@”%i”,rightmost); 

}
[pool drain];
return 0;

 

}

 

the while loop

// Program to reverse the digits of a number
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, right_digit;
NSLog (@”Enter your number.”);
scanf (“%i”, &number);
while ( number != 0 ) {
right_digit = number % 10;
NSLog (@”%i”, right_digit);
number /= 10;
}
[pool drain];
return 0;
}

the do while loop

// Program to reverse the digits of a number
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, right_digit;
NSLog (@”Enter your number.”);
scanf (“%i”, &number);
do {
right_digit = number % 10;
NSLog (@”%i”, right_digit);
number /= 10;
}
while ( number != 0 );
[pool drain];
return 0;
}

//output

Enter your number.
1456
6
5
4
1

the break and continue

i am not going to explain these thing same as “C”

u can find these else where as well million of site to help u in loop

if loop 

// Program to determine if a number is even or odd
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number_to_test, remainder;
NSLog (@”Enter your number to be tested: “);
scanf (“%i”, &number_to_test);
remainder = number_to_test % 2;
if ( remainder == 0 )
NSLog (@”The number is even.”);
if ( remainder != 0 )
NSLog (@”The number is odd.”);
[pool drain];
return 0;
}

if –else loop

// Program to determine if a number is even or odd
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number_to_test, remainder;
NSLog (@”Enter your number to be tested: “);
scanf (“%i”, &number_to_test);
remainder = number_to_test % 2;
if ( remainder == 0 )
NSLog (@”The number is even.”);
else
NSLog (@”The number is odd.”);
[pool drain];
return 0;
}

if ( expression 1 )
program statement 1
else
if ( expression 2 )
program statement 2
else
program statement 3

 

// This program determines if a year is a leap year
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int year, rem_4, rem_100, rem_400;
NSLog (@”Enter the year to be tested: “);
scanf (“%i”, &year);
rem_4 = year % 4;
rem_100 = year % 100;
rem_400 = year % 400;
if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 )
NSLog (@”It’s a leap year.”);
else
NSLog (@”Nope, it’s not a leap year.”);
[pool drain];
return 0;
}

 

//Output


Enter the year to be tested:
1955
Nope, it’s not a leap year.
The if Statement 109
Program 6.5 Output (Rerun)
Enter the year to be tested:
2000
It’s a leap year.
Program 6.5 Output (Rerun)
Enter the year to be tested:
1800
Nope, it’s not a leap year.

Nested if Statements

// This program categorizes a single character
// that is entered from the keyboard
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
char c;
NSLog (@”Enter a single character:”);
scanf (“%c”, &c);
if ( (c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c <= ‘Z’) )
NSLog (@”It’s an alphabetic character.”);
else if ( c >= ‘0’ && c <= ‘9’ )
NSLog (@”It’s a digit.”);
else
NSLog (@”It’s a special character.”);
[pool drain];
return 0;
}

The switch Statement

// Program to evaluate simple expressions of the form
// value operator value
#import <Foundation/Foundation.h>
// Insert interface and implementation sections for
// Calculator class here
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
double value1, value2;
char operator;
Calculator *deskCalc = [[Calculator alloc] init];
NSLog (@”Type in your expression.”);
scanf (“%lf %c %lf”, &value1, &operator, &value2);
[deskCalc setAccumulator: value1];
switch ( operator ) {
case ‘+’:
[deskCalc add: value2];
break;
case ‘-’:
[deskCalc subtract: value2];
break;
case ‘*’:
[deskCalc multiply: value2];
break;
case ‘/’:
[deskCalc divide: value2];
break;
default:
NSLog (@”Unknown operator.”);
break;
}
NSLog (@”%.2f”, [deskCalc accumulator]);
[deskCalc release];
[pool drain];
return 0;
}

Boolean Variables

// Program to generate a table of prime numbers
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int p, d, isPrime;
for ( p = 2; p <= 50; ++p ) {
isPrime = 1;
for ( d = 2; d < p; ++d )
if ( p % d == 0 )
isPrime = 0;
if ( isPrime != 0 )
NSLog (@”%i “, p);
}
[pool drain];
return 0;
}

The Conditional Operator

condition ? expression1 : expression2