Search code examples
objective-cfor-loopcycle

Display a count of a cycle


I have three variables, one is called A = 2012, the second is called B = 2020 and the third is C = B - A. C is the difference of years. Now I would like to see on the screen the years between two dates: 2012 2013 2014 2015 2016 2017 2018 2019 2020. I tried different solutions, but I can not do it, I think that the right approach is a cycle. Is it right? Could you show me how to do this? Thank you.


Solution

  • I assume you mean loop with cycle? You could try this:

    int a = 2012;             // Holds the first year
    int b = 2020;             // Holds the last year
    int c = b - a;            // Holds the difference of the a and b
    while(a<=b) {             // Execute this as long as a is equal or less then b
        NSLog("Year: %d", a); // Print the year to the console
        a++;                  // Increment the value of a with 1
    }