Vocab/Iterations
Homework Assignments
For, While, Nested Loops
- while loop
-
- executed while something is happening
-
- iterates over elements
-
- can be infinite if boolean is always true
-
do-while loop
-
- condition is checked after executed statements
-
for loop
-
- certain amt of loops thru code block
-
nested loop: loop w/in loop
-
for-each loop
-
- traverses an array in java
int x = 10;
while (i<10) {
System.out.println(i);
i++;
}
public class arrays
{
public static void main(String[] args)
{
}}
public static void main(String[] args) {
int[] numbers = {2, 4, 6, 8, 10};
int i = 0;
while (i < numbers.length) {
System.out.println(numbers[i]);
i++;
}}
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
for (int i = 5; i < 14; i++) {
System.out.println(i);
}
for (int j = 1; j <= 3; j++) {
System.out.println(j);
}//nested loop
//for-each loop
int arr[]={10,20,30,40,50};
for(int j:arr){
System.out.println(j);
}
//unit 4 hw
//number guesser
import java.util.Scanner;
import java.lang.Math;
Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine()); // reads input
System.out.println(num_float);
double random = Math.random() * 10;
System.out.println(random);
public class Number{
public static void main(String[] args){
while (num_float != random){
if (num_float < random){
System.out.println("too low. higher");
// Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine());
System.out.println(num_float);
}
if (num_float > random){
System.out.println("too high. lower");
Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine());
System.out.println(num_float);
}
}
System.out.println("you got it!");
}
}
Number.main(null)