• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

problem with for loops

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey!
I have a problem with the for loops in the following program.It's a little silly but it's really bugging me..

class twoDArray{
public static void main(String args[]){
int twoD[]=new int[4][5];
int i,j,k=0;
for(i=0;i<4;i++)//why no curly braces here
for(j=0;j<5;j++){
twoD[i][j]=k;
k++;
}
for(i=0;i<4;i++){
for(j=0;j<5;j++)
system.out.println(twoD[i][j]+" ");
system.out.println();
}
}
}

why doesn't the first for loop need any curly braces?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not just for loops. While loops are the same way.

According to the JLS, you can have exactly one 'thing' in the body of your loop. that thing can be (for example) a for-loop. Or an executable statement. Or a block of code surrounded by curly braces.

So, your first loop has one thing in it - a for-loop.

Now...just because you CAN leave those braces off doesn't mean you SHOULD. Pretty much everyone here is going to tell you to ALWAYS use them, whether they are strictly needed or not.
 
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System should be capitalized..
 
Marshal
Posts: 79778
382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
You ought to indent that code, then you would be able to see the relationships between the different parts. You should also tell us (for both your questions) where the question comes from.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not a good practice to avoid braces like that.
 
nitya satheesh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!!!
I have one more question about this code.
The output of this code is supposed to be :
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19

but I get
0
1
2
3
4

5
6
7
8
9......

Why this difference?

I got this code form a book called Complete reference :Java.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have to fiddle with System.out.print and System.out.println in some *rare* case nowadays...


 
Manu Somasekhar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nitya ,
what about changing



to



 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic