Jump to navigation Jump to search

Workshop: Introduction to programming (with Python)

16 bytes removed, 22:18, 11 September 2019
no edit summary
One of the most important concepts in procedural programming is the loop. Like the name suggests, a loop simply iterates through some selected lines of code a certain amount of times. In Python there are a few different types of loops, they are:
#'''While Loop''' - Like the name suggests, a while loop repeats a block of code while a certain expression is true. This kind of loop is useful when we don't know how many times a loop will actually execute at runtime. The While Loop is composed of two parts; the loop statement and the code block to repeat (indented). The syntax looks like this:[[File:While Loop Example.png|thumb|An example of a while loop]]<nowiki><code></nowiki>'''while ('''statement'''):''' '''''*code to repeat*''''' <nowiki></code></nowiki><br />The while loop will loop over the code block until the statement in parentheses equates to false. The statement can be an algebraic equation or variable. (ex: (n+1)/4 == 1) <br />[[File:Example For loop.png|thumb|2 examples of for loops that yield the same result. ]]
#'''For Loop''' - This loop will run once for each element in a list. This kind of loop is useful when iterating over loops and arrays or when we know exactly how many times the code block should run. There are two main ways to use the for loop.
##To iterate over a list - Will run the code block once for each element in a list. You can reference the element data during each iteration by putting an element name after "for". Example of this syntax can be seen below: '''for''' element '''in''' list''':''' '''''*code to repeat*''''' <br />
*Basic Understanding of If Statements (or, and, else, elif)
*Basic Understanding of Loops (for loop, while loop)
*Make a Magic Conch Shell Demo!

Navigation menu