This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FOR LOOP

 
amir zaheer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • 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).]
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • 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
 
Muhammad Siddiq
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic