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
48
votes
2 answers

SQL atomic increment and locking strategies - is this safe?

I have a question about SQL and locking strategies. As an example, suppose I have a view counter for the images on my website. If I have a sproc or similar to perform the following statements: START TRANSACTION; UPDATE images SET counter=counter+1…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
47
votes
6 answers

Is incrementing a field in MySQL atomic?

I'm making a web site where I would like to increment a counter in a standard MyISAM table. Simplified example: UPDATE votes SET num = num + 1; Will this cause problems if multiple connections are doing the same query, or will MySQL take care of it…
Newb
  • 513
  • 1
  • 4
  • 7
46
votes
3 answers

Unintuitive expression evaluation with incrementation

For the following code
Daniel
  • 7,684
  • 7
  • 52
  • 76
46
votes
9 answers

post increment vs pre increment - Javascript Optimization

I was browsing Google Code when I chanced upon this project called JSpeed - optimization for Javascript. I noticed one of the optimization was to change i++ to ++i in for loop statements. Before Optimization for (i=0;i<1;i++) {} for (var i = 0, j =…
mauris
  • 42,982
  • 15
  • 99
  • 131
46
votes
7 answers

Python dictionary increment

In Python it's annoying to have to check whether a key is in the dictionary first before incrementing it: if key in my_dict: my_dict[key] += num else: my_dict[key] = num Is there a shorter substitute for the four lines above?
Paul S.
  • 4,362
  • 10
  • 35
  • 52
42
votes
3 answers

.increment vs += 1

I have a Picture model that contains a variable for a view count (integer). The view count is incremented by +1 every time someone views the Picture object. In getting this done, what is the difference between @picture.view_count += 1 …
James Pleasant
  • 707
  • 1
  • 6
  • 12
40
votes
9 answers

Increment columns in laravel

Is there a way to increment more than one column in laravel? Let's say: DB::table('my_table') ->where('rowID', 1) ->increment('column1', 2) ->increment('column2', 10) ->increment('column3', 13) ->increment('column4', 5); But this results to: Call…
Vainglory07
  • 5,073
  • 10
  • 43
  • 77
39
votes
4 answers

i = i++ doesn't increment i. Why?

Possible Duplicates: Why does this go into an infinite loop? Things like i = i++ have undefined behavior in C and C++ because the value of a scalar object is changes twice within the same expression without intervening sequence point. However I…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
38
votes
6 answers

How to increment a number by 2 in a PHP For Loop

The following is a simplified version of my code:

I want the loop to run 8 times and I want the number in the first paragraph to increment…
user2586455
  • 591
  • 2
  • 7
  • 16
36
votes
3 answers

Reliability of atomic counters in DynamoDB

I was considering to use Amazon DynamoDB in my application, and I have a question regarding its atomic counters reliability. I'm building a distributed application that needs to concurrently, and consistently, increment/decrement a counter stored in…
Mark
  • 67,098
  • 47
  • 117
  • 162
32
votes
1 answer

How do I increment or decrement a number in Common Lisp?

What is the idiomatic Common Lisp way to increment/decrement numbers and/or numeric variables?
Jabavu Adams
  • 2,348
  • 3
  • 18
  • 16
32
votes
8 answers

Difference between ++ and +=1 in javascript

Could somebody explain why the following functions give different results. The first does not seem to work, but the second does. I'm puzzled because I thought +=1 and ++ did the same thing. (I'm not intending to actually use this code, it's just…
RobinL
  • 11,009
  • 8
  • 48
  • 68
31
votes
1 answer

Need synchronization for an increment-only counter?

I use an integer as counter. The integer will only be increased, and surely more than one thread will increase it at the same time. The value of this counter is read at the end of program execution when no other thread will try to access its value.…
qinsoon
  • 1,433
  • 2
  • 15
  • 34
31
votes
4 answers

Increment operator is not invoked at sizeof(++n) expression

In C or C++, increment and decrement operator (++n, --n) are not performed when it is in a sizeof() operator. int n = 100; int size_int = sizeof(++n); std::cout<
wizardyk
  • 321
  • 2
  • 7
29
votes
5 answers

How do I increment a java.sql.Timestamp by 14 days?

I have an app that takes a Timestamp as a boundary for the start date and end date of a sql selection, I want to populate a hashmap with weeks this year since the first monday of the year as the values and the week number as the keys. I'm finding…
davidahines
  • 3,976
  • 16
  • 53
  • 87