-7

This piece of code is inside shake event's scope,

if(i % 3) {
    self.Label.text = [NSString stringWithFormat:@"%d", y];
    y += 1;        
}

whenever i reaches 3,
y += 1 executes.

When I shake it again,

y value adds another 1
i is now equal to 4

So now i = 4 and y = 2, I get the idea why y is adding another 1 but I just can't figure out how to avoid it, please help.

zaph
  • 111,848
  • 21
  • 189
  • 228
shaideru
  • 45
  • 1
  • 6

3 Answers3

6

Try if (i % 3 == 0). By using i % 3, the piece of code will execute whenever i is not divisible by 3 rather than only when i is divisible by 3.

JAB
  • 20,783
  • 6
  • 71
  • 80
1

Probably you are looking for this:

if(i % 3  == 0){

        self.Label.text = [NSString stringWithFormat:@"%d", y];
        y += 1;        
}

This code will increase y only every 3rd iteration (or shake or whatever you are doing)

Oleg Fedorov
  • 337
  • 3
  • 10
  • That did it! thank you so much! I changed the 0 to 1 though :) – shaideru Jan 30 '14 at 13:24
  • @frustratediOSdeveloper That will cause the code to execute when `i-1` is divisible by 3. It will still occur only once every three times, but there will be a shift in when the execution occurs. (Which may be desired behavior if `i` starts at 0 before any shakes, increases by 1 for each shake, and you want `y+=1` to start with the first shake rather than the third, I suppose.) – JAB Jan 30 '14 at 14:27
0

The correct way can you try this:

if(i % 3 == 0) {
    NSLog(@"Value of i : %d and Y is : %@", i, [NSString stringWithFormat:@"%d", y]);
    self.Label.text = [NSString stringWithFormat:@"%d", y];
    y += 1;        
} else {
    NSLog(@"Value of i : %d and Y is : %@", i, [NSString stringWithFormat:@"%d", y]);
}
kagmanoj
  • 5,038
  • 5
  • 19
  • 21