Iteration

This page is under construction. Please come back later.
import org.pureprogrammer.Utils;
public class Iteration1 {
public static void main(String[] args) {
int max = 5;
if (args.length == 1) {
max = Utils.stoiWithDefault(args[0], 5);
}
for (int i = 0; i < max; ++i) {
System.out.println("Hello, world!");
}
}
}
Output
$ javac -Xlint Iteration1.java
$ java -ea Iteration1 5
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
public class Iteration2 {
public static void main(String[] args) {
for (int i = 10; i >= 1; --i) {
System.out.println(i);
}
System.out.println("Blastoff!");
}
}
Output
$ javac -Xlint Iteration2.java
$ java -ea Iteration2
10
9
8
7
6
5
4
3
2
1
Blastoff!
public class Iteration3 {
public static void main(String[] args) {
System.out.println("Even numbers up to 100...");
for (int i = 2; i <= 100; i += 2) {
System.out.println(i);
}
}
}
Output
$ javac -Xlint Iteration3.java
$ java -ea Iteration3
Even numbers up to 100...
2
4
6
8
10
12
14
16
18
...
82
84
86
88
90
92
94
96
98
100
public class Iteration4 {
public static void main(String[] args) {
System.out.println("Floating point numbers");
for (double d = 1.0; d < 2.01; d += 0.1) {
System.out.println(d);
}
}
}
Output
$ javac -Xlint Iteration4.java
$ java -ea Iteration4
Floating point numbers
1.0
1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.5000000000000004
1.6000000000000005
1.7000000000000006
1.8000000000000007
1.9000000000000008
2.000000000000001
public class Iteration5 {
public static void main(String[] args) {
System.out.println("Powers of 2 up to 256!");
for (int i = 1; i <= 256; i *= 2) {
System.out.println(i);
}
}
}
Output
$ javac -Xlint Iteration5.java
$ java -ea Iteration5
Powers of 2 up to 256!
1
2
4
8
16
32
64
128
256
import org.pureprogrammer.Utils;
public class Iteration6 {
public static void main(String[] args) {
String s = "Hello, world! 🥰🇺🇸";
for (String c : new Utils.StringCodepointsIterable(s)) {
System.out.println(c);
}
}
}
Output
$ javac -Xlint Iteration6.java
$ java -ea Iteration6
H
e
l
l
o
,
w
o
r
l
d
!
🥰
🇺
🇸
Iterators
javaQuestions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Amortization Table
- Amortization Table (Revisited)
- Blackboard
- Blackboard (Revisited)
- Character Types
- Compute π (Monte Carlo)
- Floating Point Error
- ISBN-10 Check
- License Plate Generator
- Multiplication Table
- Number Guessing Game
- Punchcard Message
- Random Floats
- Random Integers
- Supermarket Sale
- Supermarket Sale (Revisited)
- Temperature Table
- Unicode Test
References
- [[Java Language Specification]], Java SE 21 Edition, Gosling, et. al., 2023.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]
Pure Programmer


