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).
Related
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 3 years ago.
Improve this question
I am trying to update the for loop variable i inside the loop, why I cannot update it and the i is always 0?
public class HelloWorld{
public static void main(String []args){
for (int i = 0; i < 10 ; ){
i = i ++;
System.out.println(i);
}
}
}
I am expecting print out
0 1 2 3 4 5 6 7 8 9
but it is a infinite loop and print out all 0.
You can, but with i = i ++; you're not incrementing the counter. Use i++ instead - it's an operator that does not return a new value, only increments the primitive.
Keep in mind, that for loop is meant to contain the information about looping. If you manipulate the counter outside of loop, you might loose track of what is going on. This is not recommended, better use while in that place.
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 3 years ago.
Improve this question
I'm studying for my OCA certification and found this piece of code and was wondering why the result of the code below is "1, 2" and not "1, 3".
The '&' operator used here is not short cirquit, and the postfix increment is applied to the first int, so why not to the second?
int i = 0;
int j = 1;
if( (i++ == 0) & (j++ == 2) ){
i = 12;
}
System.out.println(i+" "+j);
edit Taking breaks is important while studying. I understood the concepts involved but fatigue let me make a logical error which made me assume j started out as 2
Why would j be 3 when it is incremented only once in the if block j++ == 2. Initially j is initialized to 1 then the ++ operator was applied which incremented it to 2.
++ operator is essentially doing j = j + 1, and since you used the logical AND operator & both the first condition and second condition was evaluated.
Note: If the first condition was evaluated to false still the second condition would have been evaluated as & is the logical AND operator and not the conditional && operator which is short circuiting.
Logical operators as described in the javase specs.
As #Marvin noted in the comments, j++ = j + 1 = 1 + 1 = 2.
It's Important to take regular breaks when studying for your OCA certification :)
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 7 years ago.
Improve this question
for (int i = 0; i < totalArray.size(); i++){
studentNumber.add(Long.parseLong(totalArray.get(i * 3)))
lastName.add(totalArray.get((i * 3) + 1));
firstName.add(totalArray.get((i * 3) + 2));
I'm not sure what is going on here. I've getting an index out of bounds error on this code. totalArray.size() is 42 however I'm getting the error at index 42 with the second line (parseLong).
As cricket_007 points out, on the 14th iteration of your for statement, i is 14 and your second line attempts to access element 42 of your index (where the last element of your array is accessed as element 41 because it's 0 indexed).
To stop the for statement at the proper place, merely do the inverse operations which you do on your last line of code.
i < (totalArray.size()-2)/3
(which was pointed out by Benjamin Lowry).
You're multiplying your i by 3 in your studentNumber.add(Long.parseLong(totalArray.get(i * 3))) line, so when i >= 14, it goes out of bounds.
Try setting it to studentNumber.add(Long.parseLong(totalArray.get(i))) and run it. You probably wanted to place your * 3 outside the first parenthesis, but I can't deduct your logic.
When i is 14, you are getting the index 42 with i * 3. Which is out of bounds for a list of size 42.
Your third line triples the number. This means that if i was 30 (which is possible with your current set up), then when tripled, would equal 90, which is obviously above your max size.
You need to do
for (int i = 0; i < (totalArray.size() - 2) / 3 ; i++)
In order for your code to work. Otherwise all three of your lines will exceed the max at some point.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a question: Can some one give me an example of searching the binary table to find the change from 1 to 0? For example, one line have
0 0 0 1 1 0 0 0 0 1 1 1 0 0
and it should give me 2 changes. I want to search only one line.
for(int i=1; i< binarytableline.length; i++){
if (binarytableline[i - 1] == 1 && line[i] == 0) changes++;
}
Remember that the indexes start at zero:
(Java docs)
Essentially, whats happening in the code above is that we are looping through the line. Now, i is the index. Lets start at the beginning of the loop.
i=1
So, binarytableline[1-1] = the first index of the binary table line, or the very first number. Now, we are seeing if it equals 1, and the second index, which is i, equals 0. To check, we do the following:
binarytableline[i - 1] == 1 && line[i] == 0
This would mean that there is a change in binary digits from one digit to the next, and in our example that is the 0th index to the first. Now, we would iterate our variable, changes by 1 by doing changes++. Again, this is in a for loop, meaning we will loop through all the elements like this. The amount of times it has changed will be recorded in int changes.
Let me know if this helped,
Ruchir
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 8 years ago.
Improve this question
I'm using a while loop and it won't terminate when it should. If it was working correctly, then it would terminate when randno was either == highbound or == lowbound.
Code of the loop:
do {
do {
randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1);
direction = getDirection(randno,heading);
} while (robot.look(direction)==IRobot.WALL);
System.out.println(randno);
System.out.println(highbound);
System.out.println(lowbound);
System.out.println("---------------");
} while (randno!=lowbound | randno!=highbound);
The output is either 3 3 2 ------, or 2 3 2 ------, so the loop should end. The first loop ends properly (I embedded them to try and get it to work...). What is going wrong?
randno!=lowbound | randno!=highbound is always true, since randno can't be equal to both lowbound and highbound (assuming they are not equal).
Therefore the loop never terminates.
If you wish to terminate when randno is different than both bounds, change your condition to :
while (randno==lowbound || randno==highbound)
If you wish to terminate when randno is the same as one of the bounds, change your condition to :
while (randno!=lowbound && randno!=highbound)
EDIT : based on your question, you want the second option.