Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Programming: Intro

Do you wish you could change some of your software to work just the way you want it to? Do you sometimes think "I could do better than THIS!" when your software crashes? Well, maybe you can!! It will take some work, of course.

What you'd have to learn is how to program your computer. While I can't teach you how to do that in this series of lessons (Breathe a sigh of relief now!), you can learn a little about what programming is all about.

What is a computer program?

Simply put, a computer program is a set of detailed directions telling the computer exactly what to do, one step at a time. A program can be as short as one line of code, or as long as several millions lines of code. (We'll hope those long ones do a lot of different and complex things!)

Language Types

Programming has changed a lot since the first computers were created. The original programs were very simple and straight forward compared to today's elaborate databases, word processors, schedulers, and action games.

Different computer languages have been created with which to write these increasingly complex computer programs. They can be categorized based on how close to normal speech they are, and thus how far from the computer's internal language.

Machine Languages The language of the CPU (The central processing unit of the computer, which is the part that does the "thinking"). The lowest level language. Composed of 0's and 1's
Assembly Languages Abbreviations for machine language
High-Level Languages Use program statements - words and algebra-type expressions. Developed in the 50's and 60's. After a program is written in one of the high-level languages, it must be either compiled or interpreted. A compiler program rewrites the program into machine language that the CPU can understand. This is done all at once and the program is saved in this new form. A compiled program is generally considerably larger than the original. An interpreter program translates the program statements into machine language one line at a time as the program is running. An interpreted program will be smaller than a compiled one but will take longer to execute.
4th Generation Languages = 4GL. Very high-level languages. These are results oriented and include database query languages like SQL. There are fewer options for programmers, but the programs are much easier to write than in lower level languages. These too must be compiled or interpreted.
Natural Languages 5th Generation Languages. We don't really have any programming languages yet that use natural language. In such a language you would write statements that look like normal sentences. For example, instead of odd-looking code you would write "Who are the salesmen with sales over $20,000 last month?"

 

Programming: Languages

Many computer languages are available for writing computer programs. They each have advantages for certain kinds of tasks.



Let's check out some examples of the various types of computer languages and see what they are used for.

Machine Language = The native tongue of the CPU. Each design for a CPU has its own machine language. This is the set of instructions that the chip uses itself. So it is made up of sets of 0's and 1's, that is binary numbers. Very hard for people to work with.  
10 23 11 FF 12 12 13 10 14 50 15 23 16 30 17 40 18 C0 19 00   Machine language looks like it's just numbers. In the program segment at left the first column tells the computer where to fill memory and the hexadecimal (base 16) numbers in the second column are the values to put into memory at those locations. For more information on hexadecimal numbers, see Base Arithmetic.

Here's another example of machine language.

The segment of Java code:

int counter = 0;

counter = counter + 1;

might be translated into machine language as:

Assembly Language = Codes or abbreviations for the machine language instructions In an assembly language each machine language instruction is assigned a code. So instead of having to remember a string of 0's and 1's, the programmer would only need to remember short codes like ADD, MOV, or JLE. Certainly an improvement over 000101000100010001000100001000101010111110!! But not really "user friendly" either. The assembly language program below reads two characters and prints them on the screen. The text to the right of the semicolons ( ; ) is ignored by the computer. It's there to explain the program to anyone looking at the code. Notice that each little step must be coded. All this just to display 2 characters!
;name of the program:one.asm ; .model small .stack .code mov AH,1h ;Selects the 1 D.O.S. function Int 21h ;reads character and return ASCII ; code to register AL mov DL,AL ;moves the ASCII code to register DL sub DL,30h ;makes the operation minus 30h to ; convert 0-9 digit number cmp DL,9h ;compares if digit number it was ; between 0-9 jle digit1 ;If it true gets the first number ; digit (4 bits long) sub DL,7h ;If it false, makes operation minus ; 7h to convert letter A-F digit1: mov CL,4h ;prepares to multiply by 16 shl DL,CL ;multiply to convert into four bits upper int 21h ;gets the next character sub AL,30h ;repeats the conversion operation cmp AL,9h ;compares the value 9h with the content ; of register AL jle digit2 ;If true, gets the second digit number sub AL,7h ;If no, makes the minus operation 7h ; digit2: add DL,AL ;adds the second number digit mov AH,4CH Int 21h ;21h interruption End ;finish the program code
FORTRAN = Formula Translation FORTRAN was created around 1957 to help scientists, engineers, and mathematicians write programs that describe complex situations, like nuclear power plant monitoring, nuclear explosions, and space flight. This is still a widely used language. It was the first successful high-level program. Newer versions have been released. The most recent standard version is Fortran 2008 which was finalized Sept. 2010. (Originally FORTRAN in all caps was required but recent version names can use normal case.) The example Fortran program below accepts the bus number 99 and displays the command "TAKE BUS 99"
PROGRAM IDEXMP INTEGER BUS_NUM BUS_NUM = 99 WRITE(*,*) ' TAKE BUS ', BUS_NUM END  
COBOL = Common Business Oriented Language COBOL was written about 1960 with business applications in mind. It has a very English-like structure, using sentences and paragraphs, though they are certainly different from those in a novel. This helps business people who are not high-powered programmers to be able to write or edit a program. But it has the disadvantage of tending toward wordy, lengthy programs. It is a good language for direct, simple programs. COBOL was used to create many programs for the main frames of large companies. These programs were upgraded during the Y2K fixes for the year 2000. So it seems likely that COBOL programs will be around for a long time yet. [See the article at http://www.infogoal.com/cbd/cbdz009.htm for a longer discussion of this issue.] The example below accepts two numbers, multiplies them, and displays the numbers and the result. Look at the PROCEDURE DIVISION to see where the calculation is done.
  $ SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. FragmentA. AUTHOR. Michael Coughlan.     DATA DIVISION.   WORKING-STORAGE SECTION. 01 Num1 PIC 9 VALUE ZEROS. 01 Num2 PIC 9 VALUE ZEROS. 01 Result PIC 99 VALUE ZEROS.   PROCEDURE DIVISION. Calc-Result. ACCEPT Num1. MULTIPLY Num1 BY Num2 GIVING Result. ACCEPT Num2. DISPLAY "Result is = ", Result. STOP RUN.
BASIC = Beginner's All Symbolic Instruction Code This language was written in 1964 (truly the age of dinosaurs for computers!) for college students to use to learn programming concepts. Originally BASIC was intended only for classroom use. But the language has proven to be highly useful in the real world. A wide variety of "dialects" of BASIC developed through the years. Visual Basic is now very popular for programming Windows applications. Microsoft Visual Basic for Applications is an example of a subset of BASIC that is modified to help users write small subprograms called scripts or macros for use with applications like MS Word. Some applications used to have their own variety of BASIC for writing their macros, like Word Basic and Excel Basic. The creators of BASIC wanted a language that felt more like regular English. So while it doesn't LOOK much like English, it uses enough of the syntax of English to give it a more natural feel than many other computer languages. The short program below is written in BASIC. It accepts a distance in miles, yards, feet, and inches and converts it to kilometers, meters, and centimeters. Notice how the programmer can write equations to do the calculations.
' English to Metric Conversion ' J. S. Quasney ' **************************** PRINT : PRINT "English to Metric Conversion" PRINT INPUT "Miles: ", Miles INPUT "Yards: ", Yards INPUT "Feet: ", Feet INPUT "Inches: ", Inches Inches = 63360 * Miles + 36 * Yards + 12 * Feet + Inches Meters# = Inches / 39.37# Kilometers = INT(Meters# / 1000) Meters# = Meters# - 1000 * Kilometers Final.Meters = INT(Meters#) Centimeters = Meters# - Final.Meters Centimeters = 100 * Centimeters Centimeters = INT((Centimeters + .005) * 100) / 100 PRINT PRINT "Kilometers:"; Kilometers PRINT "Meters:"; Final.Meters PRINT "Centimeters:"; Centimeters END
C Originally created for writing system software, C has evolved into C++. Both are widely used by programming professionals for all sorts of programs. The program below is written in C++. It accepts 3 numbers and checks to see if the third is equal to the difference of the first two.
#include <iostream.h> void main() { int a, b, c; cout << "Please enter three numbers\n"; cout << "a: "; cin >> a; cout << "\nb: "; cin >> b; cout "\nc: "; cin >> c;   if (c=(a-b)) { cout << "a: "; cout << a; cout << " minus b: "; cout << b; cout << " equals c: "; cout << c << endl; } else cout << "a-b does not equal c:" << endl; }
Java The language Java is used to write both full computer applications and small applets for web pages. Its goal is to create applications that will run on any computer, unlike other languages which are not cross-platform. (By the way, don't confuse Java with JavaScript, a scripting language commonly used on web pages. The only thing they share is the letters in their names! JavaScript started life as LiveScript in about 1994. Netscape bought it and renamed it, apparently for marketing reasons.) The example below draws a box on an HTML page and counts the number of times you have clicked inside the box.
import java.applet.*; import java.awt.*; public class exfour extends Applet { int i; public void init() { resize(300,300); } public void paint(Graphics g) { g.drawString("You clicked the mouse "+i+" Times",50,50); } public boolean mouseUp(Event e, int x, int y) { i++; repaint(); return true; } }

 


Date: 2015-01-11; view: 1081


<== previous page | next page ==>
Malware: What you need protection from | Programming: Creating
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.009 sec.)