Journal of a Geek

Archive for the ‘Programming’ Category

Programming Basics: Conditional Statements

leave a comment »

Now that we have gone over variables, we can get into more complex types of statements in programming. This article will cover conditional statements (if then, if then else, nested if then).


If thens basically work like common English. Let’s say you were planning on buying a Wii when they come out in November, but you didn’t pre-order. Now you are driving toWal-mart and you are thinking “If Wal-mart has Wiis for sale, then I will buy one.” The code for that would look something like this:

if (walmart is selling wiis = true)
then (buy a wii)

Basically we are telling the program that if some condition evaluates to true then do what comes after the then. Here the condition being evaluate is: is it true that walmart is selling wiis? If it is true, then the whole condition evaluates to true and we buy one, if it is false, we do nothing.


Okay but let’s say you want that Wii badly. So you decide that if Walmart is selling it then you will buy it, otherwise (else) you will go to target and see if they have them.

if (walmart is selling wiis = true)
(buy a wii)
else (go to target)

Now we had the evaluation of the condition the same way, but this time instead of doing nothing when it evaluates to false we move on to the else part and execute that.


So what happens if you want this Wii really badly, but target doesn’t have it either. Well logically you would think of another store to go. This is basically how nested if then statements work. For example:

if (walmart is selling wiis = true)
then (buy a wii)
else if (target is selling wiis = true)
then (buy a wii)
else (go to best buy)

This is basically saying that if Walmart doesnt have Wiis for sell we will go to Target, and if target doesn’t have Wiis we will go to best buy. Notice we will never execute the second if unless the condition in the first is false.


I’d like to take a moment to talk about logical operators. In the previous if thens I was checking to see if something is equal to true. If you were looking at numbers instead then you could also use greater than, less than, greater than or equal to, and less than or equal to. For example I could be only want to do something if the price of a PS3 was less than 600 dollars. I could change my if then to look something like this:

if(ps3 price < $600)
then consider it a cheap blue ray player

Now let’s say you wanted to go buy the PS3 from walmart, but only if it is less than $600. The logic for this would require checking two conditions.

1) is walmart selling the PS3
2) is it less than $600

You would get something like this:

if (ps3 price < $600 AND walmart is selling ps3's = true)
then (buy a ps3)

The part of the statement will only execute if both of the conditions are true. The AND operator tells the statement that both sides of the AND must be true in order for the whole thing to evaluate to true. The other two operators are OR and NOT. The NOT operator simply tells the conditional statement that to be the opposite of whatever it is evaluating to. If something is false, it changes it to true and vise versa. The OR operator tells the conditional statement that the whole statement is true as long as one side of the OR is evaluated to true.


The real power the conditional statement and the variable comes into play here. Notice I was using PS3 price and the fact that Walmart was selling either as something I was checking. In another part of the program you would set the variable to have the PS3’s price or Walmarts status of selling something. By leaving these as variables we can check whatever is entered, instead of only checking thing that are precoded into the program.


Written by Ryan Farnell

October 11, 2006 at 5:35 pm

Posted in Programming

Programming Basics: Variables

with one comment

One of the most important concepts in programming is the variable. A variable in programming works pretty much the same way it does in algebra. A variable represents some unknown. This comes in very handy when you are writing algorithms whether in math or computer programming. For example:

x + 2

In this case, we are adding two to whatever x contains. The variable could be given a value from many different places, but in general the value is given to the variable through what is called an assignment operator. In most programming languages this operator is the equal sign.

x = 2

Here we have given the variable x the value of two. The assignment goes from right to left. So the value on the right is given to the value on the left.

Variables are very important when moving on to much more complex operations such as conditional statements and loops. The next post in this series will talk about conditional statements.

Written by Ryan Farnell

September 27, 2006 at 5:53 pm

Posted in Programming