Java How do you handle multiple statements in Java's for loop?

  • Thread starter Thread starter icecubebeast
  • Start date Start date
  • Tags Tags
    Java Loop
Click For Summary
The discussion centers on using multiple statements within the initialization, condition, and iteration sections of a for loop in Java. The original code encountered compilation errors due to incorrect syntax, specifically using the logical OR operator (||) instead of a comma to separate initialization expressions. The correct format for the for loop is to use commas to separate multiple initializations, as demonstrated in the corrected code: "for (o = 0, p = 100; o < 100; o = a + o)". After applying this correction, the code functioned properly, resolving the user's issue.
icecubebeast
Messages
66
Reaction score
3
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:
Technology news on Phys.org
icecubebeast said:
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.
}
 
@Mark44 thanks, the code works now.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
3
Views
3K