Questions tagged [decrement]

Subtracting one from the value of a variable, generally with the use of an decrement operator.

Subtracting one from the value of a variable, generally with the use of an decrement operator(--).

425 questions
5
votes
3 answers

What would cause a for loop to decrement when it's supposed to increment?

I wrote a method to calculate how long ago a father was twice as old as his son and in how many years from now this would be true. Unexpectedly, it returns "-2 years ago" for an 8-year-old father and a 3-year-old son. Equally unexpectedly, it…
K Man
  • 602
  • 2
  • 9
  • 21
5
votes
0 answers

What is the purpose of forbidding increment or decrement operators by eslint

The eslint documentation describes a way to forbid the increment and decrement operators ++ and --. The description includes the sentence, that these operators are subject to automatic semicolon insertion, differences in whitespace can change…
Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
5
votes
4 answers

How can I increment and decrement an integer with the modulo operator

I am trying to increment an integer based on a click. How the click happens does not matter so I'll stick to the logic. I am doing this in Java but the logic should be the same all around. int index = 0; // then within the click event //arrySize…
Lucas Santos
  • 1,359
  • 3
  • 24
  • 43
5
votes
4 answers

When to use the Increment Operator on Strings in PHP?

I am learning PHP for the first time and I find it surprising that the language allows using the increment operator on strings. $foo = 'xyZ'; print ++$foo; // prints xzA The tutorials I can find around this topic introduce toy examples…
Helmyano
  • 143
  • 1
  • 7
4
votes
4 answers

Atypical uses for Javascript's ++ and -- operators

If I recall correctly from Crockford's "Javascript: The Good Parts", he is not in favor of using the ++ or -- operators, but I also tend to recall he doesn't provide an especially strong argument against them. Below is a use of these operators that…
Dexygen
  • 12,287
  • 13
  • 80
  • 147
4
votes
3 answers

Is decrement of bool variable defined in С?

This question is about С. Say we have a code like this: bool a = false; a++; printf("%d\n", a); a--; printf("%d\n", a); This on my x86-64 linux machine shows: 1 0 That was not a surprise for me. And this code: bool a = false; a++;…
alex_why
  • 95
  • 3
4
votes
2 answers

Looping Fundamentals

Can someone please let me know where I went wrong here? When I run the code, I get 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10. How would I make the loop stop at 0? When I remove the last line of code (console.log(countDown);), the loop ends at 0 but the…
junebug
  • 45
  • 4
4
votes
3 answers

post decrement in while conditon in C

I've done a search and I’ve found nothing relevant to my query. I am currently debugging a C optimizer and the code in question looks like this: while( x-- ) array[x] = NULL; What should happen in this instance? And should the result of this logic…
Ahmad Badoura
  • 43
  • 1
  • 3
4
votes
2 answers

pre Decrement vs. post Decrement

When should I use pre decrement and when to use post decrement? and for the following code snippet, should I use pre or post decrement. static private void function(int number) { charArr = new char[number]; int i = 0; int tempCounter; …
sikas
  • 5,435
  • 28
  • 75
  • 120
4
votes
5 answers

Decrementing lowercase letters to lowercase letters only

I want to decrement lowercase letters to lowercase letters only. I do this by taking the ASCII value of the character and decrement it. But for example if I decrement a by 2, the answer should be y. Not a symbol or a uppercase letter. int charValue…
manuka_m
  • 331
  • 2
  • 12
4
votes
1 answer

Understanding Java behaviour in recursive factorial

I created two recursive methods to calculate factorial as follows: private int fact1(int n) { if (n == 0 || n == 1) return 1; return n * fact1(--n); } private int fact2(int n) { if (n == 0 || n == 1) return 1; …
El Mestre
  • 85
  • 10
4
votes
2 answers

Recursion and pre-decrement operator

I have this function: void m(int n) { if(n > 0) m(--n); cout << n << " "; //for n = 5 --> output is: 0 0 1 2 3 4 } I have a problem with understanding how it works. For example: n(input) = 5 output: 0 0 1 2 3 4 My question is: Why…
gryzek
  • 537
  • 9
  • 25
4
votes
2 answers

Decrementing a value and wrapping back to a max value without using if statement?

Incrementing and wrapping back to zero is easy: i = (i + 1) % Max; But decrementing is not as straight forward. Basically to decrement and wrap you usually find code like this: i--; if (i < 0) i = Max; How can we write the previous code without…
vexe
  • 5,433
  • 12
  • 52
  • 81
4
votes
1 answer

Mixing post- and pre- increment/decrement operators on the same variable

Possible Duplicate: Why is ++i considered an l-value, but i++ is not? In C++ (and also in C), if I write: ++x-- ++(x--) i get the error: lvalue required as increment operand However (++x)-- compiles. I am confused.
Xolve
  • 22,298
  • 21
  • 77
  • 125
4
votes
5 answers

How do you subtract from a negative integer but add to a positive?

Simple question really, although I'm not sure if there's an answer. I am handling an integer which can be positive or negative. However, I would like to either increment or decrement it dependant on the sign. For example, if it's "2", then add 1 and…
Shivam Malhotra
  • 309
  • 1
  • 3
  • 10
1 2
3
28 29