0

This is a simple one (I guess)

I just wondered if there is any way to make a variable (Let's say int ) cycle through a range? Right now this is what I would do:

int someInt = 0, max = 10; 
while(1) // This loop is here just for increasing someInt
{
  someInt++;
  if (someInt >= max)
    someInt = 0;
}

Isn't there any other trick to reset someInt? maybe without using if?

Thanks!

Gelgavish
  • 21
  • 2
  • This is simple, but probably best answered by suggesting you read any decent introductory text. Look for information on the `for` loop. – Peter Dec 26 '19 at 12:06
  • What is wrong with the code that you posted? I mean how it behaves and how it differs from what you expect? – Öö Tiib Dec 26 '19 at 12:07
  • What is the condition to end looping? – Galik Dec 26 '19 at 12:10
  • 2
    Yes, I daresay there are infinite approaches that solve this problem; Most simple example that I can think of is modulo operator; – pptaszni Dec 26 '19 at 12:12
  • The modulo operator approach worked for me, if I want the limited value I just need to do ```int otherInt = someInt % max``` (in the loop), thanks for the help! – Gelgavish Dec 26 '19 at 12:21

3 Answers3

2

Just use the remainder operator (%).

The binary operator % yields the remainder of the integer division of the first operand by the second (after usual arithmetic conversions; note that the operand types must be integral types).

int someInt = 0, max = 10; 
while(1) // This loop is here just for increasing someInt
{
  someInt = (someInt + 1) % max;
}
Doeus
  • 430
  • 1
  • 3
  • 7
1

Here I would simply use a for loop inside the while loop:

int max = 10;

while(true) for(int someInt = 0; someInt < max; ++someInt)
{
    // do stuff here
}
Galik
  • 47,303
  • 4
  • 80
  • 117
0

you can simply use mod operator.

int someInt = 0, max = 10; 
for(int i = 0 ; i < 100 ; i++) // This loop is here just for increasing someInt
{
  ++someInt;
  someInt = someInt % max;
  cout<<someInt;
}
Aqeel Ahmad
  • 709
  • 7
  • 20
  • 3
    This is undefined behavior, and a bug. – Sam Varshavchik Dec 26 '19 at 12:28
  • because the loop is infinite. @Gelgavish did this according to his logic. I don't know about that. – Aqeel Ahmad Dec 26 '19 at 12:34
  • I just updated my answer. Now this error will not occur. this loop will run only for 100 times. – Aqeel Ahmad Dec 26 '19 at 12:36
  • 1
    I think he refered to the assignment and not the loop. See: https://stackoverflow.com/a/367671/11802207 – Doeus Dec 26 '19 at 12:46
  • This is still undefined behavior, and the same bug. – Sam Varshavchik Dec 26 '19 at 13:01
  • this is working perfectly fine for me. there must be some other problem. can you share the complete code of your file? – Aqeel Ahmad Dec 26 '19 at 13:09
  • "Undefined behavior" means that, as compiled, the code might work today, but fail and crash tomorrow. Just because it "is working perfectly fine for" you doesn't make the bug go away. If you don't understand what the problem is, try actually reading the links that were already given above. One of them gives pretty much the same code as yours, and explains what the bug is. – Sam Varshavchik Dec 26 '19 at 13:26
  • I've updated the answer. this will not even give you a warning. – Aqeel Ahmad Dec 26 '19 at 13:42