for loop is skipping to the end? [closed] - java

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
I am fairly new to Java, but my for loop is instantly skipping to the highest possible value in the following code:
System.out.println(i);
for(i = 0; i <= (difficulty - 2); i++);{
System.out.println(i);
nextMineX = (int) (10*Math.random());
nextMineY = (int) (10*Math.random());
for(y = 0; y <= 14; y++){
System.out.println(y);
if(nextMineX == minesX[y] && nextMineY == minesY[y]){
i = i-1;
} else{
minesX[i] = nextMineX;
minesY[i] = nextMineY;
}
}
}
The first for loop is screwing up, while the nested one is running fine. the variable i is initialized as 0, and difficulty is at 16. the output of this excerpt is as follows:
0
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
If anyone can help me with his that would be extremely appreciated. Since I'm new, it is probably something small and basic that I'm overlooking.

The problem is the semicolon at the end of the second line. It's valid to have a for loop with no body. It's also valid to have standalone a block of code inside brackets (this defines a scope for variables--if you defined a variable inside the brackets, it wouldn't be available outside). So Java is interpreting the beginning of your code like this:
for(i = 0; i <= (difficulty - 2); i++); // for loop is done, so i = difficulty - 2
{
System.out.println(i);
...

Your for loop statement ends with the semicolon you have
for(i = 0; i <= (difficulty - 2); i++); <- incorrect
for(i = 0; i <= (difficulty - 2); i++){ <- correct
//body
}

For loop is terminating cause of semicolon ;
for(i = 0; i <= (difficulty - 2); i++); //semicolon terminating loop
{...}
so you should use for loop like this
for(i = 0; i <= (difficulty - 2); i++) //remove semicolon prevent to terminate
{...}

Related

Why does it print 12345 not 01234? [closed]

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 4 months ago.
Improve this question
It has to be output like this 01234
for(int i = 0;i < 5; i++, System.out.print(i));
or
for(int i = 0;i < 5; System.out.print(i))
i++;
Output: 12345
for(int i = 0;i < 5; System.out.print(i))
i++;
Is equivalent to the following while loop:
{
int i = 0;
while (i < 5) {
i += 1;
System.out.print(i);
}
}
i is always incremented before it is printed. Same goes for i++, System.out.print(i): i is already incremented before it is printed. And I don't need to mention that 0 + 1 = 1.
It's pretty easy, you just need to understand what you're writing.
It should go like:
for(int i=0;i<5;i++){ //"i" is increased at the end of the loop
System.out.print(i) //prints "i", which is zero and will increase up to 4
}
And that's it

output of print statement is not clear [closed]

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 6 years ago.
Improve this question
int i, tem = 3;
i = tem;
for (int x = 0; x <= i; x++) {
for (int j = 1; j< tem; j++) {
System.out.println("a");
}
}
The above code prints a 8 times. I'm not able to understand why.
for (int j = 1; j < tem; j++)
The above code runs for j=1 to j=2, that is 2 times and will print
a
a
Now the for loop
for (int x = 0; x <= i; x++)
will execute the above printing 4 times (x = 0 to 3). So overall your code will print 8 times.
a //x=0
a
a //x=1
a
a //x=2
a
a //x=3
a
consider iteration one x=0 and tem=3 so inner for loop iterates twice for conditions j=1 and j=2. Second iteration now x=1 and tem=3 in this case also inner loop iterates twice for j=1 and j=2 .This process continues for x=2 and x=3 as well ,so finally first for loop condition gets passed for 4 times i.e x=0,x=1,x=2 and x=3 and inner for loop condition passes twice for every first for loop condition getting passed so it prints 4*2 times which is eight times
It should not print 'a' for 8 times. The following program print 'a' for 4*3=12 times.
public class HelloWorld{
public static void main(String []args){
int i, tem = 3;
i = tem;
for (int x = 0; x <= i; x++) {
for (int j = 1; j <= tem; j++) {
System.out.println("a");
}
}
}
}
Try executing the above program and you will get correct number of 'a' (12 times) instead of 8 a's.
The first for iterates from 0 to 3, that's 4 times.
The second for iterates from 1 to 3, that's 3 times.
Since the first for is inside the second, the print will happen 3 times * 4, that's 12.
If you need it to be 8 prints, you should change <= for an < either in the first or the second for.
Sometime is useful to use the debugger, and in cases like this where you are looping every now and then
printin the indexs to see/proof the "logic"
your code is kind of a pitfall with the indexs in the for loops
but you can replace the printline and verify that
the outter loop is from 0 to 3: 4 times
the inner loop is from 1 to 2: 2 times
ergo 4x2 times printing the char a
Example:
public static void main(String[] args) {
int i, tem = 3;
i = tem;
for (int x = 0; x <= i; x++) {
for (int j = 1; j < tem; j++) {
System.out.println("x: " + x + ",j:" + j);
}
}
}

finding odd numbers in java [closed]

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
I try to print the odd numbers in Java that are inside the array but this algorithm doesn't work ... May someone help me ?
The printing result is that :
"Exception in thread "main" .java.lang.ArrayIndexOutOfBoundsException: 7
at JavaArray.main(JavaArray.java:12)"
Code :
public class JavaArray {
public static void main(String[] args) {
int[] myArray = {1,3,4,5,8,9,10};
int i = 0;
for(i = 0; i < myArray.length; i++); {
if(myArray[i] % 2 == 1) {
System.out.println(myArray[i]);
}
}
}
}
Remove the semi-colon that is terminating your for loop
for (i = 0; i < myArray.length; i++);
^
Because you have placed semicolon after for loop, variable i increments till length of array(here 7). After that loop ends and you are trying to access myarray element through i which is 7 so it is giving out of bound exception.
Besides the extra ; you need to remove, you can consolidate by declaring the int in the loop declaration:
for (int i = 0; i < myArray.length; i++) {
.
.
.
}
Beside #Reimus point , you can also do it like below , sort the array if it's not sorted yet, in your case it is sorted . FYI, Instead of Collections.sort which is above O(N) complexity use a Hash Set.
public static void main(String[] args) {
int[] myArray={1,3,4,5,8,9,10};
Arrays.sort(str);
for (int i = 1; i < myArray.length; i++) {
if (str[i] == str[i - 1]) {
System.out.println("Dupe-num: " + str[i];
}
}
}

Java loop can't iterate down string length [closed]

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
Here is some basic code:
public static void main(String[] args) {
String string = "Hello!";
System.out.println("First loop.");
for (int i = 0; i < string.length(); i++) {
System.out.println("g");
}
System.out.println("Second loop.");
for (int i = (string.length() - 1); i <= 0; i--) {
System.out.println("g");
}
}
For some reason, the program won't go through the second loop at all. This is somewhat strange. Can you explain this, and how to fix it?
Your second loop should be looping backwards, while the index is still greater than or equal to zero, not less than or equal to zero. With <= 0, i is greater than zero on the first evaluation and the loop never runs.
Try:
for (int i = (string.length() - 1); i >= 0; i--) {
Change the for condition,the i is initial with value greater than 0 (length-1) and there is condition i <= 0 which is true in case length is equal to 1.But the length of string is 6 so change the condition as below :
for (int i = (string.length() - 1); i >= 0; i--) {
System.out.println("g");
}
Your problem is the condition in the second for loop
i <= 0
never happens. I don't understand why you would want to check that.

The value of the loop counter of a for loop is one larger than I expect after exit from the loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple for loop, which lets the loop counter, i go up to 5.
int i;
double n = 1 / 2;
for (i = 2; i <= 5; i++) {
n = n + 1.0 / i;
}
System.out.print(i);
So I expect the value of the counter to be 5 after the loop finishes. But the value is 6, nit 5. Why is that?
Thanks
Because you are incrementing i value as i++ in for{..} loop
for (i = 2; i <= 5; i++)
^ here
In for loop after checking the condition, body part will be executed
after that increment or decrement will be done
Process will be
<----
1step 2step 4step
for (i = 2; i <= 5; i++){
/*body part*/
3step
}
After 4th step it will moves to check 2nd step i.e. condition part
So thats why it prints the i value as
6
The for loop:
for (i = 2; i <= 5; i++) {
// code
}
which has condition i <= 5 and the condition will be false when i = 6 and the loop breaks, goes to the print line.
Thanks for reminding me my first time programming experience, When I used to write a code a = 5 and printed it to see what it shows in the console. :)
The i++ is the same as saying i = i + 1. In this case you can also use ++i and get the same result.

Categories

Resources