204111 Computer and 204111 Computer and Programming Lecture #1: - - PDF document

204111 computer and 204111 computer and programming
SMART_READER_LITE
LIVE PREVIEW

204111 Computer and 204111 Computer and Programming Lecture #1: - - PDF document

204111 Computer and 204111 Computer and Programming Lecture #1: Introduction Massive Information & Knowledge Engineering http://mike cpe ku ac th/204111 http://mike.cpe.ku.ac.th/204111 Department of Computer Engineering Kasetsart


slide-1
SLIDE 1

204111 Computer and 204111 Computer and Programming

Lecture #1: Introduction

Massive Information & Knowledge Engineering http://mike cpe ku ac th/204111 http://mike.cpe.ku.ac.th/204111 Department of Computer Engineering Kasetsart University Bangkok, Thailand.

MIKE TM approved

Version 2006/1

Introduction

Programming is to solve problems using computers

H t d it t ll ?

  • How to do it at all ?
  • How to do it robustly ?
  • How to do it effectively ?

Programming consists of two steps:

  • Algorithmic design (the architects)
  • Coding (the construction workers)

Coding (the construction workers)

Programming requires:

  • a programming language (e.g., C#) to express your ideas

g g g g ( g ) y

  • a set of tools to design, edit, and debug your code
  • compiler to translate your programs into machine code
  • a machine to run the executable code

MIKE TM approved

Version 2006/1

2

slide-2
SLIDE 2

Why 204111 ? y

Computer is becoming ubiquitous …

  • programming gets you more out of computer
  • learn how to solve problems
  • dealing with abstractions
  • dealing with abstractions
  • be more precise

Unfortunately, most people

y, p p

  • know little about Computer Science
  • know little about Programming

write bad or buggy programs

  • write bad or buggy programs
  • become lost when writing large programs

What can help?

Take 204111 !

What can help?

Take 204111 !

MIKE TM approved

Version 2006/1

3

This Course

The goal of 204111 is to learn

  • elementary and intermediate programming techniques
  • how to craft small to large programs

C# programming language (Java is similar)

  • C# programming language (Java is similar)
  • some important algorithms and data structures
  • some software tools and operating systems

i d th k l d b t C t S i

  • some in-depth knowledge about Computer Science

A heavy programming course Use Microsoft .NET C# Compiler Intended mainly as your first programming course

MIKE TM approved

Version 2006/1

4

slide-3
SLIDE 3

Crafting Programs Effectively g g y

Program design

d i

  • design process
  • stepwise refinement & top-down design
  • bottom-up design
  • modularization, interfaces
  • use of abstractions

Programming style

  • structured programming
  • readable code
  • effective use of language constructs

g g y

effective use of language constructs

  • “formatting”

software organization D t ti d t

Documentation and comments

MIKE TM approved

Version 2006/1

5

Algorithms and Data Structures g

Algorithm: a strategy for computing something, e.g.,

  • sorting: putting data in order by key
  • searching: finding data in some kind of index
  • finding primes and generating random numbers
  • string processing
  • graphics: drawing lines, arcs, and other geometric objects

Data structure: a way to store data e g Data structure: a way to store data, e.g.,

  • arrays and vectors
  • linked lists

Two are related:

  • data structures organize data
  • algorithms use that organization

algorithms use that organization

MIKE TM approved

Version 2006/1

6

slide-4
SLIDE 4

Java Programming Language g g g g

Sun Microsystems corporate research project (1991) -

Code-named Green, led by James Gosling and Bill Joy - Based on C and C++

Intended for intelligent consumer-electronic devices

  • Lack of popularity almost causes cancellation

Sudden popularity of WWW provides new potential Sudden popularity of WWW provides new potential

  • Java capable of dynamic content (e.g., animation, interaction)
  • Grabbed attention of business community

Now very widely used

  • Enhance functionality of WWW servers
  • Provide applications for consumer devices

Provide applications for consumer devices

MIKE TM approved

Version 2006/1

7

.NET Framework

Introduced by Microsoft (June 2000)

  • Vision for embracing the Internet in software development
  • Heart of .NET strategy

Language- and “platform-” independence

  • Visual Basic .NET, Visual C++ .NET, C# and more
  • Includes Framework Class Library (FCL) for reuse

Language and platform

independence

  • Programs compiled to Microsoft Intermediate Language (MSIL)
  • MSIL code translated into machine code

Executes programs by Common Language Runtime (CLR)

(Unlike Java) this is mostly Windows (MS) centric

  • There is a Linux port (see the Mono project)

MIKE TM approved

Version 2006/1

8

slide-5
SLIDE 5

C# Programming Language g g g g

Developed at Microsoft by Anders Hejlsberg et al Event driven, object oriented, visual programming language Based from C, C++ and Java

  • Web based applications can be distributed
  • Programs that can be accessed by anyone through any

Incorporated into .NET platform

device

  • Allows communicating with different computer languages

Integrated Design Environment (IDE)

g g ( )

  • Makes programming and debugging fast and easy
  • Rapid Application Development (RAD)

MIKE TM approved

Version 2006/1

9

Why C# (not C++) in 204111 ? y ( )

They support so-called safe internet programming. They support so called safe internet programming. They are simpler than other object-oriented languages [C++]. They are safe and robust --- no core dump or dead console. They have good graphics package. They are related to C and C++. They

have good client-server and network support.

They are good for your summer job. However, they are not easy to learn; each has many features … Right now, C# has a better buzz than Java.

MIKE TM approved

Version 2006/1 10

slide-6
SLIDE 6

Learning C# g

Just like learning any new language

g y g g

Syntax: “new words” Grammar: how to put them together Programming: telling a coherent story Library: use plots already written Library: use plots already written Initially overwhelming, but pays off in the end! Picking up Java and C++ after C# should be easy! Picking up Java and C++ after C# should be easy!

MIKE TM approved

Version 2006/1 11

Course Information

Lecture hour: Mon 10-12.00, E0803, 60-Yr Building Lab hour: Tue 13-16.00, E201, Engineering Computing Center Information, assignments, & lecture notes are available on-line.

We won’t use much paper !

Official URL: http://tmp-www.cpe.ku.ac.th/~mcs/courses/2007_02/204111 Pl

d li i f ti t l t t d !

Please read online information at least once every two days!

MIKE TM approved

Version 2006/1 12

slide-7
SLIDE 7

Course Assignments g

Around 8 assignments in all

M l i f ll & l & diffi l

  • Mostly programming; from small & easy to large & difficult
  • Due on the date specified on the schedule sheet
  • Assignments will be posted online for download (no paper handout! )

Any C# programming environment would work!

  • Microsoft Visual Studo . NET (C# IDE)
  • Need 3.3 GB disk space & 256 MB memory

p y

  • .NET Framework SDK (batch C# compiler,

~130 MB disk space)

TextPad editor download at

http://tmp-www.cpe.ku.ac.th/~mcs/courses/2007_02/204111/download.html

Almost all assignments must be submitted online (see the web site

for detail)

MIKE TM approved

Version 2006/1 13

Grading Policy g y

10% for Assignments + Lecture and Lab attendance 10% for Assignments Lecture and Lab attendance 10% for unannounced quiz 20% for 2 lab exams (10% each) 30% for written midterm exam 30% for written final exam

MIKE TM approved

Version 2006/1 14

slide-8
SLIDE 8

Copying/Cheating Arrangement py g g g

1st time, -1 polar (ลบ 1 ประจ) 2nd time, another -2 polar, and according to

th f lt f i i li th the faculty of engineering policy, those involved will be sent to the Dean for disciplinary action. disciplinary action.

Unless stated otherwise, all projects are Unless stated otherwise, all projects are

individual assignments and are expected to be the student's own work.

MIKE TM approved

Version 2006/1 15

Definition of Computer Definition of Computer

D i f f i t ti t

  • Devices for performing computations at

high speeds with great accuracy

  • A machine that can be programmed to

manipulate symbols

  • Physical components are known as

“Hardware”

16

slide-9
SLIDE 9

Computer System p y

Hardware (HW)

Actual physical machines (equipment) that make

  • Actual physical machines (equipment) that make

up the computer.

Software (SW) Software (SW)

  • Programs written for a specific application are
  • ften called software (e.g., Windows XP,

Linux, Microsoft Office) , )

MIKE TM approved

Version 2006/1 18

Computer Categories Computer Categories

  • Personal Computer

p

– Desktop Computer – Notebook – PDA – Personal Digital Assistant

  • Supercomputer Mainframe

p p

– High Computation Power

18

slide-10
SLIDE 10

Computer Components p p

Secondary Secondary Memory Main Memory HD

CPU

Output Devices Input Devices

MIKE TM approved

Version 2006/1 17

Computer Components Computer Components

  • Input Devices

p

  • Output Devices
  • Computing & Processing Unit
  • Computing & Processing Unit

20

slide-11
SLIDE 11

Computer Components (pictures) Computer Components (pictures)

Hard disc CPU

21

Computer Memory Computer Memory

Can be divided into 2 Categories Can be divided into 2 Categories

– RAM (Random Access Memory) ROM (Read Only Memory) – ROM (Read Only Memory)

M i C

  • Memory in Computer

– Main Memory – Secondary Memory

22

slide-12
SLIDE 12

Main Memory VS Secondary Memory Main Memory VS Secondary Memory

  • Main Memory
  • Secondary Memory
  • Main Memory

–Much faster

  • Secondary Memory

–Slower –More expensive –Volatile –Less expensive –Permanent

23

  • 1. Main Memory
  • 1. Main Memory
  • Memory Cell ‐> Byte ‐> bit

Memory Cell > Byte > bit

  • 1 Byte = 8 bits

24

slide-13
SLIDE 13
  • 2. Secondary Memory
  • 2. Secondary Memory
  • Floppy disk

Floppy disk

  • Hard disk

CD ROM CD R CD RW

  • CD‐ROM, CD‐R, CD‐RW
  • DVD‐ROM, DVD‐R, DVD‐RW

Etc…

25

Data Representation Data Representation

  • Data in computer is represented in “bit”

– bit = binary digit

  • 0 or 1

B – Byte = 8 bits

– 1 byte can represent many kinds of data

  • 1 byte = 011000012

1 byte 011000012

  • the above 1 byte means, “a” or 97
  • the meaning of 1 byte depends on the program

1 Kbyte = 210 = 1024 bytes 1 Mbyte = 220 = 1,048,576 bytes 1 Gbyte = 230 = 1 073 741 824 bytes

26

1 Gbyte = 230 = 1,073,741,824 bytes 1 Tbyte = 240 = 1,099,511,627,776 bytes

slide-14
SLIDE 14

Programming Languages Programming Languages

  • Program

– A set of instructions for a computer to follow, written in specific programming language

  • Types of programming language

yp p g g g g

– High‐Level Language – Machine Language Machine Language – Assembly Language

27

High‐level VS Assembly VS Machine Language High level VS Assembly VS Machine Language

  • High‐level Language
  • Uses English-like language (Nearly like human words)

* /

Uses English like language (Nearly like human words)

  • - Machine independent
  • - Portable
  • Exemple: C, C++, Java, C#

SUM := A * 2 + ALPHA/3;

  • Assembly Language

– Some key words are understandable Some key words are understandable

MULL3 A, #2, R ADDL3 R6, R7, SUM

M hi L l h

  • Machine Language: language that computers

understand

– Binary code (only “0” and “1”)

28

y ( y )

00011000011 00011001111 10011000111

slide-15
SLIDE 15

High-level Languages g g g

Procedural Language Fortran

  • Fortran
  • Cobol
  • Basic

C

  • C
  • Pascal

Object-Oriented Language

  • C++
  • Java
  • C#

Functional Language Functional Language

  • Lisp

Logic Language

g g g

  • Prolog

MIKE TM approved

Version 2006/1 20

Translation Process

Executable Compiler Source Program Program /I t t g Program /Interpreter

MIKE TM approved

Version 2006/1 21

slide-16
SLIDE 16

Language Translator g g

Interpreter …… / Compiler

…. main:

Assembler

pushl %ebp 00011000110001110 movl %esp, %ebp 00110001110101111 00110001110101111 subl $8, %esp

class MainClass

andl $-16, %esp 00011111111110001

{

movl $0, %eax

public static void Main(string[] args)

11011100001011011

{

subl %eax, %esp

Console.WriteLine("Hello World!");

subl $8, %esp ……

}

pushl $ LC0 Machine

}

pushl $.LC0 Machine

}

……..

Machine language High-level language Assembly language

Hello World! _

MIKE TM approved

Version 2006/1 22

Interpreter p

Read high-level language , translate , and

g g g then execute one command at a time.

…. 0100100

a

writeln(‘a’);

Inter-

writeln(‘b’); 0100101

b

writeln(‘c’); ( ); 0100110 ….

preter c Source

MIKE TM approved

Version 2006/1 23

slide-17
SLIDE 17

Compiler p

Read all program at a time. Translate into executable file (machine

language) library

…. writeln(‘a’);

Linker Compiler

writeln( a );

Linker Compiler

writeln(‘b’);

  • bject

writeln(‘c’); ….

file Source Source a

…. 0100100 0100101

b

0100101

Exe file c

MIKE TM approved

Version 2006/1 24

Programming Languages g g g g

The computer really only knows its own machine language or

assembly language Any high level languages would have be assembly language. Any high level languages would have be translated into machine language.

An assembler translates an assembly language program into An assembler translates an assembly language program into

machine language.

A compiler translates a high-level language into machine

p g g g language.

A source program is written in a high-level programming

l ( )

  • language. (.cs, .pas, .c)

An executable program is the machine language version of a

source program ( exe) source program. (.exe)

MIKE TM approved

Version 2006/1 25

slide-18
SLIDE 18

Short brake

5 minutes

MIKE TM approved

Version 2006/1 26

2nd-Half Outline

P

i l l l

Programming language levels Structure of C# program Compiling and running our first C# programs

p g g p g

MIKE TM approved

Version 2006/1 27

slide-19
SLIDE 19

Programming Language Levels g g g g

Each type of CPU has its own specific machine Each type of CPU has its own specific machine

language.

Oth

l l t d t ti f diff t

Other levels were created to satisfy different

  • bjectives, e.g., make it easier for a human being

to write programs to write programs

  • machine language
  • assembly language

y g g

  • intermediate language
  • high-level language

MIKE TM approved

Version 2006/1 28

Example Machine Code Fragment p g

177312 137272 001400 026400 017400 000012 000007 004420 010400 011000 000010 005023 012000 012400 000010 003426 013400 000007 000430 003000 064474 064556 037164 000001 024003 053051 000001 041404 062157 000545 007400 064514 062556 072516 061155 071145 060524 066142 000545 002000 060555 067151 000001 024026 046133 060552 060566 066057 067141 027547 072123 064562 063556 024473 000526 005000 067523 071165 062543 064506 062554 000001 046014 067151 067543 067154 065056 073141 006141 004000 004400 000007 006031 015000 015400 000001 040433 070440 067565 062564 060552 060566 064457 027557 071120 067151 051564 071164 060545 000555 003400 071160 067151 066164 000556 012400 046050 060552 060566 066057 067141 027547 072123 064562 046050 060552 060566 066057 067141 027547 072123 064562 063556 024473 000126 000041 000006 000007 000000 000000 000002 000001 000010 000011 000001 000012 000000 000035 000001 000001 000000 025005 000267 130401 000000 000400 005400 000000 003000 000400 000000 003400 004400 006000 006400 000400 005000 000000 030400 001000 000400 000000 010400 000262 011002 133003 002000 000262 011002 133005 002000 000261 000000 000001 000013 000000 000016 000003 000000 000016 000010 000020 000020 000021 000001 000016 000000 000002 000017

A b ifi h t ti th t h ld t k A number specifies what action the computer should take.

MIKE TM approved

Version 2006/1 29

slide-20
SLIDE 20

Example Assembly Code Fragment

movl (%edx,%eax), %ecx movl 12(%ebp), %eax leal 0(,%eax,4), %edx movl $nodes, %eax l (% d % ) % movl (%edx,%eax), %eax fldl (%ecx) fsubl (%eax) movl 8(%ebp) %eax movl 8(%ebp), %eax leal 0(,%eax,4), %edx movl $nodes, %eax movl (%edx %eax) %ecx movl (%edx,%eax), %ecx movl 12(%ebp), %eax leal 0(,%eax,4), %edx movl $nodes, %eax $ ,

Symbols to help programmers to remember the words.

MIKE TM approved

Version 2006/1 30

Example C++/C#/Java Code Fragment

bool DetermineNeighbor(int i, int j) { double distanceX = (nodes[i].x - nodes[j].x); double distanceY = (nodes[i].y - nodes[j].y); double di t S di * di di * di d bl distanceSquare = disx * disx + disy * disy; double distance = sqrt(distanceSquare); if (distance < radius) return true; else return false; }

You do not need to understand the exact meaning of this program, just the feeling. g p g j g

MIKE TM approved

Version 2006/1 31

slide-21
SLIDE 21

Programming Languages (again) g g g g ( g )

A program written in a high-level language must A program written in a high-level language must

be translated into machine language before it can be executed on a particular type of CPU. y

A compiler is a software tool which translates

d i ifi l source code into a specific target language.

MIKE TM approved

Version 2006/1 32

C# Translation and Execution

The C# compiler translates C# source code (.cs

fil ) i t i l t ti ll d files) into a special representation called Microsoft Intermediate Language (MSIL)

MSIL is not the machine language for any

traditional CPU, but a virtual machine.

The Common Language Runtime (CLR) then

interprets the MSIL file interprets the MSIL file

  • It uses a just-in-time compiler to translate from MSIL

format to machine code on the fly. y

MIKE TM approved

Version 2006/1 33

slide-22
SLIDE 22

C# Compilation and Execution p

C# source C# source code MSIL C# compiler Just in time Just in time compiler Machine code

MIKE TM approved

Version 2006/1 34

2nd-Half Outline

P

i l

Programming languages Structure of a C# program Compiling and running our first C# programs

p g g p g

MIKE TM approved

Version 2006/1 35

slide-23
SLIDE 23

A Simple C# Program p g

//========================================================== // // File: HelloWorld.cs 204111 Assignment 00 // // Author: Kun Toto Email: KunToto@ku.ac.th // // Classes: HelloWorld // // Classes: HelloWorld // -------------------- // This program prints a string called "Hello, World!” // //==========================================================

using System;

class HelloWorld { static void Main(string[] args) { static void Main(string[] args) { Console.WriteLine(“Hello, World!”); } }

MIKE TM approved

Version 2006/1 36

C# Program Structure g

  • Program specifications (optional)

//========================================================== // // File: HelloWorld.cs 204111 Assignment 00 // // Author: Kun Toto Email: KunToto@ku.ac.th // // Cl H ll W ld // // Classes: HelloWorld // -------------------- // This program prints a string called "Hello, World!” //

//==========================================================

Library imports (optional) Library imports (optional)

using System;

  • Class definitions

l H ll W ld class HelloWorld { static void Main(string[] args) { Console.WriteLine(“Hello, World!”); ( ) }

}

MIKE TM approved

Version 2006/1 37

slide-24
SLIDE 24

White Space and Comments p

White Space

I l d li h b bl kli

  • Includes spaces, newline characters, tabs, blanklines
  • C# programs should be formatted to enhance readability,

using consistent indentation!

  • Comments are ignored by the compiler: used only for

human readers (i.e., inline documentation)

Comments

u a eade s ( e , e docu e a o )

  • Two types of comments
  • Single-line comments use

//… // this comment runs to the end of the line

  • Multi-lines comments use /* … */

/* this comment runs to the terminating symbol, even across line breaks */ symbol, even across line breaks /

MIKE TM approved

Version 2006/1 38

Identifiers

Identifiers are the words that a programmer uses in a program.

p g p g

An identifier can be made up of letters, digits, and the underscore

character.

They cannot begin with a digit. They cannot begin with a digit. C# is case sensitive, therefore args and Args are different

identifiers.

Sometimes we choose identifiers Sometimes we choose identifiers

using System;

  • urselves when writing a program

class HelloWorld {

(such as HelloWorld).

static void Main(string[] args) {

Sometimes we are using another Sometimes we are using another

Console.WriteLine(“Hello World!”);

programmer's code, so we use

} }

the identifiers that they chose (such as WriteLine). (such as WriteLine).

MIKE TM approved

Version 2006/1 39

slide-25
SLIDE 25

Identifiers: Keywords

Often we use special identifiers called keywords that already have a

predefined meaning in the language predefined meaning in the language.

  • Example: class

A keyword cannot be used in any other way

C# K e ywo rds

abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally p y fixed float for foreach get goto if implicit in int interface internal is lock long namespace new null

  • bject
  • perator
  • ut
  • verride

params private protected bli d l f t b t public readonly ref return sbyte sealed set short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using value virtual void volatile while value virtual void volatile while

All C# keywords are lowercase!

MIKE TM approved

Version 2006/1 40

Namespaces p

Partition the name space to avoid name conflict! All .NET library code are organized using namespaces! By default, C# code is contained in the global namespace To refer to code within a namespace, must use qualified name

p q (as in System.Console) or import explicitly (as in using System; )

using System; namespace System { class HelloWorld { class HelloWorld { i id M i ( i [] ) { i id M i ( i [] ) { static void Main(string[] args) { static void Main(string[] args) { Console.WriteLine(“Hello World!”); Console.WriteLine(“Hello World!”); } } } } } class HelloWorld { static void Main(string[] args) { System.Console.WriteLine(“Hello World!”); } }

MIKE TM approved

Version 2006/1 41

slide-26
SLIDE 26

More on C# Program Structure g

In C#, a program is made up of

, p g p

  • Program specifications (a.k.a. header comments, optional)
  • Library imports (optional)

Library imports (optional)

  • One or more class (and namespace) definitions

A class contains one or more methods

  • A class contains one or more methods.
  • A method contains program statements.

These terms will be explored in detail throughout These terms will be explored in detail throughout

the course.

MIKE TM approved

Version 2006/1 42

C# Program Structure: Class g

// comments about the class class HelloWorld class HelloWorld {

class header class body class body Comments can be added almost anywhere

}

MIKE TM approved

Version 2006/1 43

slide-27
SLIDE 27

C# Classes

Each class name is an identifier.

  • Can contain letters, digits, and underscores (_)
  • Cannot start with digits
  • Can start with the at symbol (@)

Convention: Class names are capitalized, with

each additional English word capitalized as well (e g MyFirstProgram ) (e.g., MyFirstProgram ).

Class bodies start with a left brace ({)

Cl b di d ith i ht b (})

Class bodies end with a right brace (})

MIKE TM approved

Version 2006/1 44

C# Program Structure: Method g

// comments about the class class HelloWorld class HelloWorld { // comments about the method // comments about the method static void Main (string[] args) {

Console.Write(“Hello World!”); Console.WriteLine(“This is from 204111!”);

} }

MIKE TM approved

Version 2006/1 45

slide-28
SLIDE 28

C# Method and Statements

Methods

  • Building blocks of a program
  • The Main method
  • Each console or windows application must have exactly
  • ne (actually can have more, but it is unlikely that you

will see or use).

  • All programs start by executing the Main method.
  • Braces are used to start ({) and end (}) a method.
  • Every statement must end with a semicolon ;

Statements

MIKE TM approved

Version 2006/1 46

1 // Welcome1.cs 2 // A program in C#. 3 4 using System;

Welcome1 cs

4 using System;

Welcome1.cs

5 6 class Welcome1 7 { 8 static void Main( string[] args ) 9 { 9 { 10 Console.WriteLine( "Welcome to C# Programming!" ); 11 } 12 }

Program Output

Welcome to C# Programming!

MIKE TM approved

Version 2006/1 47

slide-29
SLIDE 29

2nd-Half Outline

Programming languages Programming languages Structure of a C# program Compiling and running our first C# programs Compiling and running our first C# programs

MIKE TM approved

Version 2006/1 48

Console Application vs. Window Application

Console Application

pp

  • No visual component
  • Only text input and output

Run under Command Prompt or DOS Prompt

  • Run under Command Prompt or DOS Prompt

Window Application F ith diff t i t d t t t

  • Forms with many different input and output types
  • Contains Graphical User Interfaces (GUI)
  • GUIs make the input and output more user friendly!
  • Message boxes
  • Within the System.Windows.Forms namespace
  • Used to prompt or display information to the user

MIKE TM approved

Version 2006/1 49

slide-30
SLIDE 30

1 // Welcome4.cs 2 // Printing multiple lines in a dialog Box. 3 4 using System; 5 using System.Windows.Forms; 6

Welcome4.cs

7 class Welcome4 8 { 8 { 9 static void Main( string[] args ) 10 { 11 MessageBox.Show( "Welcome\nto\nC#\nprogramming!" ); 12 } 13 }

Program Output

MIKE TM approved

Version 2006/1

Syntax and Semantics

The syntax rules of a language define how we can put

symbols, reserved words, and identifiers together to make a valid program.

The semantics of a program statement define what that

statement means (its purpose or role in a program).

A program that is syntactically correct is not necessarily

logically (semantically) correct.

A program will always do what we tell it to do, not what

we meant to tell it to do.

MIKE TM approved

Version 2006/1 51

slide-31
SLIDE 31

Errors

A program can have three types of errors. The compiler will find problems with syntax and other

basic issues (compile-time errors).

  • If compile-time errors exist, an executable version of the program

is not created.

A bl d i i h

A problem can occur during program execution, such as

trying to divide by zero, which causes a program to terminate abnormally (run-time errors).

A program may run, but produce incorrect results (logical

errors). )

MIKE TM approved

Version 2006/1 52