Professional Documents
Culture Documents
PROG2 - Discussion Forum Unit 2
PROG2 - Discussion Forum Unit 2
// Do_While_For.java
public class Do_While_For{
public static void main(String[] args) {
System.out.println("######## PART 1 ########");
int i = 0;
System.out.print("While 1---------------");
while (i < 5) { // Condition is tested before entering the loop
System.out.print(" "+i);
i++;
}
System.out.println();
i=0;
System.out.print("Do_While 1---------------");
do {
System.out.print(" "+i);
i++;
} while (i < 5); // Condition is tested after entered the loop
System.out.println();
System.out.print("For 1---------------");
for (int i2 = 0; i2 < 5; i2++) { // Condition is tested before entering the loop
System.out.print(" "+i2);
}
System.out.println("\n\n");
System.out.println("######## PART 2 ########");
i=0;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.print("Do_While 2---------------");
do {
System.out.print(" "+cars[i]);
i++;
} while (i < 4);
System.out.println();
i=0;
System.out.print("While 2---------------");
while (i < 4) {
System.out.print(" "+cars[i]);
i++;
}
System.out.println();
System.out.print("For 2---------------");
for (int i2 = 0; i2 < 4; i2++) {
System.out.print(" "+cars[i2]);
}
System.out.println("\n\n");
System.out.println("######## PART 3 ########");
System.out.print("For 3---------------");
for (int i2=0; i2<4 ; i2++) System.out.print("@");
System.out.println();
i=0;
System.out.print("While 3---------------");
while (i < 4) {
System.out.print("#");
i++;
}
System.out.println();
System.out.print("Do_While 3---------------");
i=0;
do {
System.out.print("$");
i++;
} while (i < 4);
} }
OUTPUT:
Reference:
Eck, D. J. (2019). Introduction to programming using Java, version 8.1. Hobart and William Smith College.
http://math.hws.edu/javanotes.