Part Zero - Review

Write a program that takes a grade and prints "You passed!" if the grade is 65 or above and "You failed :-(" if it's below. Use the type double for the grade.

If you guys want to we can start taking human input for our programs using Scanner. Click here to use scanner or here to use ther regular main method

// This is the import statement you can load libraries
// that are not automatically loaded with java
import java.util.Scanner;

public class Lesson4 {
  
  public static void main (String[] args){
    
    //creates a new scanner object that listens to the System.in input stream
    Scanner scanner = new Scanner(System.in);
    
    //print a message to the user
    System.out.println("What grade did you get?");
    
    // declare grade and scans for a double
    // WARNING: if you dont type a double it will cause an error
    double grade = scanner.nextDouble();
    
    /**
     *  put your code here
     */
    
  }
}
        

Part One

If theres one thing computers do really well, its doing repetative tasks over and over and over again. Alot of problem solving in computer science has to do with breaking a problem down into small peices and then repeating a solution for every peice. Think about spell check in Microsoft Word, it breaks up a document into words and then runs the same dictionary lookup algorithm on each one.

In programming when we use the same block of code over and over again we call it looping. Java in particular (as well as many other programming languages) provides us with a few controll structures for iteration.

Lets write a program to illustrate what a for loop does

for( int x = 0; x <= 10; x++ ){
    System.out.println("The value of x is " + x );
}
        

Thats quite a bit to digest. Lets take a look at whats going on.

int x=0
This part does exactly what you think it does. The first time through the loop it declares an integer x and sets it to 0

x <= 10
This is the test part of the for loop. It's sort of like the test in an if statement but this test runs every time the loop iterates. When the test returns false it breaks out of the loop.

x++
this is the part that increments the variable x. You could also write it as x = x + 1 or x += 1 if you prefer.

Note that each part is seperated by a semicolon and also like the if statement the code that it loo[ps over is within the curly braces.

Part Two

We dont have to just increment our counter (also called an iterator) by one though. This is a program that adds up all the multiples of two up to 100 and prints the sum. (extra credit for math majors: use induction to find the sum of this series without looping) (just kidding)

int sum = 0;

for( int i = 0; i <= 100; i = i + 2 ){
    sum = sum + i;
}

System.out.println(sum);
            
We can also increment down instead of up. This program will count down from 20 to 10.
for( int j = 20; j >= 10; j = j - 1 ){
    System.out.println(j)
}
            
We dont always have to use addition or subtraction, we can use multiplication, division or any other expression to change the itterator.

Part Three

The for loop is not the only way to loop in java. We also have the while and do while loops. Here we are counting to 10 again but using the while loop instead. You'll see that the while loop only has a test. It does not declare an itterator or increment it.

int x = 0;
while ( x <= 10 ) {
    System.out.println("The value of x is " + x );
    x = x + 1;
}
        

Part Four

do while works in sort of the same way but it does the test at the end.

int x = 0;
do {
    System.out.println("The value of x is " + x );
    x = x + 1;
} while ( x <= 10 );
        
You generally wont have to use do while so dont worry about memorizing it.

Home Work

1. Install Dr.Java on your home computer if you haven't already!

2. Use a for loop to counts down from 10 to 0 but instead of printing 0 prints "Blast Off!" Your output should look like this:

10... 9... 8... 7... 6... 5... 4... 3... 2... 1... Blast Off!!!        
            
3. Write a program that counts all the multiples of 5 up to 50

... And if you want to learn more

Try doing the home work with both for and while loops