-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13 .For Loop.java
More file actions
68 lines (54 loc) · 1.64 KB
/
13 .For Loop.java
File metadata and controls
68 lines (54 loc) · 1.64 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package test;
import java.util.Arrays;
import java.util.Scanner;
//import javax.swing.*;
//import java.lang.Math;
//import java.util.Random;
//import java.awt.Color; // abstract window toolkit
import java.util.Arrays;
public class main {
// For Loop.
// For Loop examples: iterate a part of the program several times
//
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare the Scanner library
Scanner scanner = new Scanner(System.in);
// explanation
//for (start; end; counter(increment/dis..)){
//}
// Simple for loop
for (int index = 0; index < 6; index++) {
System.out.println("Result of index is equal : " + index);
}
// Looping through an array
int[] numbers = {1,2,3,4,5,6,7,8,9,10}; // array list contains many numbers
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " "); // 1 2 3 4 5 6 7 8 9 10
}
// new line
System.out.println();
// Looping through an collection using enhanced for loop (ints)
for(int number : numbers) {
System.out.print(number);
}
// new line
System.out.println();
// Looping through an collection using enhanced for loop (strings)
String Names[] = {"IA","Ahmad","Ali","Omar"};
for(String fetch_individual_name : Names) {
System.out.println(fetch_individual_name);
}
// Looping through a string (original for loop)
String text = "Hello, it's ia";
for(int i = 0 ; i < text.length(); i++) {
System.out.print(text.charAt(i) + " ");
}
// Nested for loops
for (int i = 1 ; i <= 3 ; i++) {
for (int j = 1; j <= 3 ; j++) {
System.out.println(i + ", " + j);
}
}
}
}