This question already has answers here:
Byte incrementing operation in Java [duplicate]
(4 answers)
Closed 7 years ago.
When I start my code
for (byte i = 0; i < 1000; i++) {
System.out.print(i);
}
I get infinite loop. Why?
Simply because byte value starts to overflow after its max value i.e. 127.
The value of i will go up to 127 and then will overflow to -128 and then increment back to 127. This process will therefore never satisfy your for loop termination condition, and thus loop forever.
The maximum value of a byte is less than 1000.
Related
This question already has answers here:
What's the difference between i++ vs i=i+1 in an if statement? [duplicate]
(2 answers)
Closed 1 year ago.
can you do array[i]++?
Is it the same as array[i] = array[i] + 1?
If not, why not? Is it something to do with how primitives vs references are treated?
There's a slight difference: postfix vs prefix increment operator
If you use array[i]++, the old value will be used for the calculation and the value of i will be increased by 1 afterwards.
For array[i] = array[i] + 1, it's the opposite: i will first be incremented and only then the calculation will take place.
For more details, check this out.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
int keyCardNum = 10;
for(int x = 0; x<= keyCardNum; x++) {
System.out.println(x);
}
It prints the following:
0
1
2
3
4
5
6
7
8
9
10
Online I read that for loops start from 0. It that true and how can I remove the 0 and start from 1 to 10.
for(int x = 0; x <= keyCardNum; x++)
Defines three properties of the for loop:
int x = 0: one or more loop variables and their initial value. Executed once when the loop starts. Usually, this variable is called i.
x <= keyCardNum: the loop condition. Executed once per iteration. The loop terminates when this condition evaluates false. (the loop never runs if the condition evaluates to false immediately). Usually this is a less-than (<) expression. i=0;i<10;++i will loop 10 times, i=0;i<=10;++i will loop 11 times.
x++: the incrementor. Executed once per iteration. Here you define what happens after each loop iteration. Usually, you increment the loop counter variable, but you could do anything here.
The loop body is executed as many times as the loop condition evaluates to true. If you are interested in the nitty-gritty details, the Java Language Specification has them.
If you want a loop to start at 1, you have to initialize the loop variable with 1. Or, you "normalize" your loop variable when using them in an expression, e.g. x + 1 to produce a value offset by 1 (i.e. 1-11 in your example).
This question already has answers here:
++someVariable vs. someVariable++ in JavaScript
(7 answers)
Closed 7 years ago.
I don't consider myself to be bad at programming, but there's been something troubling me since the past few days.
int counter = 3;
++counter;
Is the following code above the same as counter++;.
It is similar, but not the same.
In your expression it doesn't matter, but if you had something more complicated, like System.out.println(counter++), it would make a big difference.
For example:
int counter = 3;
System.out.println(counter++)
This will print 3, then increment counter to 4.
However, if you do
int counter = 3;
System.out.println(++counter)
it will print 4 because it increments prior to giving the value as a parameter to the print function.
It's a question of when the increment is performed, the prefix performs it before other operations, postfix performs it after. They have different precedences.
This question already has answers here:
why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?
(9 answers)
Closed 8 years ago.
After going through this question, i had one another in my mind.
Question: Why an integer variable value is set to Integer.MAX_VALUE.
eg.
int x = Integer.MIN_VALUE;
x--;
if (x == Integer.MAX_VALUE) {
System.out.println("Why....");
}
There must be some reason that is why this behavior implemented explicitly, otherwise throwing an Exception would be a better idea. I could not find/locate this behavior in JLS.
Because of underflow. Computers have worked like this for years, throwing an Exception would be a horrible idea here.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.2
"The integer operators do not indicate overflow or underflow in any way."
This question already has answers here:
Post-increment and pre-increment within a 'for' loop produce same output [duplicate]
(12 answers)
Semantics of pre- and postfix "++" operator in Java [duplicate]
(7 answers)
Closed 9 years ago.
In some code i've seen a for loop with a --i as third parameters
for(int i=array.length; i<0; --i)
Maybe someone can explain me the difference with i-- ?
i guess it's something like the moment when i is decremented ?
If, for example, i = 5:
--i decrements i by 1 then gives you the value of i (4).
i-- gives you the value of i (5) then decrements it by 1.
Both will give you the same result in a for loop.