For Loop

package basic;

public class ForLoop 
{

	public static void main(String[] args) 
	{
	
		for(int i=1;i<10;i++)
		{
			System.out.println(i);
		}
		
	}
	
}
output:

1
2
3
4
5
6
7
8
9


For each loop


package basic;

import java.util.ArrayList;

public class Foreach 

{
	public static void main(String[] args) {
		int a[]={12,2,3,333,};
		String s[]={"heloo","hiii","hows u"};
		
		ArrayList arr1=new ArrayList();
		
		arr1.add("helloo");
		arr1.add("hiiiii");
		arr1.add("hous u");
		
		
		//PRINTING STRING ARRAY
		for(String str:arr1)
		{
			System.out.println(str);
		}
		
		//PRINTING INTEGER TYPE ARRAY
		for(int b: a)
		{
			System.out.println(b);
		}
	}

}

output:

helloo
hiiiii
hous u
12
2
3
333


Leave a Reply