Hi everyone. I'm just learning how to do Obj C (only programming experience I have is with NetLogo), and an exercise wants me to convert a for statement into a while statement. However, the "while" examples they give don't have a lot of info and I'm not quite sure what to do. Here is the for statement:
And here is what I came up with (yes, I know it's bad):
It doesn't have any errors, but it also doesn't ask for any input. Just runs and then exits. Can anyone tell me what I'm doing wrong?
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int n, number, triangularNumber, counter;
for (counter = 1; counter <= 5; ++counter )
{
NSLog (@"What Triangular number do you want?");
scanf("%i", &number);
triangularNumber = 0;
for (n = 1; n <= number; ++n )
triangularNumber += n;
NSLog (@"Triangular number %i is %i", number, triangularNumber);
}
[pool drain];
return 0;
}
And here is what I came up with (yes, I know it's bad):
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int n, number, triangularNumber, counter;
while (counter <= 5) {
NSLog (@"What Triangular number do you want?");
scanf("%i", &number);
triangularNumber = 0;
while (n <= number) {
triangularNumber += n;
NSLog (@"Triangular number %i is %i", number, triangularNumber);
++n,
++counter;
}}
[pool drain];
return 0;
}
It doesn't have any errors, but it also doesn't ask for any input. Just runs and then exits. Can anyone tell me what I'm doing wrong?