(Java) Problems with for loop

  • #1
Hello, I haven't used the for loop in Java much, so I have a question. How do you put multiple statements inside the initialization, condition, and iteration portions of the for loop?

I was coding and I came across this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The operator || is undefined for the argument type(s) int, boolean
Syntax error on token "=", <= expected

at loop.For.main(For.java:18)

This is the code that I used:

Java:
package loop;
import java.util.Scanner;
public class For
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int q;
        int a;


        System.out.println("Please enter in a number:");
        q =in.nextInt();
        System.out.println("Please enter in a second number:");
        a =in.nextInt();
        int p;
        int o;
        for ( o = 0 || p = 100; o < 100 ; o = a + o)
        {
         
        }
    }
}
 
Last edited by a moderator:

Answers and Replies

  • #2
34,158
5,778
Hello, I haven't used the for loop in Java much, so I have a question. How do you put multiple statements inside the initialization, condition, and iteration portions of the for loop?

I was coding and I came across this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The operator || is undefined for the argument type(s) int, boolean
Syntax error on token "=", <= expected

at loop.For.main(For.java:18)

This is the code that I used:
Please use code tags on your code. They preserve the indentation you are using. Put a [ code ] or [ code=java ] tag (without extra spaces) at the top, and a [ /code ] tag (also without spaces) at the bottom. I have done this in your code.
icecubebeast said:
Java:
package loop;
import java.util.Scanner;
public class For
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int q;
        int a;


        System.out.println("Please enter in a number:");
        q =in.nextInt();
        System.out.println("Please enter in a second number:");
        a =in.nextInt();
        int p;
        int o;
        for ( o = 0 || p = 100; o < 100 ; o = a + o)
        {
         
        }
    }
}
Use a comma to separate initialization expressions, not ||.
Code:
for ( o = 0, p = 100; o < 100 ; o = a + o)
{
   // etc.
}
 
  • #3
@Mark44 thanks, the code works now.
 

Related Threads on (Java) Problems with for loop

  • Last Post
Replies
2
Views
2K
Replies
4
Views
2K
Replies
2
Views
2K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
2
Views
5K
Replies
8
Views
2K
Replies
2
Views
705
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
4K
Top