ITERATIVE CODE, BRANCHING PROGRAMS AND LOOPS [ILP-EP#3]
imgsource
Hello friends, Welcome to I learn programming(ILP) - Day #3, 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 this episode, we will be taking a look at the iterative code and its control flow(i.e; how the code moves from step to step).
ITERATIVE CODE AND BRANCHING PROGRAMS
Iteration, in the context of computer programming, is a process wherein a set of instructions or structures are repeated in a sequence a specified number of times or until a condition is met. When the first set of instructions is executed again, it is called an iteration. When a sequence of instructions is executed in a repeated manner, it is called a loop. source
An iteration consists mostly of branching programs(conditional statements of if's to test for true or false) and loop codes(while or for loops).
BRANCHING PROGRAMS
When an "Algorithm" makes a choice to do one of two (or more things) this is called branching. The most common programming "statement" used to branch is the "IF" statement. source
From the chart, you can see that the control flow of the code is too move through the test(using an if-statement) to see if it is true or false, based on the result, the code goes through different sets of instructions and finally ends.
ITERATIVE CODE
Example of a branching code
It has become customary for me to break down sections of the code and explain each line when discussing about codes, not only does it help me to understand the algorithmic thinking, it will also help my readers understand the code.
This is a code to accept input from the user and check if the input is greater or lesser than 7 and nothing else, it does not test for equality to 7.
The first line is to accept the integer input from the user and store in the variable a.
Next, the control flow moves to the true block, which prints a true statement to notify the user if the conditions are met.
Next, the code is implemented using the 'elif-statement', which is basically saying that, "if the previous condition is not met or fails, try this condition to see if it could be true", you can have as many 'elif-statements' as you want, depending on your algorithm.
Lastly, an else statement is used to exit the program if all the previous conditions have failed.
The executed program is as shown below
It is important when programming to try to imagine real-life scenarios of your code execution, When running the code, I entered a letter and observed that my code crashed before it launched because the code was designed to only accept integers, but what if the user mistakenly enters a letter, I wanted to write a better one that seems smoother and gives room for user error before crashing, and that brings us to the while loop which is an extension of iterative programming.
A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true. source
The while loop in this code is used with the "try and except" conditions, like the name implies, "try" means to try a particular block of code. "except" means that if the try condition is not meet then it execute the except block code. it is like a more simpler 'if-statements'.
Example of an Iterative code
This is a code to accept any input from the user including errors and check if the input is greater or lesser than 7 and nothing else, it also checks for equality to 7.
This line of code is used to accept any input from the user, notice that the int(integer) function is not used here, it does not crash when the user inputs a letter.
From the definition of the while loop statement, it keeps executing the code block as the statement remains true, therefore a "while True" statement runs for an infinite number of times.
This line of code uses an infinite loop because we are unsure of how many times the user will make an error and enter a non-integer value.
If any code within the try statement causes an error, execution of the code will stop and jump to the except statement.source
which means that if the entered value is not an integer, it will jump and move to the 'except-statement'.
The except code does not have a break-statement because it will keep running under the infinite while loop as long as the user keeps making an error and entering a non-integer value.
When the user has successfully entered a valid integer entry, It breaks from the try-statement, out of the loop and into the if-statement written earlier, the only difference is that the final else-statement is set to display that "the value is equal to 7" because we are now sure that the user has entered an integer and if the integer is not greater or less than 7 then it means it is equal to 7.
The execution of the program can be seen below, as you can see, when I entered the error value of 'k' and 'm', the program did not crash, it simply tested it against my infinite loop and will keep returning an error message and accepts a new value and test it again, until I enter a valid integer, then it breaks out of the loop and into my (if-statement-branching-program) and returns my result.
With this examples, we can now see how an iterative code is implemented in a loop, we are also able to gain insight on branching programs, I hope I have been able to spark your interest in programming, as for me the journey continues, in the next episode we'll be looking at 'while' and 'for' loops and how they can be used in an iterative code.
Resources
EDX.ORG(MIT website) - INTRODUCTION TO COMPUTER SCIENCE USING PYTHON
LEARN PYTHON THE HARD WAY
OFFICIAL GUIDE FROM PYTHON WEBSITE
imgsource
Wow!!!
You wanna burst my brain
lol, if you are interested, we can learn together. I posted my resources, and you can start blogging about it also. programming is a good skill to acquire, thanks for passing by @biggijay
Well it sure looks difficult 🏃🏃🏃🏃🏃
Let me see if I can do it!!
Wow i am very much interested in programming...especially python
It's one of the easiest programs languages to get started with @dee-y. just start and you'll see :-)
Yeah so have gathered. Was learning the language before through an e-book until i got stuck in an exercise and couldn't figure out why i was getting the error i was getting.
Although now am learning coding with Fav54
Nice one bro... I did post my resources under my post, you can check it out for further studies.
You have been upvoted by the @sndbox-alpha! Our curation team is currently formed by @jeffbernst, @bitrocker2020, @zord189, @aaronleang, @jrswab & @teachblogger . We are seeking posts of the highest quality and we deem your endeavour as one of them. If you want to get to know more, feel free to check our blog.
Thanks a lot @sndbox-alpha
very cool man... to wrap your brain around code and share the experience... thanks.
thanks @buttcoins --- just trying to learn something new, each day :-)