Today I did a Java lecture on for loops. Broadly my understanding of for loops is that they allow you to automate a looping process throughout a defined range of values. This means you don’t have to write a line of code for every single value you can just define the rules and the computer will do the process for you.

The first program I did today is found below. Prints out the total interest for a range of values based the rate and base.

public class Main {

    public static void main(String[] args) {

        for(int i=8; i>1; i--){
            System.out.println("10,000 at " + i + "% interest is " + String.format("%.2f",calculateInterest(10000, i)));
    public static double calculateInterest(double amount, double interestRate){
        return(amount * (interestRate/100));

The way it works is there is a method which calculates the interest based on the amount and interest rate. It returns the interest calculated.

Then above that is a for loop. It uses the integer i and defines it as 8 to begin with. It counts backwards by 1 and stops when it gets to 2. It prints out a line for each of these values of 8 to 2.

Notice how the variable is is used in the print line statement and also as a parameter in the calculate interest method, which is also referred to.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s