aspose file tools*
The moose likes Beginning Java and the fly likes how does this program  work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how does this program  work " Watch "how does this program  work " New topic
Author

how does this program work

jay nair
Greenhorn

Joined: Sep 13, 2005
Posts: 28
hi ,
i am new to java and would like to know how this program works ,

class aarr{
public static void main ( String args[])
{
int arr[][]=new int [4][5];
int i=0,j,k=0 ;

for ( i=0;i<4;i++) // have a doubt in this loop , no curly braces ??
for (j=0;j<5;j++)
{
arr[i][j]=k;
k++;

}
for ( i=0;i<4;i++)
{
for ( j=0;j<5;j++)
System.out.print(arr[i][j] + " " );
System.out.println();
}
}
}



Everything works fine it gives out an out put Of :
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19

what i would like to know is how does the first "for" loop work,
shouldnt it have curly braces ( start of loop and end of loop )
and if it works with out curly braces shouldnt it work till the end of program , since there is no " end of Loop"

i might sound v confused
help will be appreciated
thanks in advance


Preparing for SCJP 5
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Braces combine one or more constructs to be - in effect - one construct. Because the 2nd for does have braces, it is looked upon by the 1st for as being one construct.

~Bill


~Bill
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name
here.
Thanks!

Now, as for your question: in Java's grammar, a block (i.e., a set of curly braces and what they contain) is a kind of statement. It's a "compound statement." We can call a single line of code a "simple statement." Anywhere a simple statement can appear, a compound statement can also appear.

The body of a loop is, technically, a statement. It can be either a simple statement or a compound statement. Therefore if the body of a loop contains just one simple statement, the braces can be left off.

Now, here's the possible tricky part: a loop is itself a single statement. Therefore a loop, all by itself, can be the body of another loop. That means that



is legal. The body of the "while (3)" loop is the println() statement. The body of the "2" loop is the "3" loop, including its body. No braces are needed. Now, if we want to print two things in the inner loop we need some braces:



Make sense?


[Jess in Action][AskingGoodQuestions]
jay nair
Greenhorn

Joined: Sep 13, 2005
Posts: 28
thanks a lot .... it does make sense now
i had a doubt if this is specific to Java's Grammar , so i checked it out in " C" also and it works just fine , ,

thanks again
and changed the display name
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how does this program work