1

Very new Java programmer and I'm trying to get myself around this Fibonacci problem. (Leaving out the import/class defines

    Scanner sc = new Scanner(System.in);
    System.out.print("Put in how many you want to input");
    numToPrint = sc.nextInt();
    sc.close();

    int current = 1;
    int last = 0;
    System.out.println(last);
    System.out.println(current);

    // This is the section I don't really understand.
    int lastlast;
    for (int c =2; c < numToPrint; c++){
        lastlast = last; //How does last variable change from 0 as assigned from above?
        last = current; // How does current variable change from 1? 
        current = lastlast + last;
        System.out.println(current);
        }
      }
2
  • I don't understand your question but I think you have a little issue: a = b means that you're assigning value b to var a and not the other way around. Commented Jan 25, 2015 at 21:00
  • I suggest you step through the code in your debugger and you will be able see what each line does. Commented Jan 25, 2015 at 21:01

2 Answers 2

2

As the OP is a very new Java Programmer, I thought it might be be helpful to give a small tutorial, as one might in beginners class.

The other's that have responded have been correct, but everyone has to start somewhere.

OK. The section you don't understand has several integer variable, which are the names of storage locations in the computer's memory. I'll draw them out to show what they are storing (at the moment they are empty):

  .---.      .---.  .---.  .---.  .---.
  |   |      |   |  |   |  |   |  |   |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

Now in Java, new variables are initialised to zero when the program starts. (This is not true of all languages BTW).

I'll Set the values to those they hold after reading (say) 4 and positioned at the comment:

//This is the section I don't really understand

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 1 |  | 0 |  | 0 |  | 0 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

Now, moving on a couple of lines, we start the loop:

for (int c =2; c < numToPrint; c++) {

We can see thatc < numToPrint is true so we continue:

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 1 |  | 0 |  | 0 |  | 2 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

The next two lines get executed:

lastlast = last; last = current;

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 1 |  | 1 |  | 0 |  | 2 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

Then the next line is:

current = lastlast + last;

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 1 |  | 1 |  | 0 |  | 2 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

Then: System.out.println(current);

This outputs "1"

At the bottom of the loop we increment c by one:

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 1 |  | 1 |  | 0 |  | 3 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

and then back to the top to compare c < numToPrint which is still true, thus we continue:

The next two lines get executed:

lastlast = last; last = current;

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 1 |  | 1 |  | 1 |  | 3 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

Then the next line is:

current = lastlast + last;

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 2 |  | 1 |  | 1 |  | 3 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

Then: System.out.println(current);

This outputs "2"

At the bottom of the loop we increment c by one:

  .---.      .---.  .---.  .---.  .---.
  | 4 |      | 2 |  | 1 |  | 1 |  | 4 |
  '---'      '---'  '---'  '---'  '---'
numToPrint  current last  lastlast   c

and then back to the top to compare c < numToPrint which is now false, so the program ends.

Hopefully that helped you understand the code a bit more? (Courtesy of emacs picture edit mode and a cold beer!)

Sign up to request clarification or add additional context in comments.

Comments

1

"How does last variable change from 0 as assigned from above?" Because you assign the value in current to last with

last = current;

"How does current variable change from 1? " Because you assign the sum of lastlast and last to current with

current = lastlast + last;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.