300 likes | 443 Views
Languages and Environments. Computer languages. Computer languages are classified as either low-level or high-level. Put simply, low-level are understood by the computer, and high-level by humans!. Languages and Environments. Machine code.
E N D
Languages and Environments Intermediate 2 Computing
Computer languages Computer languages are classified as either low-level or high-level. Put simply, low-level are understood by the computer, and high-level by humans! Languages and Environments Intermediate 2 Computing
Machine code A computer is an electronic device so it only understands binary (digital) information. Therefore computer programs (software) have to be in binary form to be run (executed). A low-level language called machine code is the name given to program instructions in binary form e.g. 11001100 00010001 etc Languages and Environments Machine code is difficult for humans to understand, is processor-specific and errors are difficult to find. Intermediate 2 Computing
Assembler Assembler is a low-level language that is easier for programmers to write machine code programs – but is still very difficult to learn! Languages and Environments Assembler instructions are mnemonics e.g. JMP, MOV, LDA etc. Intermediate 2 Computing
High-level languages High-level languages (HLL) were developed to make programming easier for humans. HLLs combine English, maths and program structures to make writing software easier. Languages and Environments HLLs combine English, maths and program structures to make writing software easier. Intermediate 2 Computing
Translators Programs written in high-level languages need to be translated into low-level (machine code) for processing and executing by the CPU. This is done by a translator program. • There are two types of translator program: • interpreters • compilers Languages and Environments Intermediate 2 Computing
Interpreters Interpreter programs translate HLL code into machine code one line at a time. Advantage: easy to find errors, better for learners Disadvantage: programs slow as have to be continually interpreted, interpreter program always in memory to interpret program Languages and Environments Intermediate 2 Computing
Compilers A Compiler program translates the whole program into a machine code version that can be run without the compiler being present. Advantage: programs fast as already in machine code, translator program only needed at the time of compiling Disadvantage: slow to compile as whole program translated Languages and Environments Intermediate 2 Computing
Text editors AHLL program’s source code is typed into a text editor such as MS-Notepad, orin a programming language’s own text editor. Here is Visual Basic’s text editor window. Languages and Environments A text editor has word-processing features such as ‘cut and paste’ that help the programmer develop the program quickly. The source code is then saved as a text file (from which it can be interpreted or compiled.) Intermediate 2 Computing
Macros Amacrois a small, time-saving program written in a scripting language within an application program such as MS-Excel. Here is a macro recorded in Excel using Visual Basic for Applications (VBA) Languages and Environments A macro is recorded (programmed) to automate repetitive tasks and to extend functionality i.e. to do something the application cannot already do. A macro can be assigned to a keystroke to run it e.g. Ctrl + K Intermediate 2 Computing
HLL Programming Intermediate 2 Computing
Input and Output Input FName = txtFName.Text FName = InputBox (“Enter first name:”) HLL Programming Output lblAnswer.Caption = “The answer is “ & answer & “.” MsgBox “The answer is “ & answer & “.” Intermediate 2 Computing
A variable is a name given to an item of data in a program. e.g. Dim length as Integer An array is a name given to a list of items of data in a program e.g. Dim scores(5) As Integer Assignment An assignment statement assigns the value or control on the right-hand side of an equals symbol to a variable or control on the left. Examples: Age = 26 VAT = 17.5 SName = “Schwarzennegger” average = (num1 + num2 +num3) / 3 mark = txtMark.Text HLL Programming Intermediate 2 Computing
Arithmetic operations Arithmetic operations include + - * / ‘arithmetic operation example ‘ totalscore = (score 1 + score 2) * 2 HLL Programming Intermediate 2 Computing
Logical functions Logical functions include using AND, OR, NOT etc between conditions (complex conditions) ‘logical function example ‘ If Age >= 10 And <=25 Thenetc. HLL Programming Intermediate 2 Computing
Simple conditions A simple conditional statement uses one condition to make a decision. If condition Then action End If ‘Example 1 – SIMPLE CONDITIONAL ‘ If Mark >= 50 Then MsgBox = “Pass” End If HLL Programming ‘Example 2– SIMPLE CONDITIONAL ‘ If Mark >= 50 Then MsgBox = “Pass” Else MsgBox = “Fail” End If If condition Then decision 1 Else decision 2 End If Intermediate 2 Computing
Complex conditions A complex conditional statement uses more thanone condition. If condition 1 AND / OR / NOT condition 2Then decision 1 Else decision 2 End If ‘Example 1 – COMPLEX CONDITIONAL ‘ If Age >= 10 And Age <=25 Then MsgBox = “You may join.” Else MsgBox “You may not join.” EndIf HLL Programming ‘Example 2 – COMPLEX CONDITIONAL ‘ If ans = “Y” Or ans = “y” Then End‘close program Else etc. EndIf Intermediate 2 Computing
Fixed loops A fixed loop performs a set (fixed) number of times. For…To code to be repeated Next ‘Example – FIXED LOOP ‘ Dim counter As Integer For counter = 1 To 10 Then frmDisplay.Print “Hello there!” Next counter HLL Programming Intermediate 2 Computing
Conditional loops A conditional loop repeats until a condition is met. Do code to be repeated Loop Untilcondition ‘Example – CONDITIONAL LOOP ‘ Dim counter As Integer DO num = InputBox(“Enter number:”) Loop Until num = 999 HLL Programming Intermediate 2 Computing
Nested loops A nested loop is simply a loop within a loop. ‘Example – NESTED LOOP ‘ For count = 1 To 6 Then Do num = InputBox(“Enter number:”) Loop Until num = 999 Next count HLL Programming This example demonstrates the use of a fixed loop with a conditional loop nested within. Intermediate 2 Computing
Numeric and string variables A variable is a name given to an item of data in a program that is stored in memory. Integers and singles are used to store numbers e.g. Dim length as Integer ‘whole number Dim price as Single ‘fractional number A string variable is used for text e.g. Dim pname as String ‘text HLL Programming Intermediate 2 Computing
1-D arrays An array is a name given to a list of items of data in a program. The list is stored in contiguous memory locations. Here is an array nums(10) of integers between 1 and 9: HLL Programming Here is some VB code to setup and array: ‘Example: 1D array Dim nums(10) As Integer Here is some VB code that would input data to an array: ‘Example: Input to array For c = 1 To 10 nums(c) = inputbox(“Enter number etc. List1.additem nums(c) Next c Intermediate 2 Computing
Predefined Numeric Functions Predefined functions are in-built commands that perform a specific task within a program. They save programmers lots of time coding these tasks. Numeric functions Int removes fractional part of a whole number e.g. r = Int(num) Round returns the nearest whole number e.g. r = Int(num) Sqr returns the square root of any number e.g. r = Sqr(num) Sin returns sine angle (in radians) e.g. HLL Programming Intermediate 2 Computing
Predefined functions String functions Ucase – converts to upper case Lcase – converts to lower case Len – returns number of characters Mid$ - gets text from anywhere in a string Asc – returns ASCII code Chr$ - return ASCII character HLL Programming Intermediate 2 Computing
Mid$ Mid$ gets a substring (a piece of a larger string) from anywhere within a string. Usually used to ‘grab’ text from the middle of a string. Format: Mid$(string, start, length) Suppose we have a string |”Visual Basic Code” and we want to grab the word “Basic” from the string. HLL Programming MyString = “Visual Basic Code” NewString = Mid$(MyString, 7, 5) This example will start at character 7 and grab the next 5 characters I.e. the word “Basic”. Intermediate 2 Computing
Input validation algorithm Input validation is a standard algorithm that allows only acceptable data to be entered into a program. ‘Example – INPUT VALIDATION ALGORITHM ‘ Do mark = InputBox(“Enter mark:”) If mark < 0 Or mark > 100 Then MsgBox “Invalid. Please re-enter:” End If Loop Until mark >= 0 And mark <= 100 Standard Algorithms and Arrays This example rejects integers that are not within the acceptable range of 0 – 100. Intermediate 2 Computing
Find Minimum algorithm This algorithm finds the lowest number in a list of numbers. Standard Algorithms and Arrays In the list above it would return the value 2. Intermediate 2 Computing
Find Maximum algorithm This algorithm finds the highest number in a list of numbers. Standard Algorithms and Arrays In the list above it would return the value 9. Intermediate 2 Computing
Linear Search algorithm This algorithm finds a number if it is in a list of numbers. Standard Algorithms and Arrays If the target value is 7… …it would return the message “7 was found in the list at position 10”. Intermediate 2 Computing
Count Occurrences algorithm This algorithm counts the number of times (frequency) a number appears in a list of numbers. Standard Algorithms and Arrays If the target value is 3… …it would return the message “3 appears in the list 2 times”. Intermediate 2 Computing