forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhileLoop.java
More file actions
80 lines (72 loc) · 1.24 KB
/
WhileLoop.java
File metadata and controls
80 lines (72 loc) · 1.24 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
package com.examplehub.basics;
public class WhileLoop {
public static void main(String[] args) {
/*
* Syntax
* while (condition) {
* // code block to be executed
* }
*/
/*
* #####
*/
int i = 1;
while (i <= 5) {
System.out.print("#");
i++;
}
System.out.println();
/*
* 1 2 3 4 5 6 7 8 9 10
*/
i = 1;
while (i <= 10) {
System.out.print(i + " ");
i++;
}
System.out.println();
/*
* 2 4 6 8 10
*/
i = 2;
while (i <= 10) {
System.out.print(i + " ");
i += 2;
}
System.out.println();
/*
* 100 95 90 85 80
*/
i = 100;
while (i >= 80) {
System.out.print(i + " ");
i -= 5;
}
System.out.println();
/*
* 1 3 5 7 9 2 4 6 8 10
*/
int[] numbers = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
i = 0;
while (i < numbers.length) {
System.out.print(numbers[i] + " ");
i++;
}
System.out.println();
/*
while (true) {
infinite loop
}
*/
/*
* 1+2+3+4...+100 = 5050
*/
int sum = 0;
i = 1;
while (i <= 100) {
sum += i;
++i;
}
System.out.println("1+2+3+4...+100 = " + sum);
}
}