Tuesday, August 11, 2009

What is Programming? Part II

Learning to programming takes time. No one just "knows how to do it." Programming demands that you plan ahead, think logically and creatively, be patient but persistent and pay close attention to details. Although, learning to program can be a challenge, it can also be a lot of fun!

All programming languages have 8 basic concepts you will need to master. Although, the techniques each language uses to deal with these concepts differs...slightly...once you have the basics behind these concepts down learning any computer language becomes easier.

  • Symbolic Assignment - All languages have named storage areas where the programmer can assign data. These symbolical storage areas can hold data that can vary - variables as well as data that will never change - constants. Different languages deal with these storage areas and how you assign values to them differently: some require that all storage locations be declared by name in a special area before being used, some require that the type of data that will be stored be defined in some detail when the variable/constant is declared, some are very strict about what can be stored in a variable and some are very lax. But all languages have user-defined, named, storage locations and all allow the program to assign values to those locations and retrieve the data stored in them.

  • Syntax - Syntax refers to the "grammar" of a programming language. Yes, programming languages have a grammar! Typically, you will use spaces to separate words, periods, commas and semi-colons will have meanings and how you lay out a "statement" will follow a format...like for example: verb noun . . . PRINT "Hello World"

  • Sequence - This is the order in which the statements will be read or executed. This is almost always from left to right, top to down in the source code, with statements separated by some sort of punctuation. In C type languages, for example, the semi-colon ";" is used to separate statements, while in CoBOL the period "." has the same purpose. Almost all languages allow you to "block" several statements together...think of this as being sort of like a paragraph. In fact, that is what it is called in CoBOL! In C type languages a block of code will be surrounded by curly braces " { statement; statement; }

  • Selection - This is how the programming language makes decisions. "Perform this statement if this is true, and that statement if this is false." Most languages use some variation on the If (true/false evaluation) block to do if true; else block to do if false.

  • Iteration - Often you will want a program to perform the same statements over and over until something is accomplished. To do this you create a loop using while, do/while, or for instructions. In a way these are like Selection, but rather than "do this block if true", it becomes "continue doing this block while this is true."

  • Input/Output - All programs will have ways to get data into a program and to get information back out of the program. Here the program is usually dealing with external hardware: the keyboard, the screen, the file-system on a hard drive or perhaps across a network. This sort of interaction tends to be pretty complex, so almost all higher level languages have extensive libraries already created to deal with this. In C++ you might write a simple: cout << "Hello World!"; to display Hello World on the screen, but what is really happening is that you are calling a library where maybe an entire page of code has already been written defining exactly how you get those two words to appear on a screen. As an aside, the majority of that code in the library is dealing with the <<>
  • Branching - Deprecated! Deprecated means you shouldn't use this, because it has been superceded and may go away in the future. While I don't expect branching to disappear from computer languages, you still shouldn't use it! Okay, having said that, what is branching? It means jumpping from one statement to another somewhere else in the program...in other words, not following the normal sequence. The reason this is frowned upon is that if a programmer does a lot of branching in a program it becomes harder and harder to understand what he was doing and why he was doing it that way. Where you can find branching you will find the goto statement.

  • Subroutines - This is what has taken branching's place! A subroutine, also known as a module, procedure or function, is a block of code with a name. As a program executes there is an "instruction pointer" that moves along letting the computer know what it is to execute next. When a program "calls" a subroutine's name the next "instruction pointer" points at the beginning of the subroutine's code. The computer executes the block of code inside the subroutine, and when it finishes the subroutine issues a return instruction which causes the "instruction pointer" to return to where it came from in the calling program. This sounds more complex than simple branching, but it reads much easier in actual programs. Subroutines have a number of other advantages over branching as well.
Once a programmer understands these 8 programming concepts he/she can pick up new "procedural" languages pretty easily.

Object-Oriented Computer Languages (like Java, C++/C# and Smalltalk) use the above 8 programming concepts and add 4 more:
  • Abstraction
  • Data Hiding
  • Inheritance
  • Polymorphism
I'll write about those later.

No comments:

Post a Comment