Questions tagged [increment]

DO NOT USE THIS TAG ALONE. Use with a language tag like [javascript] or [python]. Adding one to the value of a variable, generally with the use of an increment operator.

Adding one to the value of a variable, generally with the use of an increment operator.

3520 questions
11
votes
5 answers

How exactly does the ?: operator work in C?

I have a question, how the compiler operate on the following code: #include int main(void) { int b=12, c=11; int d = (b == c++) ? (c+1) : (c-1); printf("d = %i\n", d); } I am not sure why the result is ‍‍‍d = 11.
J0S
  • 145
  • 7
11
votes
4 answers

Relative performance of x86 inc vs. add instruction

Quick question, assuming beforehand mov eax, 0 which is more efficient? inc eax inc eax or add eax, 2 Also, in case the two incs are faster, do compilers (say, the GCC) commonly (i.e. w/o aggressive optimization flags) optimize var += 2 to…
11
votes
8 answers

Simple function that returns a number incremented by 1 on each call, without globals?

I am trying to write a python function that on the first call, returns a 1. On the second call, returns a 2. On the third, a 3. Etc. Currently, I have achieved this using a global variable: index = 0 def foo(): global index index += 1 …
manocormen
  • 747
  • 1
  • 6
  • 17
11
votes
4 answers

Java create a unique ID for each instantiated object using instance methods instead of class/static methods

Quite new to this so I hope I have the terminology in the title right. I am trying to figure out how to create an instance method that will do the following: --An ID number is returned. --As each object is created from the class…
SeesSound
  • 503
  • 4
  • 13
  • 24
11
votes
3 answers

=+ Python operator is syntactically correct

I accidentally wrote: total_acc =+ accuracy instead of: total_acc += accuracy I searched the net and could not find anything. So what happened, why does Python think I mean what I am typing? Computers trust us too much. :)
gsamaras
  • 71,951
  • 46
  • 188
  • 305
11
votes
5 answers

Increment a value from AAA to ZZZ with cyclic rotation

I need to code a method that increment a string value from AAA to ZZZ with cyclic rotation (next value after ZZZ is AAA) Here is my code: public static string IncrementValue(string value) { if (string.IsNullOrEmpty(value) || value.Length…
fxkim
  • 363
  • 1
  • 5
  • 11
11
votes
3 answers

Does C# ++ operator become threadsafe in foreach loop?

Recently I moved from VB to C#, so I often use a C# to VB.NET converter to understand syntax differences. While moving next method to VB I noticed an interesting thing. C# original code: public bool ExceedsThreshold(int threshold, IEnumerable
AsValeO
  • 2,859
  • 3
  • 27
  • 64
11
votes
3 answers

A race condition when using Redis command incr and expire

Based on the redis document: http://redis.io/commands/incr In the paragraph Pattern: Rate Limiter 2 A shorter version code: value = INCR(ip) IF value == 1 THEN EXPIRE(ip, 1) It's claimed there's a race condition to make EXPIRE never executes.…
David Hsu
  • 113
  • 1
  • 1
  • 7
11
votes
3 answers

Why is x++-+-++x legal but x+++-+++x isn't?

I'm wondering why in C# the following is fine: int y = x++-+-++x; But int y = x+++-+++x; Isn't? Why is there a bias against the +?
user1968525
  • 121
  • 5
11
votes
3 answers

How to increment number using animate with comma using jQuery?

I'm trying to increment a number inside an element on page. But I need the number to include a comma for the thousandth place value. (e.g. 45,000 not 45000)