MHR
MHR

Reputation: 53

How to loop in cyclic order?

Suppose:

int id = 2;
for (int i = 0; i < 4; i++) {
    System.out.println(i);
}

This outputs in the order 0, 1, 2, 3.

How can I make it start at id and wrap around to give output 2, 3, 0, 1?

Upvotes: 0

Views: 1968

Answers (7)

Sunny Pandey
Sunny Pandey

Reputation: 11

     int id=2;
        for (int i = id;  i = id - 1; i = (i + 1) % 4)  {
         System.out.println(i);
        }

Upvotes: 1

Amit Kumar Shrivastava
Amit Kumar Shrivastava

Reputation: 510

  import java.util.*;

  class Main {

  public static void main (String[] args) throws Exception {

  int id = 2;
  int limit=4;

  for(int i = id; i < limit;i++){

      System.out.println(i);

       if (i==3){
        i=-1;
        limit=2;
       }

     }
    }
   }

This is exactly what you want to achieve.

Here i've assign -1 to i because of i++.

Upvotes: -1

Boann
Boann

Reputation: 50061

A do-while loop provides a very raw solution, a smidgin faster than using modulo:

int i = id;
do {
    System.out.println(i);      
    if (++i == 4) i = 0;
} while (i != id);

Upvotes: 0

Lars Kristensen
Lars Kristensen

Reputation: 1495

This will do what you want, but I doubt it will be a usable approach in practice.

import java.util.*;

class Main {
    public static void main (String[] args) throws Exception {
        int[] indexArray = {2, 3, 0 ,1}; //An array that holds the order you want to use
        int id = 2;
        for(int i = 0; i < indexArray.length; i++) {
            int j = indexArray[i];
            if (j==id) {
                System.out.println(j);
            } else {
                System.out.println(j);
            }
        }
    }
}

What exactly are you trying to achieve?

Upvotes: -1

pri
pri

Reputation: 1531

Are you looking for something like this?


import java.util.*;
class Main {
  public static void main (String[] args) throws Exception {

int id = 2;
for(int i = id; i < 4; i++)
      System.out.println(i); 
for(int i = 0; i < id; i++)
      System.out.println(i);
  }
}

Upvotes: 0

sestus
sestus

Reputation: 1927

General Solution: use modulo:

import java.util.*;
class Main {
    public static void main (String[] args) throws Exception {

        int id = 2;
        int size = 4;
        for(int i = id; i < size + id; i++){
            System.out.println(i % size);
        }
    }
}

Upvotes: 0

LordAnomander
LordAnomander

Reputation: 1123

One possibility is to be "creative" and use mathematical operators.

int id = 2;
for (int i = 0; i < 4; i++) {
    System.out.println((id + i) % 4); // 2 + 0 % 4 = 2, 2 + 1 % 4 = 3, 2 + 2 % 4 = 0
}

Instead of using 4 as static number, you can also define a variable, e.g.,

int id = 2;
for (int i = 0; i < n; i++) {
    System.out.println((id + i) % n);
}

Which will always print a cyclic order. For example, 2, 3, 4, 5, 0, 1, ... for n = 6.

Another approach would be to define an array

int[] numbers = new int[] {2, 3, 0, 1};

and iterate over it.

Upvotes: 9

Related Questions