decrement

Decrement value in mysql but not negative

橙三吉。 提交于 2019-12-04 08:47:55
问题 I want to decrement a value when user delete it in php and mysql. I want to check not to go below than 0. If value is 0 then do not decrement . mysql_query("UPDATE table SET field = field - 1 WHERE id = $number"); If field is 0 then do not do anything 回答1: Add another condition to update only if the field is greater 0 UPDATE table SET field = field - 1 WHERE id = $number and field > 0 回答2: You could prevent the new value to drop below zero by using GREATEST(). If the value drops below zero,

Accessing a variable of a thread from another thread in java

六眼飞鱼酱① 提交于 2019-12-04 03:29:27
I'm trying to access and modify a variable of a thread in another thread in java, and I really don't know how to do this. ex : Runnable r1 = new Runnable() { int value = 10; public void run() { // random stuff } } Runnable r2 = new Runnable() { public void run() { // of course the bellow line will not work r1.value--; // I want here to be able to decrement the variable "value" of r1 } } Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); thank you if you have any idea! Is there any way to create a getter and setter for a thread in java? EDIT : the answers were good,

What are the historical reasons C languages have pre-increments and post-increments?

时光毁灭记忆、已成空白 提交于 2019-12-03 17:15:16
问题 (Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.) Developers of C (Dennis Ritchie et al) created increment and decrement operators for very good reasons. What I don't understand is why they decided to create the distinction of pre- vs post- increments/decrements? My sense is that these operators were far more useful when C was being developed than today. Most C/C++

What are the historical reasons C languages have pre-increments and post-increments?

半世苍凉 提交于 2019-12-03 06:16:41
(Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.) Developers of C (Dennis Ritchie et al) created increment and decrement operators for very good reasons. What I don't understand is why they decided to create the distinction of pre- vs post- increments/decrements? My sense is that these operators were far more useful when C was being developed than today. Most C/C++ programmers use one or the other, and programmers from other languages find the distinction today bizarre and

Decrementing for loops [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-03 06:11:50
问题 This question already has answers here : Loop backwards using indices in Python? (14 answers) Closed 3 years ago . I want to have a for loop like so: for counter in range(10,0): print counter, and the output should be 10 9 8 7 6 5 4 3 2 1 回答1: a = " ".join(str(i) for i in range(10, 0, -1)) print (a) 回答2: Check out the range documentation, you have to define a negative step: >>> range(10, 0, -1) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] 回答3: You need to give the range a -1 step for i in range(10,0,-1):

Why don't multiple decrement operators work in C when they work in C++?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 05:29:59
问题 Looking at this question and trying out some of the code: int x = 100; while ( 0 <-------------------- x ) { printf("%d ", x); } I attempted to compile with gcc and got the following error: file.c: In function 'main': file:c:10:27: error: lvalue required as decrement operand while ( 0 <-------------------- x ) But compiling with g++ works. Why is this code valid in C++ but not C? 回答1: In C, --x is a value, not an lvalue. Its effect is to decrement x , and evaluate to the newly assigned value

Decrement value in mysql but not negative

我怕爱的太早我们不能终老 提交于 2019-12-03 00:58:20
I want to decrement a value when user delete it in php and mysql. I want to check not to go below than 0. If value is 0 then do not decrement . mysql_query("UPDATE table SET field = field - 1 WHERE id = $number"); If field is 0 then do not do anything Add another condition to update only if the field is greater 0 UPDATE table SET field = field - 1 WHERE id = $number and field > 0 You could prevent the new value to drop below zero by using GREATEST() . If the value drops below zero, zero will always be greater than your calculated value, thus preventing any value below zero to be used. UPDATE

Decrementing for loops [duplicate]

做~自己de王妃 提交于 2019-12-02 18:42:34
This question already has an answer here: Loop backwards using indices in Python? 14 answers I want to have a for loop like so: for counter in range(10,0): print counter, and the output should be 10 9 8 7 6 5 4 3 2 1 user225312 a = " ".join(str(i) for i in range(10, 0, -1)) print (a) Check out the range documentation, you have to define a negative step: >>> range(10, 0, -1) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] You need to give the range a -1 step for i in range(10,0,-1): print i for i in range(10,0,-1): print i, The range() function will include the first value and exclude the second. range step

Why don't multiple decrement operators work in C when they work in C++?

白昼怎懂夜的黑 提交于 2019-12-02 17:51:21
Looking at this question and trying out some of the code: int x = 100; while ( 0 <-------------------- x ) { printf("%d ", x); } I attempted to compile with gcc and got the following error: file.c: In function 'main': file:c:10:27: error: lvalue required as decrement operand while ( 0 <-------------------- x ) But compiling with g++ works. Why is this code valid in C++ but not C? In C, --x is a value, not an lvalue. Its effect is to decrement x , and evaluate to the newly assigned value of x . Since --x is not an lvalue, it cannot be decremented. In C++, --x is an lvalue, not an rvalue. Its

Lua for loop reduce i? Weird behavior [duplicate]

巧了我就是萌 提交于 2019-12-01 22:36:54
问题 This question already has an answer here : Decrementing a loop counter as loop is executing (1 answer) Closed 4 years ago . Can someone explain me this? for i = 1, 5 do print(i) i = i - 1 print(i) end Output is: 1 0 2 1 3 2 and so forth I exspected i to alter between 1 and 0. But obviously it keeps increasing as if I did not change it at all. What's going on? I have to delete an i'th table element every now and then. So the next element to process would be i again. In C I would just write --i