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 5 days ago.
Improve this question
I've been trying to get this loop working for almost two hours and I don't know what I'm doing wrong.
This is what my code looks like:
for(int i=0; i==10; i++) {
System.out.println(i)
}
I tried it with ++i, i-- and --i but nothing seems to work.
TL;DR: Try to replace the condition i == 10 with i<10.
i is being initialized to 0, and before the first iteration gets executed, the condition i == 10 is checked, which returns false. Thus resulting in the loop never being executed.
If you want to execute the for() loop a few number of times (in your case, maybe 10?), you need to put a condition that holds true until you increment i that number of times.
Putting i<10 in place of i==10 would result in evaluating a condition that holds true for all i (0, 1, 2, 3, ...) until 10.
If you want to loop 10 times use:
for(i = 0; i < 10; i++){
System.out.println(i)
}
If you want to loop until i equals 10 use:
for(i = 0; i <= 10; i++){
System.out.println(i)
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Might be a simple question but, having two nested loops, if I execute a break over the outer one, will the inner one break immediately too; Or will it let it finish its execution, then break the outer one right after?
For example:
outer: for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (j == 5) break outer;
System.out.println(j);
}
System.out.println("test");
}
Would this piece of code print numbers from 0 to 5 before it breaks the outer loop or would it print numbers from 0 to 10 instead?
Will the System.out.println("test"); execute too?
It will print numbers from 0 to 4.
5 not included, as the print is not executed when the condition is met (j==5). If you wish to include it, change the condition to (j>5).
Regarding your second question, no, "test" would never be printed. The labeled break finishes the outer loop (with every statement inside, including the inner loop) and ends your program's execution, by just printing:
0
1
2
3
4
Edit: tested and confirmed, above's the resultant output.
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
The value of sum is not changing even if I am adding it inside the for loop. Value of diff is 3
The value which is getting printed is like 3 3 3 in initial iteration which should be 3 6 9.
Can someone please help?
loop start
sum = sum + diff<(26-diff)?diff:(26-diff);
loop end
Order of precedence in how your numeric expression is being calculated is what's causing you to get the wrong result. Your current statement is equivalent to this:
sum = (sum + diff)<(26-diff)?diff:(26-diff);
and so equates to the value of diff, which is always 3. Change your statement to this:
sum = sum + (diff<(26-diff)?diff:(26-diff));
and you'll get the behavior you're expecting. you can also use the += operator to fix this by changing your statement to:
sum += diff<(26-diff)?diff:(26-diff);
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 want to sum the elements of 2 different lists.
in my test list 1 contains 1 and 2
and list 2 contains 3 and 4.
I want to sum the elements of list 1 and list 2 like this:
1+3, 1+4, 2+3, 2+4.
I've tried the code below,
but it does not work.
my code:
for (int i = 0; i < l1.size(); i++) {
for (int j = 0; j < l2.size(); j++) {
System.out.println( l1.get(i) + l2.get(i) );
}
}
my output always shows
5
5
5
5
kindly help and correct me if im wrong or missed the logic.
you have to use j at some point, at which you should be able to figure out yourself
this is a very common error with starters I guess, because using "i" becomes a unquestioned habit
to avoid this problem you could start using another naming pattern,
eg. call them it1 and it2 when iterating lists, and x y when traversing coordinates etc., this way you know what you intended to do with them
this also can improve readability a lot,
if you ever have to refine a complex nested for you will curse at not using better names
you might want to consider using the refactoring (in eclipse strg+1 and "rename in file") to give the iterators a more meaningful name afterwards, or if you get confused midway through the algorithm even beforehand
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 9 years ago.
Improve this question
I'm trying to count the number of items in a scanner object which are divisible by 2.
My code looks like this:
while (s.hasNext()) {
num = s.nextInt();
if ((num % 2) == 0); {
count++;
}
}
For every integer in the object though, count is increasing by 1, regardless if it is divisible by 2, or not. Can someone tell me what I'm doing wrong?
You have a semicolon (;) after your if clause. That means empty code is executed if the condition is true and the code in the code block is always executed.
The ; after the if should be omitted
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
why do i get this output ? I expect the output to be 10,9,8...
But it is 11,12,13....
class tester {
static int i = 0;
public static void main(String args[]) {
recursive();
System.out.println("after recursive");
}
public static void recursive() {
while(i++<10) {
recursive();
System.out.println(i);
}
}
}
The simple logic is as follows:
Since your incremental is a posfix increment, the original value is evaluated first, before increment.
Logic:
while (i++ < 10) { recursive();}
i is initially 0, so, 0 < 10 (true), increment by 1. Now, call recursive().
This loops (1) until i = 10. When i = 10 is evaluated, the while expression is false, so it leaves the loop.
Remember, we have already called recursive() 10 times already, so it now needs to print i, 10 times, (since the JVM pushed recursive() function 10 times in the stack). Each times it pops the function, it goes back to the System.out.println(i) statement, knowing that i was incremented in each call, hence why you see values:
11
12
13
14
15
16
17
18
19
20
after recursive
That's basically recursion in a nutshell. :-)
It makes recursive calls until i=10. i is incremented to 11. Then it stops and you start popping back up the stack, printing i. That's why it starts with 11. Remember it is going to check the while loop condition on the way back up, which increments i again each time. That's why it goes to 12, 13,...
It's because your i variable is declared outside of your recursive variable, and you are incrementing and recursing before your display it. Don't forget that i++ < 10 will get to evaluate i++, which will increment the value of i, even if the result of i++ < 10 is false.
If you manually write down the sequence of i++ that's a result of your code's execution, and also write down when the value of i is displayed, it should all become clear.
The control only reaches System.out.println when the recursive calls have stopped, and by that time the guard of the loop is false, which means i > 10 after the last comparison (i == 10 in this last comparison, but it gets post-incremented).
You are printing out the value of i. Nowhere in your code there's any statement that could decrease the value of i. Therefore you shouldn't be expecting to see decreasing values being printed out.
That aside, it is generally a bad idea to have recursion keep state in a global variable (here, i) as this tends to make it extremely hard to reason about what's happening in the code.