There are three key components to any computer program:

  • Variables/Constants
  • Data Structures
  • Logic Structures

In my previous post, I explained what a “Data Structure” was, and gave some examples of where they might be used in a program. I also described the difference between three major Data Types: Integer, String, and Double.

The pieces of the puzzle are starting to fit together now to paint a bigger picture. We have the Variables and Constants, which act like storage units in a program. We have the Data Structures, which organize and describe the storage units in the program. What’s missing?

Logic Structures

Essentially, Logic Structures control the “flow” of the program, or how it gets from A to B. Remember the example I gave earlier, about an imaginary program that dealt with different types of adult drinks? It wouldn’t start unless myAge was greater than 21. How would we portray such logic with code?

This is where our first example of a Logic Structure comes into play. It is called an If Statement, which is a type of Conditional. Put simply, a Conditional Logic Structure evaluates an expression – such as “Am I greater than 21 years of age?” It then takes one of several different paths. Which path it takes is conditional on the result of the expression… hence the name, Conditional Logic Structure. Here’s how an If Statement looks in some of the most common programming languages:

Visual Basic

Java

C#

Notice how the code for Java and the code for C# is exactly the same? This is a good example of my previous statement, “Once you learn your first programming language, it becomes very easy to pick another one up.”

The nuances of the programming languages, as always, slightly differ. However, somebody with an open mind should be able to take a look at the code and, especially if it’s commented well, they should be able to at least get a basic understanding of what’s going on.

A Side Note on Commenting

Commenting is an important concept, shared by most programming languages. I’ll get into details in a later post, but for now, just know that comments are a form of documentation. They are usually natural-language annotations spread around a well-written program. In the examples above, I showed how to write a comment in Visual Basic, Java and C#, which says “You are free to enter the program.” Some people would prefer to write something along the lines of “Insert code here, for entering the program.” Either way, the compiler does not typically read comments. Therefore – think of it this way – the computer could care less what you write in the comments, but anyone reading your code might get a little lost or confused without good comments documenting the various aspects of the program.

Loops

Conditionals are not the only type of Logic Structure. There are also Loops!

Let’s pretend you wanted to write a program that prints the numbers 1 through 10. The instant it hits 11, you want it to exit. Using what we’ve learned above from Data Structures and Comments, take a look at this code:

For direct comparison, if computers could read plain English, here’s what the code would look like:

So, we can go one step further, and add plain English comments to the code listed above! This green text makes the program easily readable, by any human taking a look at the code:

For comparison, here’s the same code again, in Visual Basic:

And here it is, properly commented:

Hopefully this makes sense to both you, and the computer. Of course, you would test your program after writing it… to make sure the computer can, in fact, understand what you’re asking it to do.

Here’s what happens when you compile and run the program:

conditional-while

And there’s the above code, the while loop, in action!

Share

Comments are closed.

Post Navigation