Intro to Programming EC201 Fundamental Programming

10
Introduction to Programming Lecture by: Lecture by: Pn. Wan Fazlini Idayu binti Wan Fakari Politeknik Kuala Terengganu Politeknik Kuala Terengganu 1 Discuss on: Discuss on:Program Programming Programming Programmer 2 What is program? What is program? A set of stepbystep instructions that tells a computer to perform a specific task and to computer to perform a specific task and to produce the required results. written by the programmer Produced through programming Produced through programming 3 What is programming? What is programming? Programming is a process of designing/ i creating a program. It is a communication technique to explain the instructions to the computer. Used to prod e the pro ram Used to produce the program. 4

description

EC201 Fundamental ProgrammingChapter 1: Intro to Programming

Transcript of Intro to Programming EC201 Fundamental Programming

Page 1: Intro to Programming EC201 Fundamental Programming

Introduction to Programming

Lecture by:Lecture by:Pn. Wan Fazlini Idayu binti Wan Fakari

Politeknik Kuala TerengganuPoliteknik Kuala Terengganu

1

Discuss on:Discuss on:‐

• Program• ProgrammingProgramming• Programmer

2

What is program?What is program?

• A set of step‐by‐step instructions that tells a computer to perform a specific task and tocomputer to perform a specific task and to produce the required results.

• written by the programmer• Produced through programming• Produced through programming

3

What is programming?What is programming?

• Programming is a process of designing/ icreating a program.

• It is a communication technique to explain the q pinstructions to the computer.

• Used to prod e the pro ram• Used to produce the program.

4

Page 2: Intro to Programming EC201 Fundamental Programming

What is Programmer?What is Programmer?

• A Programmer is a person who designs, writes and test computer programs.

5

The process of writing, testing and maintainingthe source code of the computer program

What ?

Computer Programming

How to program ?

•Requires knowledge in the application domain•Follow the steps in software development method

6

•Follow the steps in software development method

Programming is a problem-solving activityactivity

To solve problems d i lif

To ease daily process occurred in life

with the assistance of computer

e.g.: transaction, payroll, accounting,

registration, information

7

p registration, information exchange etc.

A set of symbol, word, code or instructions which is understood by What ?ycomputer

?Programming Language?g g g g

Method of communication for which ld d d d Function?computers could understand and execute

the instructions written in source code.

Function?

8

Page 3: Intro to Programming EC201 Fundamental Programming

A programming

H ll ! Wh t I

A programming language is therefore a Hello! What can I 

do to you?therefore a practical way for us (humans)for us (humans) to give instructions toinstructions to a computer.

Tell me  1 + 1 is equal to  what???

9

Type of Programming LanguageType of Programming Language

“ lk”• programmers “talk” to computer using a computer p g planguage. 

• Generation of computer planguages  :‐ Low Level LanguageLow Level Language‐ High Level Languages

10

Type of Programming LanguageType of Programming Language

11

Category ExplanationMachine Binary number codes understood by Machine language

Binary number codes understood by a specific CPU

A bl i d h d Assembly language

Mnemonic codes that correspond to machine language instructions

High level language

Machine-independent programming language that combines algebraic expressions and English symbols.

Comp ter programmers s all onl

12

Computer programmers usually only involve in writing high level language

Page 4: Intro to Programming EC201 Fundamental Programming

Low Level Language:g g• 1st : Machine language• 2nd : Assembly language : The l d dlanguage structure depends on instruction set/ CPU’sinstruction set/ CPU s architecture.

* low level language enables efficient machine low level language enables efficient machine usage but it is difficult to use.

13

High Level Language:3rd P d i t d• 3rd : Procedure oriented: COBOL, C, FORTRAN, Pascal

• 4th : Problem oriented/query : DATA TRIEVE INTELECTDATA‐TRIEVE, INTELECT

14

Machine languageMachine language

• The lowest‐level programming language• only languages understood by computers.• easily understood by computers, but almost impossible for humans to use.

• Expressed in binary form. • Very difficult to use. Lowest level of computer languages.

• Example:0101 1000 0001 0000 1100 0001 0000 11110101 1010 1101 0001 1010 0000 0101 0100

15

Assembly language (1)Assembly language (1)

• Second level of language. An assembler translates assembly language.assembly language.

• It was developed to replace "0" and "1" used in Machine LanguageMachine Language.

• Machine languages consist entirely of numbers and l t i ibl f h t d d itare almost impossible for humans to read and write. 

Takes a long time to write programs.

16

Page 5: Intro to Programming EC201 Fundamental Programming

Assembly language (2)Assembly language (2)

• English‐like abbreviations representing elementary computer operations (translated via assemblers).computer operations (translated via assemblers).

• Example (to add 3 hex numbers): ORG0H ;start (origin) at location 0MOV R5#25H ;load 25H into R5MOV R7#34H ;load 34H into R7;MOV A,#0 ;erase an exiting value in accumulator (A)ADD A,R5 ;add contents of R5 to AADD A,R7 ;add contents of R7 to A, ;ADD A,#12H ;add value 12H to A

HERE: SJMP HERE ;stay in this loopEND ;end of asm source codeEND ;end of asm source code

17

High level languageHigh‐level language

• Instruction is written as a series of English‐like words.

• Translator (Compiler/Interpreter) is needed to translate high level language to machinetranslate high level language to machine language.

• It is Machine‐Independent. The program can be written and executed on any computer.be written and executed on any computer.

18

Computer only p yunderstands machine language.

S t dSo, computer needtranslator call:-Assembler-Assembler-Compiler or interpreter.

Assembler:assembly machiney

compiler or interpreterhi h l l hihigh level machine

19

Assemblyycode

A blAssembler

Obj t dObject code (machine language)20

Page 6: Intro to Programming EC201 Fundamental Programming

What is C?What is C?

• A high level structured & procedural programming language.

• C is an intermediate language between human &• C is an intermediate language between human & computer.

C i h l t bl l & i d tHuman language Machine language

• C is much closer to assembly language & is used to write UNIX based OS. 21

C HistoryC History• Derived from the BCPL language by• Derived from the BCPL language by Martin Richards (1967).

• Ken Thompson developed a B language from BCPL language(1970)BCPL language(1970).

• Evolved into the C language by Dennis Ritchie (1970) at Bell Telephone Laboratories Inc. (now the AT & T Bell Laboratories).( )

• C language was first used on a computer Digital Equipment Corporation PDP 11 to fullyDigital Equipment Corporation PDP‐11 to fully use in UNIX operating system. 22

How Computers Run the ProgramHow Computers Run the Program

• computer program run through the following steps:Edit S d ( h )Edit : Source code (type the program)

Compile    : If no syntax errors  Object Code

Link : Link to library function  exe mode

Run : Output (check for any semantic errors)p ( y )

23 24

Page 7: Intro to Programming EC201 Fundamental Programming

Examples of C programming:Examples of C programming:‐

#include <stdio.h>

main()main(){

printf("hello, world\n");return 0;return 0;

}

25

Ex 2 (pengiraan markah)Ex.2 (pengiraan markah)#include <stdio.h>

// Tentukan gred dan sama ada gagal atau lulusif (jumlah >= 80)int main(void)

{float u1, u2, pro; // markah dimasukkan oleh pemakaifloat berat_u1, berat_u2, berat_pro, jumlah; // berat markah

if (jumlah >= 80){

printf("Gred : A\n");printf("Lulus!!!\n");

}else if (jumlah>= 70)

// Input markah dari pemakaiprintf("Masukkan markah Ujian1 : ");scanf("%f", &u1);printf("Masukkan markah Ujian2 : ");

{printf("Gred : B\n");printf("Lulus!!!\n");

}else if (jumlah >= 60){

scanf("%f", &u2);printf("Masukkan markah Projek : ");scanf("%f", &pro);

// kira berat markah untuk ujian dan projek

{printf("Gred : C\n");printf("Lulus!!!\n");

}else if (jumlah >= 50){

i tf("G d D\ ")// kira berat markah untuk ujian dan projekberat_u1 = (u1 / 100) * 30;berat_u2 = (u2 / 100) * 30;berat_pro = (pro /100) * 40;

printf("Gred : D\n");printf("Lulus : Mengulang\n");

}else if (jumlah >= 40){

printf("Gred : E\n");// kira jumlah markahjumlah = berat_u1 +  berat_u2 + berat_pro;

// Cetak ujian1, ujian2, projek, dan jumlah markahi tf("\ T t 1 (30%) %5 2f\ " b t 1)

p ( )printf("Gagal : Mengulang\n");

}else{

printf("Gred : F\n");printf("Gagal : Mengulang\n");printf("\nTest 1 (30%) : %5.2f\n", berat_u1);

printf("Test 2 (30%) : %5.2f\n", berat_u2);printf("Project(40%) : %5.2f\n", berat_pro);printf("Overall(100%): %5.2f\n\n", jumlah);

printf( Gagal : Mengulang\n );}return 0;

} 26

Types of programmingTypes of programming

• Structured programmingp g g• Modular programming• Object‐oriented programming

27

Structured programming (1)Structured programming (1)

Definition of Structured Programming:Structured programming can be defined as a Software application programming technique that follows a top down design approach with block oriented structures.

takes on the top to bottom approachIt splits the task into modular forms which makes theIt splits the task into modular forms which makes the program simpler with less codes & easier to read.It support loop such as while do while and forIt support loop such as while, do‐while and for.

28

Page 8: Intro to Programming EC201 Fundamental Programming

Structured programming (2)Structured programming (2)

Codes similar to everyday EnglishU h i l iUse mathematical notations.The most popular structured programming languages include C C++ Ada and PascalC,C++, Ada, and Pascal.Example:

if (x < 10)if (x < 10) {

printf(“too low, try again\n”);scanf(“%d”,&x);

}

29

Structured programming (3)Structured programming (3)

Ask  user for text input

Receive user text input

Process user inputProcess user input

Return result to user 30

Modular programming (1)Modular programming (1)

Programming paradigm based on data types.An object stores a data type value; variable name refers toAn object stores a data type value; variable name refers to object.A programming style that breaks down program functions intoA programming style that breaks down program functions into modules, each of which accomplishes one function and contains all the source code and variables needed to accomplish that function.Modular programming is a solution to the problem of very large programs that are difficult to debug and maintain. By segmenting the program into modules that perform clearly defined functions you can determine the source of programdefined functions, you can determine the source of program more easily.

31

Modular programming (2)Modular programming (2)

Main program data

module1 module2module1data+data1

module2data+data2

procedure1procedure2 procedure3

32

Page 9: Intro to Programming EC201 Fundamental Programming

Object‐oriented programming ‐OOP (1)

Based on objects and visual.4th generation languageThe concepts in the OOP:– ObjectObject– class

OOP splits the program into objects that can beOOP splits the program into objects that can be reuse into other program.E h f ti i d l d t i t t bj t tEach function is developed to instruct an object to perform actions based on the even that take place.

33

Object‐oriented programming –OOP (2)

An object‐oriented program is decomposed into a network of collaborating objects. An object represents a thing or concept and has a known set of behaviors that may be invoked by other objects. For any activity of the program, an object responsible For any activity of the program, an object responsiblefor that activity may interact with other objects by invoking their behaviors, or "methods", until the g , ,activity is complete.

34

Object‐oriented programming –OOP (3)

The most popular object‐oriented programming languages include Java Visual Basic C# C++ andlanguages include Java, Visual Basic, C#, C++, and Python.

35

Object‐oriented programming ( )(OOP)

Load program

Text 

Draw window button

input

window

P d tProgram ready to use 36

Page 10: Intro to Programming EC201 Fundamental Programming

Compare between the following Compare between the following types of programming:

Structured programmingModular programmingModular programmingObject-oriented programming

* Discuss in group and list the answer.* We will discuss in next class.

37

d l bStructureProgramming

Modular Programming

ObjectOriented 

Programming

Definition ‐ ‐ ‐

d lLanguage use Ada, Pascal, C….

Step used ‐top to bottom approach‐

Advantage/disadvantage

L l d t Hi h l lLevel moderate High level

38

39