Unit 4 ( Programming Languae and Logic)
Unit 4 ( Programming Languae and Logic)
What is Program.
Program
is a set of instruction that tell computer what to do and how
to do a specific task.
enables a single computer to perform many different types
of task.
Programming Languages
A set of words and symbols used to write a program
is a computer language that is used by programmers
(developers) to communicate with computers.
High Level Languages
Low Level Languages
High Level Language
are written in a form that is close to our human(natural) language, enabling to
programmer to just focus on the problem being solved.
require no knowledge of the hardware and instruction set of the computer that will
use the program.
are portable and can be used on different types of computer
Examples include: C++, Java, Pascal, Python, Visual Basic.
Advantages
❑ Closer to English Language/ human Language so that
easier to read/ write/ understand
❑ Easier to debug
❑ Less likely to make errors
❑ One lines of code can carry out multiple command
❑ Portable language
Low level Language
are used to write programs that relate to the specific architecture and hardware of
a particular type of computer.
are closer to the native language of a computer (binary) making them harder for
programmers to understand.
Examples of low level language:
Assembly Language
Machine Code
Assembly Language
Few programmers write programs in an assembly language.
is easy distinguishable from a high level language as it
contains few recognizable human words but plenty mnemonic
code 4 First
Acc
LDA First
ADD Second 4 9 5 Second
STO Sum
9 Sum
LDA means load the value of the variable into the accumulator
ADD means add the value of another variable to the value stored in the accumulator
STO means replace the value of the variable by the value stored in the accumulator.
Assembly Language
Advantages
Can make use of special hardware or special machine-dependent instructions (e.g.
on the specific chip)
Translated program requires less memory
Write code that can be executed faster
Total control over the code
Can work directly on memory locations
Limitation
A very limited range of instructions is available
Debugging is very difficult
Machine Code
Programmers rarely write in machine code (binary) as it is difficult to understand
and it can be complicated to manage data manipulation and storage.
Advantages Disadvantages
High Level Independent of the type of computer Programs can be larger
Language being used (portable) Programs can take longer to
Easier to read, write and understand execute
programs Programs may not be able to use of
Quicker to write program special hardware
Programs are easier and quicker to
debug
Easier to maintain program in use
Low Level Can make use of special hardware It takes a longer time to write and
Language Includes special machine-independent debug programs
instructions Programs are more difficult to
Can write code that doesn’t take up understand
much space in primary memory
Can write code that performs a task
very quickly
Translators
Why translator is needed?
The computer can only understand machine code low-level language
But programmer used to write program in high level language.
Thus, program (source code) need to be translated into machine code using
translator.
What is translator?
Translator is a software that convert the source code into machine code (object
code).
There are three type of translator
Compiler
Interpreter
Assembler
Compilers
is a program that translates high level language program (source code) into machine code, often
object code and saved as executable file to be executed later
Translates (the source code) all in one go/all at once and produce an executable file of machine
code.
Once compiled ,machine code can then be directly used by the computer.
Compiled programs are used without the compiler.
Error report produced once entire program is compiled. (error may cause program to crash)
One high-level language statement may be several lines of machine code when compiled.
Error report
result 3
Source code Intepreter
Error report
Sum = 1+2
00100000
run
Sum = 1# 2 10000100
11110000
Advantages and Disadvantages of Interpreter
Advantages Disadvantages
Easier and quicker to debug and test Interpreted programs cannot be used
programs during development. without the interpreter.
Errors can be quickly spotted - once an Interpreted programs run slowly as the
error is found, the program stops running processor has to wait for each instruction
and the programmer is notified at which to be translated before it can be executed.
part of the program has failed. Interpreters do not produce an executable
Interpreter is easier to use, especially for file that can be distributed. As a result, the
beginner. source code program has to be supplied,
and this could be modified without
permission.
Interpreters do not optimize code - the
translated code is executed as it is.
Assembler
are used to translate a program written in assembly language into a machine code
(object code) file.
An executable file of machine code is produced.
Assembled programs can be used without the assembler.
One low-level language statement is usually translated into one machine code
instruction.
An assembled programs are used without assembler.
An assembled program is usually distributed for general use.
(b) Write down the other three of the functions of a typical operating system
1 …………………………………………………………………………
2 …………………………………………………………………………
3 …………………………………………………………………… [3]
2.The programmer uses the high-level language or low-level language to write a program.
(a) State three benefits of writing a program in a high-level programming languages.
[3]
1 ………………………………………………………………………
2 …………………………………………………………………………
3 ……………………………………………………………………………
(b) Explain why a programmer would not choose to write a program in a low-level programming language.
[2]
……………………………………………………………………………
……………………………………………………………………………
The compilers and interpreters are types of translators.
Tick (√) to show which statements apply to each translator. [5]
A B C A AND B P
0 0 0 0 0
0 0 1 0 1
0 1 0 0 0
0 1 1 0 1
1 0 0 0 0
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
Logic
Algorithm and programs are based on logic.
There are some logic statement such as (IF…THEN) and operators such as AND,
OR and NOT.
The truth table are used to evaluate the expected output of logic from different
inputs.
0 0 0 0 0
0 0 1 0 1
True = 1 0 1 0 0 0
False = 0
0 1 1 0 1
1 0 0 0 0
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
Logic
Input Output
0 0 0
0 1 1
1 0 1
1 1 1
Logic
IF ( NOT g_mode AND h <=0) OR s > 1000 THEN
SET game_over to true
game _over = 1 if ( NOT g_mode AND h <=0) OR s > 1000
game _over = ( NOT g_mode AND h <=0) OR s > 1000