Showing posts with label general. Show all posts
Showing posts with label general. Show all posts

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.

What is Programming? Part I

Computer Programs are made up of a series of instructions that the computer executes to perform some desired task. The process of defining these instructions that the computer will execute is called programming. You normally store the instructions in a standard text file (ASCII file), also known as the source code, which is then either interpreted by a program called, appropriately enough, an interpreter into "machine language" (the binary codes a computer actually executes) or is compiled by a program...guess what, it's called a compiler. Interpreted code typically is executed instruction by instruction immediately by the computer. Compiled code is saved to a file in its binary format for later execution.

Compiled code, also known as object code, is then run through another program called a Linker to link it to any "libraries" of code the program referenced. Once a compiled program has been linked it is known as an executable and will often have a three letter extension of .EXE or .COM.

Keep in mind that programmers, not being stupid, don't want to re-invent the wheel every time they write a program, so they have created many, many, libraries of code over the last 50 plus years. Today almost all programs will link into code already written by those that have come before. Today's programs are often "frameworks" that provide calls to code in many of those libraries .

When we talk about a computer language we are generally talking about an English-like language that uses words and syntax to make up statements that we, humans, can easily read and understand. These English-like languages are called higher level computer languages. Higher level languages like Ada, BASIC, C, C++, C#, CoBOL, FORTRAN, Java, and Pascal...to name just a few...are how most programs are written today. The various languages have their own strengths and weaknesses, just as a carpenter won't try to use a single tool for every job, a programmer won't use a single language for every program. You should learn several!

There are also low level computer languages, called assemblers, that are much harder for us to read and understand. Assemblers typically allow a programmer to more finely detail exactly what they want a computer to do, so are used mostly to "tweak" programs rather than to write entire programs. Today, few programmers learn assembler, or for that matter how to work directly with machine code, however, if you plan to make a career of programming, you might want to consider learning how to work with these lower level languages.