This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how does this program  work " Watch "how does this program  work " New topic

how does this program work

jay nair

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++)

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

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.


Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183


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

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

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:
subject: how does this program work
Similar Threads
Evaluation of nested loops
Initializing a one dim array ??? need help plz
ArrayIndexOutOfBundsException ??? pl help !!!
need help for critical path analysis in java
ArrayIndex in Arrays