Skip to main content

For Loop

Print 1 to 9 number using for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
package basic;
 
public class ForLoop 
{
 
	public static void main(String[] args) 
	{
 
		for(int i=1;<10;i++)
		{
			System.out.println(i);
		}
 
	}
 
}
output:
 
1
2
3
4
5
6
7
8
9

For each loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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