WHILE LOOPS AND INTRODUCTION TO FIBONACCI PROGRAMMING
imgsource
Hello friends, Welcome to I learn programming(ILP) - EP 4, in which, I teach myself how to code. I believe if I have a formal documentation about my progress, I will be less complacent, I will learn and also share my knowledge and encouragement to potential programmers out there.
In the last episode, we discoursed about iterative code, we will be taking a look at the loops which is an integral part of iterative codes.
Iteration is simply defined as the repetition of some specific instructions until some conditions are met. One of the ways you can perform iterations is by using the 'while' or 'for' loops.
The while construct consists of a block of code and a condition/expression. The condition/expression is evaluated, and if the condition/expression is true, the code within the block is executed. source
Basic syntax for while loop
while < condition >
< block of code to be executed if conditions remains true >
As you can see from the flowchart, the while loop is basically set to keep running if a condition is met to be true. a common characteristic of a while loop is the counter-(which counts how many times the loop runs), the counter can also be used as a condition to test the while loop.
Examples using while loops (I will be explaining each line)
This program is used to find the sum of integers within a range entered by the user
Some variables have to be defined before the programme begins, the summation variable will be used to store the summation, it is initially set to zero because we have not performed any addition yet in the code, the second variable "counter" is used to count how many times the loop runs, the value of the counter will also be used to test and exit the while loop later on.
Next, an input syntax is used to accept information from the user, the information is going to be an integer data type, and it is stored in the variable "n".
Next, we have the while loop.
In other to understand the while loop, it is important to know what we are testing it against, so we first have to know the test condition(count<=n) our goal is to keep adding the integers within the range(n), so let's say the user enters 10, it will add(1+2+3+4+5...+10).
so we use two variable("count" and "n") in the test conditions. "n" is the range entered by the user, while, the "count" will be increased by 1 per each loop run, and will also be tested each time, the test condition states that as long as the count is less and equal to the range(n), the loop will keep running. as count keeps increasing and reaches 10, then the loop ends.
That is our condition to break out of the loop, now to get the summation we can just simply add the counter to the summation each time the loop runs. i.e: when the count is 1, it adds and stores 1 in summation, when the counter is 2, it adds 2 to 1 and stores 3 in summation, it continues in this manner until the range(n) reaches 10.
using the print function syntax is used to display the quoted string, and also the result displayed to the user screen.
The execution of the code is displayed below, the user entered an integer '10' which is the range used in the while loop condition and tested against the counter per each run of the loop.
This simple code below uses the while loop to accept the correct password entry
The password variable is set to "steemit" before the while loop and its conditions, after the user inputs the information, the input is checked against the loop condition until the password input is equal to "steemit"
The program is executed as shown below:
Introduction to Fibonacci programming
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:
0, 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , … source
A Fibonacci sequence is also known as nature's code or golden ratio, as this sequence is found in so common in nature.
This program uses the while loop to display the Fibonacci sequence within the range entered by the user
Like most programs, some variable has to be defined so they could be used later on in the program, the first two numbers in the Fibonacci sequence are constant and set to (0 and 1), the counter is used within the loop to count each turn of the loop.
The next line of code is used to accept input from the user to determine the end of the range for the Fibbonaci sequence, the print function is placed outside the following loop because we only need to print that quoted stings just once.
The explanation of this code could be seen visually.
The basic explanation for the Fibonacci sequence algorithm is as follows, the sequence has to be printed one after the other, n1 is going to be the printed value and its value is going to be changing throughout the loop, firstly the initial value of n1 is printed, when it gets printed, it's then added to the second variable "n2" and stored into the "total" variable. the value of "n1" is then transferred and stored inside "n2", "n2" now becomes the former value of "n1". "total" now contains our next printed value, so it is stored into "n1". "n1" is now changed and it is going to be printed later at the next loop. the value of "n2" is also changed and will be added to "n1" to get a new total in the coming loop.
The executed program is as follows, the user enters the value 15 which is run through the alogrithm and prints out the Fibonacci sequence.
Thanks for reading, there are also many other examples involving while loops and the Fibonacci sequence, the trick is to keep practicing and I hope to continue doing just that. see you all next time :-)
Episodes: 1, 2,3
Resources
EDX.ORG(MIT website) - INTRODUCTION TO COMPUTER SCIENCE USING PYTHON
LEARN PYTHON THE HARD WAY
OFFICIAL GUIDE FROM PYTHON WEBSITE
WIKI BOOKS
imgsource