Unit 4 Iteration
Iteration
- Basically makes code a lot shorter
- You need an operator
- ++ or --
- Postfix or prefix mode
- Examples
- WHile loop
- for loop
- recursion loop
- nested iteration
While loop
- Keeps running while something is true
- Until the condition is false it'll stop
For loop
- For as long as a condition is met, something will keep occuring
- For each part of the loop
Recursion Loop
- You have a function and in that function you put that function itself inside
- Function calls itself to repeat (recurses)
Nested iteration
- Not a method but rather a technique
- Putting a loop inside of a loop
import java.util.Scanner;
public class Checker
{
public static void main(String[] args)
{
int number;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get a number from the user.
System.out.print("Enter a number in the range of 1 through 100: ");
number = keyboard.nextInt();
while ()
{
System.out.print("Invalid input. Enter a number in the range " +
"of 1 through 100: ");
number = keyboard.nextInt();
}
}
}
Checker.main(null);
public class LoopConversion
{
public static void main(String[] args)
{
//convert to for loop
for (int count = 1; count < 5;count++)
{
System.out.println("count is " + count);
}
}
}