Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java For loop to print out numbers in order

ByBoz

New Coder
Hi, I'm trying to write a code with for-loop to get numbers in order. For example 3,4,5,6 or -1,0,1,2,3. This is what I have come up with this far.
Java:
public static void runLoop(int start, int end){

    
    for (start <= end; start++){
        System.out.print(start);
    
    }
}
 
Java:
public class test {
    public static void main(String[] args) {
        for (int i = -3; i < 4; i++) {
            System.out.println(i);
        }
    }
}

output
Code:
-3
-2
-1
0
1
2
3
 
CODE=java]public static void runLoop(int start, int end){
for (start <= end; start++){
System.out.print(start);
}
}
[/CODE]

I guess you did not try to compile that code ? Here's what you would have seen then :
D:\java>javac runLoop.java
runLoop.java:1: error: class, interface, enum, or record expected
public static void runLoop(int start, int end){
^
runLoop.java:4: error: class, interface, enum, or record expected
for (start <= end; start++){
^
runLoop.java:7: error: class, interface, enum, or record expected
}
^
3 errors

If you are not aware that Java always wants a class definition, please read up on the basics of Java. So with a class definition added:
Java:
public class test{
    public static void runLoop(int start, int end){
        for (start <= end; start++){
            System.out.print(start);
        }
    }
}

the compiler now says

D:\java>javac test.java
test.java:6: error: not a statement
for (start <= end; start++){
^
test.java:6: error: ';' expected
for (start <= end; start++){
^
2 errors
so you need to read up on how to write a for loop, and what the 3 different parts mean. The correct syntax here is for (; start <= end; start++) Note the empty start condition. There actually is no start condition here, but you have to obey the syntax nonetheless. Java is very pedantic.

So with that added, your code compiles clean. Now of course you'll somehow want to call this method (nobody's going to do that for you), so you best add a main() method. If you're not aware Java programs need a main() method, read up on the basics again. Finally you'll see that it prints everything on one line, so you'd better use println rather than print.

All of these points are of course already implicitly made clear by the code @menator01 kindly provided 😉
 

Buy us a coffee!

Back
Top Bottom