<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">
<channel>
<title>for loop</title>
<link>http://www.computersight.com/tags/for loop</link>
<description>New posts about for loop</description>
<item>
<title>The Generic for Loop</title>
<link>http://www.computersight.com/Programming/Java/The-Generic-for-Loop.139550</link>
<description>
<![CDATA[<p>I would like to share a basic things on the modified “for” loops in the java 5. </p>
 
 <p>The “For Loop” is a loop used to iterate the group of statements.</p>
 
 <p>The general syntax of the “For” loop:</p>
 <p>	for (initialization; condition; increment or decrement)</p>
 <p>	{</p>
 <p>	 //statements here</p>
 <p>	}</p>
 <p>Initialization:  This part is used to initialize the variable. </p>
 <p>	 Ex:  int i = 0;</p>
 
 <p>Condition:  This part is used to declare the condition. If the condition fails, it will not execute the statements in the loop.</p>
 <p>	 Ex:  i<10;</p>
 <p>Increment or Decrement:  This part is used to increment or decrement value of the declared variable previously.</p>
 <p>	 Ex: i++; or i--;</p>
 
 <p>We can implement the “for” loop in different ways. We can eliminate the any part (initialization, codition, or increment or decrement) from the loop. But my main focus is that how to use the basic “for” loop in the java 1.4.x and 1.5.</p>
 
 <p>Example:  Print the number from 1 to 100;</p>
 <p>//declare the class</p>
 <p>public class NaturalNum</p>
 <p>{	//declare the main method</p>
 <p>	public static void main (String args[])</p>
 <p>	{</p>
 <p>	 //declare the variable</p>
 <p>	 <strong>int natNum = 100;</strong></p>
 <h3>	 //for loop (initialization;  condition; increment) </h3>
 <h3>	 for (int number = 1; number <= natNum; number++)</h3>
 <h3>	 {</h3>
 <h3>	  //print the numbers</h3>
 <h3>	  System.out.println(number);</h3>
 <h3>	 }</h3>
 <p>	}</p>
 <p>}</p>
 
 
 
 <p>The above loop is the common “For” loop used in the java 1.4.x.   The java5 introduces new “For” loop with generics. Please see the following examples.</p>
 
 
 <h3>The older version of the “for” loop:</h3>
 <p>public void printNames(Collection names)</p>
 <p>{</p>
 <p> <strong>Iterator namesIterator = names.iterator();	</strong></p>
 <h3>	for (int ptr=0; ptr < names.size(); ptr++)</h3>
 <h3>	{</h3>
 <h3>	 String name = (String) namesIterator.next();</h3>
 <h3>	 System.out.println(name);</h3>
 <h3>	}</h3>
 <p>}	</p>
 
 
 <h3>The newer version of the “for”(generics) loop:</h3>
 <p>//The collection should be declared like below</p>
 <p>//  Collection<String> names;</p>
 <p>public void printNames(Collection<String> names)</p>
 <p>{</p>
 <p> <strong>for ( String name : names)</strong></p>
 <h3>	{</h3>
 <h3>	 System.out.println(name);</h3>
 <h3>	}</h3>
 <p>}</p>
 
 <p>In newer version, the JVM will implement the iterator stuff.  It will reduce the overhead of using iterators.  But we will not escape iterators every time. If you want to modify or remove the value from the Collection, We need iterator for that. Using generics, we can not modify or remove the value from the collection.</p><a href="http://www.pheedo.com/click.phdo?x=&u=http%3A%2F%2Fwww.computersight.com%2FProgramming%2FJava%2FThe-Generic-for-Loop.139550"><img src="http://www.pheedo.com/img.phdo?x=&u=http%3A%2F%2Fwww.computersight.com%2FProgramming%2FJava%2FThe-Generic-for-Loop.139550" border="0"/></a>]]></description>
<pubDate>Mon, 16 Jun 2008 02:51:24 PST</pubDate></item>
</channel>
</rss>
