Fibonacci Series using For Loops
Start course
Difficulty
Beginner
Duration
3h 15m
Students
118
Ratings
4.6/5
Description

In this course, we look at control flow statements in the Java programming language.

### Learning Objectives

• Decision-making statements
• Looping statements
• Branching statements

### Intended Audience

• Anyone looking to get Oracle Java Certification
• Those who want to learn the Java Programming language from scratch
• Java developers who want to increase their knowledge
• Beginners with no previous coding experience in Java programming
• Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

### Prerequisites

• No prior knowledge is required about the Java programming language.
• Basic computer knowledge
Transcript

Hi there. In this video, we'll do another project. In this project, we'll learn a mathematical concept, the Fibonacci Series, and we'll learn how to implement it in Java. First, let's create a new Java project and specify the project name as the FibonacciSeries. Don't change other options and click the 'Finish' button. In this project right-click on the 'Source' folder and select 'New,' 'Class.' Specify package name as fibonacci and class name as Fibonacci. And select the checkbox to add the main method. Before coding, let's remember the Fibonacci sequence.

Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and so on. The next number is found by adding up the two numbers before it. For example, the next number in the sequence above is 34 + 55 = 89. Let's start to code. First, we need to import the scanner class for getting input from the user. We choose the import scanner class in java.util package to import the scanner class. We show a message to enter the max sequence number by using the print method. S.out.println("Input max sequence number:"), and we declare a variable number with int type and assign the next int method of the scanner class to this variable. Let's declare three variables with int type again.

The first one will be named fibPrevious with an initial value of zero. The second one will be named fibonacci with an initial value of one, and last, sum, with initial value of zero. Variable fibPrevious represents the first number of the Fibonacci Series and variable fibonacci represents the second number of the Fibonacci Series. Let's start to write the for loop. In the for loop, we declare the variable i and initialize it. Its initial value is one. Our test condition is i <= number and it increments by one. We use the print method to display numbers of the Fibonacci Series. So, we use variable fibPrevious for this. And on each iteration, we add the first number fibPrevious and the second number, fibonacci, and assign this sum to the variable sum. And then, we assign the second number fibonacci to the first number fibPrevious.

And we assign the sum of the last two numbers to the second number fibonacci. The program is ready. Let's examine the code. For example, the variable number can be five. So, the initial value of i is one, and one is less than five, so the condition is true. For this reason, the print method will execute, and the first number will display on the console is zero, i.e the value of the fibPrevious variable. Now, in the first iteration the sum will be one, because the fibPrevious is zero and fibonacci is one. And the sum of the zero and one is equal to one. And in the first iteration, the fibPrevious will be one, because the value of fibonacci is currently one. And in the first iteration, fibonacci will be one because the value of the sum is currently one.

Now the compiler begins the second iteration. In the second iteration, the value of i is two and two is less than five, and the condition is true. For this reason, the print method will execute again, and the second number will display on the console as one, i.e the value of the fibPrevious variable. If you notice, the value of the fibPrevious is one. Now, in the second iteration, the sum will be two, because the fibPrevious is one and fibonacci is one, and the sum of the one and one is equal to two. And in the second iteration, the fibPprevious will be one again because the value of fibonacci is currently one. And in the second iteration, fibonacci will be two, because the value of the sum is currently two.

Now, the compiler begins the third iteration. Now in the third iteration, the value of i is three and three is less than five, and the condition is true. For this reason, the print method will execute again, and the third number will  display on the console is one, i.e the value of the fibPrevious variable. If you notice, the value of the fibPrevious is one. Now in the third iteration, the sum will be three, because the fibPrevious is one and fibonacci is two, and the sum of the one and two is equal to three. And in the third iteration, the fibPrevious will be two, because the value of fibonacci is currently two. And in the third iteration, fibonacci will be three, because the value of the sum is currently three.

Now the compiler begins the fourth iteration. In the fourth iteration, the value of i is four, and four is less than five, and the condition is true. For this reason, the print method will execute again, and the fourth number will display on the console is two, i.e the value of the fibPrevious variable. If you notice, the value of the fibPrevious is two. Now, in the fourth iteration, the sum will be five, because the fibPrevious is two and fibonacci is three, and the sum of the two and three is equal to five. And in the fourth iteration, the fibPrevious will be three, because the value of fibonacci is currently three. And in the fourth iteration, fibonacci will be five, because the value of the sum is currently five. Now the compiler begins the fifth iteration.

In the fifth iteration, the value of i is five, and five is equal to five, and the condition is true. For this reason, this print method will execute again, and the fifth number will display on the console is three, i.e the value of the fibPrevious variable. If you notice, the value of the fibPrevious is three. Now, in the fifth iteration, the sum will be eight, because the fibPrevious is three and the fibonacci is five. And in the fifth iteration, the fifth previous will be five, because the value of fibonacci is currently five. And in the fifth iteration, fibonacci will be eight, because the value of the sum is currently eight. Now the compiler begins the sixth iteration. In the sixth iteration, the value of i is six, and six is greater than five, and the condition is false.

So, the for loop will terminate. So, the output will be 0, 1, 1, 2, and 3. Let's run the code. I enter five. We enter five for max sequence. You'll see the first five numbers of the Fibonacci Series in the console. This time let's debug the code. Debugging allows us to run a program interactively while watching the source code and the variables during the execution. Let's put a breakpoint ahead of the print method line in the for loop. In order to define a break point in our source code, double-click on this position or right-click in the left margin and select 'Toggle Breakpoint' for the same operation.

In order to debug our application, we can use the debug button in the Eclipse toolbar. Let's click the 'Debug' button. We enter 10 for max sequence. Eclipse asks us if we want to switch to the debug perspective. We click 'Switch' in the dialog window. Eclipse provides debug buttons for controlling the execution of the program. Also, we have some shortcut keys to step our code. You can use the F6 key or this 'Step Over' icon. Let's check the values of variables from debug perspective. We click the 'Step Over' button and display the values of variables in the variable section. At first, fibPrevious is zero, fibonacci is one, and the sum is zero. Let's click the 'Step Over' button again. The variable sum is one. Let's click the button again.

Variable fibPrevious is one. Let's click the button four times. The variable sum is two. Let's click the button again. Variable fibonacci is two. Let's click the button three times. The sum is three. Let's click the button again. Variable fibPrevious is two. Let's click the button again. Variable fibonacci is three, and goes on like that. Let's click the 'Resume' button to resume the execution of the program code until it reaches the next break point. Let's click this 'Resume' button until the execution of the program finishes. And as you can see, the fibonacci series is on the console. I think you understood how to debug your code in Eclipse by doing this project. And if you pay attention, currently the perspective is the debug. Let's change it to Java. From the upper right corner of the Eclipse, I'll click the Java perspective icon and the interface is changed. Let's take a short break here. I'll see you in the next video. 