forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForLoop.java
More file actions
103 lines (88 loc) · 1.85 KB
/
ForLoop.java
File metadata and controls
103 lines (88 loc) · 1.85 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.examplehub.basics;
public class ForLoop {
public static void main(String[] args) {
/* output: ########## */
for (int i = 1; i <= 10; ++i) {
System.out.print("#");
}
System.out.println("\n");
/* output: 11111 */
for (int i = 1; i <= 5; ++i) {
System.out.print(1);
}
System.out.println("\n");
/* output: 22222 */
for (int i = 5; i >= 1; --i) {
System.out.print(2);
}
System.out.println("\n");
/* output: 12345 */
for (int i = 1; i <= 5; ++i) {
System.out.print(i);
}
System.out.println("\n");
/* output: 246810 */
for (int i = 2; i <= 10; i += 2) {
System.out.print(i);
}
System.out.println("\n");
/* output: 9630 */
for (int i = 9; i >= 0; i -= 3) {
System.out.print(i);
}
System.out.println("\n");
/*
* output:
* i = 0, j = 5
* i = 1, j = 4
* i = 2, j = 3
*/
for (int i = 0, j = 5; i < j; i++, j--) {
System.out.println("i = " + i + ", j = " + j);
}
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}
/* output: 12345 */
for (int i = 0; i < numbers.length; ++i) {
System.out.print(numbers[i]);
}
System.out.println("\n");
/* output: 12345 */
for (int number : numbers) {
System.out.print(number);
}
System.out.println("\n");
/*
* 1 2 3 4 5
*/
int x = 0;
while (true) {
x++;
System.out.print(x + "");
if (x == 5) {
break;
}
}
System.out.println();
/*
* 1 2 3 4 5
*/
int i = 0;
do {
i++;
System.out.print(i + "");
if (i == 5) {
break;
}
} while (true);
System.out.println();
/* infinite loop */
/*
for (; ; ) {
do some work
}
*/
}
}