• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FOR LOOP

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i m new to java and trying to lern the basics. i have understood
what FOR LOOP is but i cant understand the logic to put while mking a programme
i have tried to understand from different books how the FOR LOOP
works but cant mke programme as i desire
can any one send me detailed and simple notes to understand FOR LOOP operation
thanks
amir
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

compile and run this... it counts from 1 to 10 because of the for loop.
"i is set to "1"
for( int i = 1 ;
and is incremented by 1 each "loop"
i++ );
until it is "<= 10"
i <= 10 ;
then the loop terminates.

[This message has been edited by Greg Harris (edited August 14, 2001).]
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi amir I'm moving this topic to "Java in General (Beginner)" forum.
Good luck learning java
good catch though Greg
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Loops are basically used for iterations(repeatation). It's not only in java but used in other computer languages also for the same purpose. For eg. if u to print your name ten times. There
are two approaches to this problem.
1) is to write the statement System.out.println("Your Name"); ten times. This is not appreciable because it makes your code longer and inefficient.
2)The other is to use a for loop,while loop or a do-while loop.

The syntax for the For loop is
for(initialization_expression;conditonal_expression;increment_expression)
{
body of loop
}
lets implement the example stated above using the for loop.
class yname
{
public static void main (String [] args)
{
int i=1;
for(i=1;i<11;i++)
{
System.out.println("Your Name");
}
}
}
Output
This code will print "Your Name" ten times.
I m also a java beginner. If u have further questions regarding for loop, plz dont hesitate to ask.
Thanks.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic