Question: I am trying to make a program that will continuously take a user input and determine if the user input is even or odd. The program will only stop when zero is entered.

However, when I enter zero, it `print`s `0 is an even number` followed by `All done!`. The expected output is just `All done!`. Here is my code:

```
def main():
    total = 0
    count = 0

    while True:
        entry=int(input('Enter a number or 0 to quit:'))
        if entry % 2 == 0:
            print (format (entry), 'is an even number.')
        else:
            print (format (entry),'is an odd number.')

        if entry == 0:
            print ('All done!')
            break
main()

```

Answer: 
1. Define a `NSTimer` instance and a counter in your view controller:

```
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger counter;

```
2. Implement the method when timer is fired:

```
- (void)timerAction:(id)sender {
    [_textField setText:@"hi"];
    NSString * text = [_textField text];

    // Update status.

    [[TSNAppContext singleton] updateStatus:text];

    // Add the status to the bubble.
    [self appendLocalPeerTableViewCellWithMessage:text];

    // stop the timer after sending for 300 times
    self.counter += 1;
    if (self.counter >= 300) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

```
3. Starts the timer when switch is on:

```
- (void)changeSwitch:(id)sender{
    if ([sender isOn]) {
        self.counter = 0;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    } else {
        // kill the timer when switch is off
        [self.timer invalidate];
        self.timer = nil;
    }
}

```

However, you can't make the timer to continue working indefinitely after your app is entering background (with some exceptions: VoIP, GPS applications, etc.). Please refer to the official document [Background Execution](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html).
Use `NSTimer`: [Documentation of NSTimer](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/)

Declare property of `NSTimer` in `.h` file:

```
@property (retain,nonatomic) NSTimer *myTimer;

- (void)changeSwitch:(id)sender
{
   if([sender isOn]){
   myTimer = [NSTimer scheduledTimerWithTimeInterval: 4 target: self
                                   selector: @selector(AddMessage:) userInfo: nil repeats: YES];

   } else{
    [self.timer invalidate];
   }
}

```

After each 4 second, timer will call the below function:

```
-(void) AddMessage:(NSTimer*) timer 
{ 
    //Add Message
}

```