Chapter1_IntrotoC

download Chapter1_IntrotoC

of 29

Transcript of Chapter1_IntrotoC

  • 8/11/2019 Chapter1_IntrotoC

    1/29

    CSEB114: PRINCIPLEOF PROGRAMMINGChapter 1: Introduction to Computer andProgramming

  • 8/11/2019 Chapter1_IntrotoC

    2/29

    Objectives

    In this chapter you will learn about:

    Overview of PC components

    The different types of language

    Natural Language

    Formal Language

    Functional / Imperative Language

    Programming Languages

    C as an imperative language

    C program at a glance

  • 8/11/2019 Chapter1_IntrotoC

    3/29

    Overview of PC Components

    Section 1

  • 8/11/2019 Chapter1_IntrotoC

    4/29

    Computer Hardware

    Components

    Components of a PC

  • 8/11/2019 Chapter1_IntrotoC

    5/29

    Input / Output Devices

    Input DevicesAccepts information from the user and

    transforms it to dig i tal codes that the

    computer can processExample: keyboard, mouse, scanner

    Output Devices

    An interface by which the computerconveys the output to the user

    Example: monitor, printer

  • 8/11/2019 Chapter1_IntrotoC

    6/29

  • 8/11/2019 Chapter1_IntrotoC

    7/29

    Central Processing Unit (CPU)

    Does most of the work in executing a program

    The CPU inside a PC is usually the

    microprocessor

    3 main parts:Control Unit Fetch instructions from main memory and put them in

    the instruction register

    ALU (Arithmetic Logic Unit) Execute arithmetic operations

    Registers Temporarily store instructions or data fetched from

    memory

  • 8/11/2019 Chapter1_IntrotoC

    8/29

    Storage Devices

    A magnetic device used to store a large

    amount of information.

    Store the software components or data

    needed for the computer to execute its

    tasks.

    Could be read only or writable.

    Example: Hard drive, CD ROM, floppy

    disks

  • 8/11/2019 Chapter1_IntrotoC

    9/29

    Network Devices

    Connect a computer to the other

    computers.

    Enable the users to access data or

    execute programs remotely.

    Example: modem, Ethernet card

  • 8/11/2019 Chapter1_IntrotoC

    10/29

    The different types of language

    Section 2

  • 8/11/2019 Chapter1_IntrotoC

    11/29

  • 8/11/2019 Chapter1_IntrotoC

    12/29

    Semantics and Syntax

    Semanticsthe meaning of the language

    within a given context

    Syntax - Syntax are the rules to join words

    together in forming a correct expression

    or phrase.

    In natural languages it is often possible to

    assemble a sentence in more than one

    correct ways.

  • 8/11/2019 Chapter1_IntrotoC

    13/29

    Formal Language

    Language with limited, defined, words

    Each concatenation of words ('phrase') has a

    single, clearly defined meaning

    no (miss-)interpretation possible

    Sometimes called Context Free Language

    To 'talk' to a computer; to instruct a computer;

    our commands must be 100% clear andcorrect.

    Often there is only a single, correct syntax.

  • 8/11/2019 Chapter1_IntrotoC

    14/29

    Functional / Imperative

    Language

    Functional Language:

    Tell what to do, but not how:

    sum [1...10]

    Imperative Language:

    Tell what to do, but mainly how:

    Take number 1 and add the next number to it;

    then add the next number to the sum; and so on;until you have reached 10 as number to be

    added. Then print the sum of all numbers

  • 8/11/2019 Chapter1_IntrotoC

    15/29

    What is Programming?

    Programming is instructing a computer to dosomething for you with the help of aprogramming language

    The two roles of a programming language:Technical: It instructs the computer to

    perform tasks.

    Conceptual: It is a framework within which we

    organize our ideas about things andprocesses.

    In programming, we deal with two kind of things:

    Data- representing 'objects' we want to

    manipulate

  • 8/11/2019 Chapter1_IntrotoC

    16/29

    Programming Language

    Formal Language used to communicate

    to a computer.

    A programming language contains

    ins t ruct ions for the computer to perform

    a specific action or a specific task:

    'Calculate the sum of the numbers from 1 to

    10

    'Print I like programming

    'Output the current time'

  • 8/11/2019 Chapter1_IntrotoC

    17/29

    Programming Language

    Can be classified into as a special-purpose and

    general-purpose programming languages.

    Special-purpose : is design for a particular type

    of application Structured Query Language (SQL)

    General-purpose : can be used to obtain

    solutions for many types of problems

    Machine Languages

    Assembly Languages

    High-Level Languages

  • 8/11/2019 Chapter1_IntrotoC

    18/29

    Machine Language

    The only language that the processor actually

    'understands

    Consists of binary codes: 0 and 1 Example: 00010101

    11010001

    01001100

    Each of the lines above corresponds to a specific task to

    be done by the processor.

    Programming in machine code is difficult and slow sinceit is difficult to memorize all the instructions.

    Mistakes can happen very easily.

    Processor and Architecture dependent

  • 8/11/2019 Chapter1_IntrotoC

    19/29

    Assembly Language

    Enables machine code to be represented in words and

    numbers.

    Example of a program in assemb ler language:

    LOAD A, 9999

    LOAD B, 8282

    SUB B

    MOV C, A

    LOAD C, #0002

    DIV A, C

    STORE A, 7002

    Easier to understand and memorize (called

    Mnemonics), compared to machine code but still quite

    difficult to use.

    Processor and Architecture dependent

  • 8/11/2019 Chapter1_IntrotoC

    20/29

    High-Level Language

    Use more English words. They try to resemble English

    sentences. Therefore, it is easier to program in these

    languages.

    The programming structure is problem oriented - does

    not need to know how the computer actually executes

    the instructions.

    Processor independent - the same code can be run on

    different processors.

    Examples: Basic , For tran , Pascal, Cobo l, C, C++,

    Java

    A high level language needs to be analyzed by the

    compiler and then compiled into machine code so that

    it can be executed by the processor.

  • 8/11/2019 Chapter1_IntrotoC

    21/29

    C Programming Language

    Why 'C' ?

    Because based on 'B'; developed at Bell

    Laboratories

    Developed by Dennis Ritchie at BellLaboratories in the 1960s

    In cooperation with Ken Thomson it was used

    for Unix systems The C Language was only vaguely defined, not

    standardized, so that almost everyone had his

    own perception of it, to such an extend that an

    urgent need for a standard code was creeping

  • 8/11/2019 Chapter1_IntrotoC

    22/29

    C Programming Language

    In 1983, the American National Standards

    Institute (ANSI) set up X3J11, a Technical

    Committee to draft a proposal for the ANSI

    standard, which was approved in 1989and referred to as the ANSI/ISO 9899 :

    1990 or simply the ANSI C, which is now

    the global standard for C. This standard was updated in 1999; but

    there is no compiler yet

  • 8/11/2019 Chapter1_IntrotoC

    23/29

    C as An Imperative Language

    Section 3

  • 8/11/2019 Chapter1_IntrotoC

    24/29

    CAn Imperative Language

    C is a highly imperative language

    We must tell it exactly how to do what;

    the means and functions to use;

    which l ibrar ies to use;

    when to add a new line;

    when an instruction is finished;

    in short: everything and anything

    Hint: Observe the syntax in the next slide

  • 8/11/2019 Chapter1_IntrotoC

    25/29

    C Program at a Glance

    Section 4

  • 8/11/2019 Chapter1_IntrotoC

    26/29

    A Simple Program in C

    #include

    int main()

    {

    printf("I like programming in C.\n");

    return 0;

    }

  • 8/11/2019 Chapter1_IntrotoC

    27/29

    A Simple Program in C -

    explanation

    #include

    int main()

    {

    printf("I like programming in C.\n");

    return 0;

    }

    standard Library, input-output, header-file

    Begin of program

    End of statement

    End of Segment

    Start of Segment

    Function for printing text

    Insert a new line

  • 8/11/2019 Chapter1_IntrotoC

    28/29

    C Output

    I like programming in C.

  • 8/11/2019 Chapter1_IntrotoC

    29/29

    Summary

    We have looked at some underlying hardware

    We have seen some different types of

    languages;

    the relevance of semantics and syntax.

    We have observed the detail necessary in an

    imperative language to instruct a computer

    properly. Finally, we examined the syntax to print a line

    of text to the screen of our computer.