i am doing a problem.the problems was that what do the following code prints

class printing{
            public static void main(String[] args){
            for(int i=1; i<=10; i++)
{
            for(int j=1;j<=5;j++)
        System.out.print("@");
            System.out.print("");
}}

This code prints infinte "@" and after some second the printing starts to slow down.

when i do through the code by editing i come to known that first outer loops executes then it comes to inner loop and prints the char "@" 5times and then it goes to outer loop again and "i" becames 2 and then same proces repeats and j became 2 and due to System.out.print("") statement in the inner loop prints char "@" infinite time and slows after some time and if there is no System.out.print("") statment in the inner loop it then there is no infinte loop.this what i come to understand but i didnt understand that why it became infinite loop because of this System.out.print("") statement. Have your suggestion please

Recommended Answers

All 3 Replies

First of all, your closing braces are not there, So I don't even know how it is compiling. So fix that, it is probably your problem.

class printing{
public static void main(String[] args){
for(int i=1; i<=10; i++)
{
    for(int j=1;j<=5;j++)
    {
    System.out.print("@");
    System.out.print("");
    }
}
}

}//you didn't close the class brace

The for loops don't matter as much as the end brace(You can use them without braces), but its good practice to contain all your code in braces.

This code (assuming the class end brace was there somewhere later, and ignoring the non-issue of putting braces round a single-statement if) runs perfectly for me.
Maybe the problem's somewhere else in the rest of the code or the system config, not in this code.
Do other programs run ok?

class printing{
            public static void main(String[] args){
            for(int i=1; i<=10; i++)
{
            for(int j=1;j<=5;j++)
	    System.out.print("@");
            System.out.print("");
}}

Is this exactly the code you're running? Did you copy and paste this, or did you re-type it.
The easiest way to get this into an infinite loop would be to mistype either the comparison or the increment portion of one of the for loops. The loops are correctly written here, are they correct in your code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.