We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10
Programming Language for SS1
Definition of Terms
Program : A computer program is a sequence of related instruction
(command) that tell the computer how to accomplish a specific task. A program can also be defined as a set of instruction that is executed by the CPU. Programming: Programming is the act of writing computer program. A computer program are written by trained and qualify people called programmer. Computer language: Computer language is a language used by, or associated with the computer. Programming Language: A computer programming language is an artificial language that can be used to control the behavior of a machine particularly a computer. Programming language is a means through which programmer communicate with the computer in solving different categories of problems. A set of rules governing how the words in the language are written is called syntax and the meaning associated with each word is called semantic. Markup languages like HTML are generally not regarded as programming languages, but they are computer language. Programming language foster the communication of programs among programmers and computer; markup language communicate the formatting or structure of document among human and computer. Levels of Programming Language There are three levels of programming language 1. Machine Language 2. Low Level Language (Assembly Language) 3. High Level Language Machine Language This was the first generation programming language. A computer will only understand one language, which is the machine language. There are two symbols in machine language; these are 1 and 0 generally called binary digits or bits. Advantages 1. Machine language makes fast and efficient use of the computer. 2. It requires no translator to translate the code. It is directly understood by the computer. Disadvantages 1. All operation codes have to be remembered 2. It is machine dependent 3. It is hard to amend or find errors in a program written in the machine language. Low Level Language A low Level programming Language is a programming that provides little or no abstraction from a computer’s microprocessor. The word low does not imply that the language is inferior to high level programming languages but rather refers to the small or nonexistent amount of abstraction between the language and machine: because of this, low level languages are sometimes described as being “close to the hardware. The example of low-level language is the assembly language, and it was the second generation programming language, or 2GL. It was developed to overcome some of the many inconveniences of machine language. Assembly language is a mnemonic representation of machine language. It is one level above machine language. A program for translating low assembly language is called assembler. Advantages of Assembly Language 1. It is easier to understand and use as compared to machine language. 2. It is easy to locate and correct error as compared to machine language 3. Program written in assembly language executes faster than that of high level language. Disadvantages of Assembly Language 1. Assembly language, like machine code is also machine dependent 2. Difficult to remember the syntax. High Level Programming Language These are programming languages that allow for programs to be written in forms that are readable to human beings. A high level language is a programming language that, in comparison to low level programming languages, may be more abstract, easier to use, or more portable across platforms. Examples of High Level Language include a. PASCAL b. BASIC (Beginners All-purpose Symbolic Instruction Code) c. C ++ d. Java e. FORTRAN (Formula Translation) f. COBOL (Common Business Oriented Language) g. PROLOG h. ALGOL (Algorithmic language) i. APL (A Programming Language) j. RPG (Report Program Generator) k. Python Advantages of High Level Language 1. It is easier to learn and use 2. It user friendly 3. Programs in high level language requires less time to write 4. It is easier to maintain 5. It is machine independent 6. It does not require programmer to have knowledge of the computer hardware architecture. Disadvantages of High Level Languages 1. It takes time to execute or run because it must first be translated into machine code by a translator before it can be executed. 2. Programmers need to remember a large set of syntax and semantics. Comparison of Machine Language, Low Level Language (assembly language) and High Level language Different Languages Characteristics or Features 1. Machine Language 1. Machine dependent 2. Uses special code and the assignment of storage location. 2. Low Level Language (Assembly language) 1. Machine dependent 2. Uses mnemonics(symbolic operation code) and operand (symbolic storage address) 3. 1 to 1 language 3. High Level Language 1. Machine independent 2. Uses instructions that seem English like 3. 1 to many language, i.e. for one high level instruction, many machine level statement may be generated. Comparison of levels of Programming Languages MACHINE LANGUAGE ASSEMBLY LANGUAGE HIGH LEVEL LANGUAGE Difficult to learn, write and debug It is difficult but easier than machine language It is easier to learn, write and debug It is machine dependent Machine dependent Machine independent Does not require a translator Requires a translator called Assembler Requires a translator called a compiler or interpreter Instructions consist of binary digits (1’s and 0’s) Instructions consist of symbols and mnemonics Instructions consist of English-like words, symbols and numeric. Examples of Programming Language The following are some of the many programming languages: Ada, Algol, APL, BASIC, C, C++, C#, COBOL, FORTRAN, Java, JavaScript, LISP, Pascal, PHP, Perl, Python, Visual Basic etc. TOPIC 5: BASIC Programming language The BASIC programming language was developed in 1964 by John G. Kemeny and Thomas Kurtz at Dartmouth College. BASIC is an acronym that stands for “Beginner’s AllPurpose Symbolic Instruction Code” Basic Character Set A character set is simply a list of letters, numbers and symbols that provide one with the characters used in a particular language. BASIC as a language has its own character set, they include the following: · Alphabets characters (A to Z or a to z) · Numeric character (0, 1 to 9) including hexadecimal characters. · Special characters (such as $,#,!, ^ etc.) that perform special functions in BASIC Arithmetic operators CHARACTER NAME USE * Asterisk For multiplication, e.g. A*B or (3*5) - Minus For subtraction, e.g. M-N or (4-1) + Plus For addition, e.g. K+N or (1+6) / Forward slash For real division, e.g. A/B or (7/3)=2.33 \ Bask slash For integer division, e.g. P\G or (7\3)=2 ^ Caret For exponentiation, e.g. A^B or (7^3) Relational (Comparison) Operator CHARACTER NAME USE = Equal to A=B > Greater than A>B < Less than A<B > = Greater than or Equal to A >=B < = Less than or Equal to A <= B Data types Data type is a description of the set of values and the basic set of operation that can be applied to values of the type. i) Integers: a positive or negative number without decimal. It has a range of values from - 32,768 to 32,767. Each value is stored using 2 bytes of memory (storage) space. ii) Real numbers: numbers with fractional parts or with a decimal point. It is stored using 4 bytes of memory space. iii) Boolean: consist of only two values; “YES and NO” or “True or False” or 1 or 0. iv) String: a sequence of characters in double quote. For example, “Computer Studies” is a string value with 16 characters. Each character is stored using 8 bits (one byte) in the ASCII character set and two byte in the UNICODE character set. Alphabet is represented in ASCII. Keyword Keywords are words that have special meaning and function in BASIC. Such words must be used strictly according to their functions, otherwise the computer will respond with error message. Here are some of the BASIC keywords and their uses KEYWORDS USE EXAMPLE REM Make comment about an instruction or about 10 REM Program to the whole program add two numbers INPUT Used to ask the user to supply the data to be processed while the program is executing 5 INPUT A,B,C PRINT Used to display the output of operation on the screen PRINT “The values”, A,B LET Used to assign a value to a variable 3 LET A=5 READ Used to tell the computer that the data to be processed is supplied within the program statements. Used together with DATA keyword 10 READ A,B DATA Used to show the computer the data it is asked to read in the READ statement. Used along with READ keyword. 10 DATA 4,7 END To end the program 5 END Variables and Constant A variable is an identifier or a name of a memory location where data (values) can be placed or stored. Because the value placed in a memory location can be changed at any time, we call such memory location “a variable”. However, when the value of a memory location is not to be changed, we refer to such memory location as “constant”. Declaration of Variables and Constants When you are to supply data to the computer for processing, you are required to state the data type. This help the computer to interpret it and an appropriate storage space is reserved for the data. To achieve this, the keyword DIMENSION (or DIM) is used to specify the data type. For example, variable Name and Pie are to hold “character and real” data values respectively. This can be written in BASIC as follows: 1. DIM Char Name, INT Age, REAL Height 2. DIM Name AS Char, Age AS INT, Height AS Real 3. DIM Name$, Age%, Height! Rules for naming Variable 1. Every variable must begin with an English alphabet (A to Z or a to z). 2. The name must not be more than 40 characters in length. 3. Names can be alphanumeric (combinations of alphabet and numbers 4. Name must not be any keyword 5. Do not include a blank space in the name. Basic Expression A BASIC expression is formed when two or more characters, strings, variables or constants are combined with arithmetic, relational or logical operators Examples include: 1. F2 + 7 (add 7 to the value in the variable F2) 2. A+ B (add the value in A to the value in B) Arithmetic expression and their BASIC expression Algebraic expression BASIC expression A ÷ B A/B 2b2+5c 2*(b^2)+5*c -b±√b2-4ac/(2a) (-b+sqr(b^2 – 4*a*c))/2*a Arithmetic operation precedence The rule that guides the order in which operation must be performed in an expression is known as “precedence” rule. THE ORDER SYMBOL NAME 1 ( ) Bracket 2 ^ Exponentiation 3 - Negation 4 *or / Multiplication 5 \ Integer 6 MOD Modulo (remainder) 7 + or - Addition or subtraction BASIC statements A BASIC statement is any valid instruction given to the computer for processing of data. BASIC statement may be an expression, an assignment, a reserved word (keyword) with or without arguments or their logical combinations 1. Declaration statement: Every variable in a program must be declared before it can be used. A variable must be declared with appropriate data type. Example of declaration statements are a. DIM Age AS Integer b. DIM Name AS Character 2. Assignment statement: This statement causes a computer to store a value in a variable. The statement has two sides separated by the equality sign (=). a. Name = “Computer Studies” b, Age = 16 3. INPUT statement: This statement is used to ask the user to supply the data to be processed while the program is executing. a. INPUT Name$, Age% (This statement is not interactive) b. INPUT “Enter the Name and Age”, Name, Age (This statement is interactive) 4. READ and DATA statement: The READ statement is used to supply data into the program, but unlike the INPUT statement, the programmer supplies the data inside the program codes using the DATA keyword. a. READ “Enter Name and Age”, Name$, Age% b. DATA “Jide Babs”, 20 5. PRINT statement: This statement is used to show the result of data processed. A simple QBASIC program 1 REM This program calculates the area of any rectangle and print the length, width, and area. 2 ‘To calculate the Area of a Rectangle we must know its length and width 3 Length = 10 4 Width = 6 5 Area = length * breadth 6 PRINT “Area is “, Area 7 END Another program solving Quadratic equation is shown below. 1. REM program to solve a quadratic equation 2. INPUT “Enter coefficients a, b, c “; a,b,c 3. Discrim = b^2 – 4*a*c 4. If Discrim > 0 then 5. Dummy = sqr(Discrim) 6. PRINT “There are 2 distinct roots” 7. PRINT “first root (X1) = “; (-b + dummy)/(2*a) 8. PRINT “Second root (X2) = ”; (-b –dummy)/ (2*a) 9. ELSEIF Discrim = 0 THEN 10. PRINT “There is only 1 root, the 2 roots coincide” 11. PRINT “double root = ”; - b / (2*a) 12. ELSE 13. PRINT “The roots are imaginary” 14. Dummy = sqr (- discrim) 15. PRINT “Real part = “; - b /(2*a) 16. PRINT “Imaginary part = ”; dummy/(2*a) 17. END IF 18. END