简体   繁体   中英

Jump forward and backwards in for loop

I have some problems figuring out how to implement a for loop in C++(14). It's a normal for loop:

for (j = 65; j < 91; ++j) {
    body...
}

For the first iteration j will be 65. For the second iteration, I want it to be increased by 32 (second iteration j = 97). For the third, it has to go back to 65 + 1 => 66. Basically, j values will be like this: 65, 97, 66, 98, 67, 99, 68, 100, 69, 101... and so on. I want the code to be minimal when talking about character used.

PS: If it's helpful, those values are the ASCII value for A, a, B, b, C, c, D, d, F, f...

TL;DR: I wanna iterate through those integers/chars in as less code as possible.

Thank you.

Just use two iteration variables

for(auto lo=65, hi=lo+32; lo!=91; ++lo,++hi) {
  /* use lo */
  /* use hi */
}

The compiler may optimize either or both of lo and hi away. Otherwise, you can add to a single iteration variable

void work(int);
for(auto j=65; j!=91; ++j) {
  work(j);
  work(j+32);
}

just iterate over those values:

for(auto ch : {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'F', 'f'}) {
   /* use ch */
}

There is no need to jump back and forth. Iterate through one range of characters and use toupper / tolower to go the the opposite character. For example

for (char ch = 'A'; ch <= 'Z'; ch++)
{
    char lower = tolower(ch);
    //use ch
    //use lower
}

As long as 'A' to 'Z' is a contiguous range this will work just fine. If you do not have a contiguous range you can use something like

for (char ch = std::numeric_limits<char>::min(); ch <= std::numeric_limits<char>::max(); ch++)
{
    if(isalpha(ch) && isupper(ch))
    {
        char lower = tolower(ch);
        //use ch
        //use lower
    }
}

Hint: use ^=

for (j = 65; j < 91; ++j,j^=' ') {
    body...
}

consider that on first iteration. consider fact on first iteration (i=0) you have pair of values (current=65, next=97), on second (current=97, next = 68)

for(char c = 65, n=97; c+n<=213; ++c,c^=n,n^=c,c^=n)
   body

shortening it

  for(char c = 65, n=97; n<123; swap(c,n), c++)

or

  for(char c = 65, n=97; n!=91; swap(c,n), c++)

binary representation of 65 is 100 0001, of 97 - 101 0001, and so on, so even steps can be created from odd ones by bitwise or c ^ 32. increments must be done only on odd steps. if we assume, that implicit cast of boolean true to integral value yield 1, it looks so

  for(char c = 65; c!=91; c^=32, c+=c<91)

if above not true

  for(char c = 65; c!=91; c^=32, c+=1-c/91)

just equally short solution, that uses preliminary step of for loop and fact that assignment operator returns value

  for(char c = 64; (c+=c<91)!=91; c^=32)

NB. NEVER write your loops like that unless you want everyone who reads code to hate you

Your goal is short. I believe this wins.

for (j=65;j!=91;(j^=' '),j+=(j<91))

live example .

This may help you!

for (int i = 'A', j = 'a'; i <= 'Z'; i++, j++)
{
    // Body
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM