Beginners Guide to Programming 5: Loops and Conditionals

Loops and conditionals are the basic building blocks of most if not all programs. They allow you to perform most types of useful functions one requires from a computer. From counting the number of characters someone has used to making sure the user is entering the correct form of input. Many bugs and performance issues have been tracked back to poor use of both of these, but lets just try to get a handle on using them today.

Loops are really the work horse of computer programming, but figuring out how to cleverly utilize a loop is very important. Loops have major ease of use to performance considerations, meaning that if you use ten million loops where you should have used 2 loops your program is going to run really slow. Also try to avoid infinite loops, were you create a loop never ends. This is usually not a huge deal but people have crashed their computers.

Conditional are statements are traditionally known as " If then else " statements, but they commonly come in the form of switch statements as well. Try not to think to narrowly when you think of computer programming, conditionals could come in many different forms. Conditional statements have definite pitfalls of their own. It's awfully easy to make a minor error and change the flow of your program, thereby wrecking it. When you think of a conditional statement you should be visualizing a fork in the road or turning down a street. The point is, you are really taking the program in a different direction when using a conditional, yeah I know not always but often. If you write a program that asks some one the make of their car to determine what tires are available for that model you will have a program flow ( that's potentially) like this:


Let's take a quick look at a really simple loop from python and then discuss a couple the things you may want to do with it. Python implements one of the most common types of loops the for loop, but they definitely have a bit of a wrinkle compared to other for loops. The setup of a python for loop is for a in b . Let's we have the variable b which is to a range value.

b = range( 1,11)

If we want to display every number we could walk through that range of values like so.

for a in b:
print a

You can obviously change the values of the range to count in different ways and exchange the print statement for some other logic. Like if you wanted to perform a calculation ten times or pretty much anything you want to do a certain number of times. Another common form of loop is the while loop. It works exactly as you would imagine. while some condition is true/false do this thing. For instance the following code keeps the program running a user has not entered q. It is also a fine example of what to use if statements for.

while user != "q":
print "what is your lucky number ? "
if user==7:
print "That's Pretty Lame"
print "thanks"

One more use I would like to tell you about is checking user input. Checking user input is really important, because some one stupid will probably use your program. If you are the only using it, you will do something stupid with input and some point, and without checking inputs your program will crash if you enter a letter where a number should be. I will give you the basic idea and I bet you can figure this one out on your own. Create a program that only allows users to enter numbers one through nine. Start with a while loop like the one above and use if statements to set a value that allows the user to exit the while statement.

I really could go on forever about loops and conditionals, because I feel it's the true heart of programming, but to really understand them you have got to use them! As always this tutorial is meant to be interactive, if you have questions or comments post them in the comments section.

The next post will get away from code a little bit and dig into designing a program and doing what you really want to do with it.

Spread The Love, Share Our Article

Related Posts

No Response to "Beginners Guide to Programming 5: Loops and Conditionals"

Post a Comment