Sie sind auf Seite 1von 28

Introduction

CSIS1117A
CS S
Computer Programming I

1
Contents
 Introduction to hardware and software
 Steps for running a program
 L
Layoutt off a C
C++ program
 Testing and Debugging

c1117 lecture 1 2
Introduction
 Basic concepts
 Program: A set of instructions for a computer to follow
 Hardware: The actual physical parts that make up a
computer
 Software: The collection of programs used by a computer
 Hardware (only the simple model that is sufficient
for our programming purpose is mentioned)
 Basic components: Central processing unit, main memory,
input/output devices,
devices secondary memory
memory.

c1117 lecture 1 3
Output device

Input device

P
Power Supply
S l

H d Disk
Hard Di k
DVD Drive

Main Memory

CPU
c1117 lecture 1 4
Hardware
 Ce t a Processing
Central ocess g U
Unitt (C
(CPU):
U) tthee “brain”
ba of computer
o co pute
use to perform basic operations like simple calculation

A Pentium chip

 Main memory:y For temporary


p y storage
g of data &
information
 The information will be lost when the computer is
switched off

c1117 lecture 1 5
Hardware
 Input/output device: allow computer to communicate
information with users.
 e.g keyboard, mouse, monitor, speaker, printer
 Secondary memory: For permanent storage of data &
information in units of files
 e.g. hard disk, ssd (solid-state drives), DVD/CD ROM,
memory stick
 The information will NOT be lost even after turning off
the computer

c1117 lecture 1 6
Hardware

CPU

Input Output
devices Main memory devices

Secondary
memory
c1117 lecture 1 7
Software
 Software that are frequently encountered in
programming
 Operating system,
system Command prompt
prompt, Editor
Editor, Compiler
Compiler,
Debugger
 Operating System
 A program that is running as long as the computer is on
 It acts as a manager to coordinate the resources of a
computer e.g. read data from disk, execute programs.

c1117 lecture 1 8
為了協助保護您的隱私權, PowerPoint 無法自動下載此外部圖片。若要下載並顯示此圖片,請按一下訊息列的 [選項 ],然後按一下 [啟用] 外部內容。

Operating System (OS)


 It provides graphical user interface (GUI) for users to
perform basic tasks easily e.g. controlling the printer,
facilitating networking and managing files.
 Common OS: Windows, DOS, UNIX, Linux, MacOS.

Window XP
desktop

c1117 lecture 1 9
More
 Command prompt
 A program that allows user to interact with the OS
to start or stop programs
 Init by typing “cmd” in RUN for windows.

 Editors
 A word
word-processor
processor like program used to create our
programs, e.g. emacs, vi, pico, notepad, ultraEdit
c1117 lecture 1 10
 Compilers
 A program that translates the high-level program into
low-level
low level native code, which is understandable by a
computer
 E.g. g++, gcc, vc++, pascal compiler, etc.
 High-level language (such as C++) is closer to English
and is more human-readable.

int main() {
t width
int dt = 5;
int height = 10;
int area = width * height;
}

c1117 lecture 1 11
Low-level
Low level language
 However,, a computer
p can onlyy understand pprogram
g
written in it’s low-level language (instructions), which
varies from different types of computers.
 Th following
The f ll i are some extracted
t t d low-level
l l l instructions
i t ti
of the previous program in two different types of
computers
p (Pentium
( & Sparc).
p )

Move value 5 movl $5, -4(%ebp)


Multiply the
and 10 to o
movl $10,
$ 0, -8(%ebp)
8(%ebp)
values in the
two memory movl -4(%ebp), %eax
two locations
locations movl %eax, %edx
i ll -8(%ebp),
imull 8(% b ) %%edx
d
movl %edx, %eax
Move the result to
another location
c1117 lecture 1
Pentium
12
mov 10,
, %o0
Move value 5
st %o0, [%fp-20] Multiply the
and 10 to
mov 5, %o0 values in the
two memoryy
st
t % 0 [%f
%o0, [%fp-24]
24] two locations
locations
ld [%fp-20], %o0
ld [%fp-24],
[ p ], %o1
call .umul, 0
Move the result to
mov %o0, %o1
another location
Sparc

 A program written in high


high-level
level language can be run on
different machines if the appropriate compilers exist.

c1117 lecture 1 13
Steps for running a program
Compiler
Create a program Source code
Check the syntax
using an editor e.g. hello.cc

Library 1
Generate object
Library 2 code

……
Object code
e.g. hello.obj
Executable
E t bl Combine
C bi with
ith library
lib
e.g. hello.exe components by a linker

c1117 lecture 1 14
Steps for running a program

 C t a program with
Create ith an Editor
Edit
 Source code: Your program written in high-level
language (e.g.
(e g C++)

 Compile the program by compiler


 The syntax is checked.
 The syntax of a language is the structures and rules that
one has to follow, just like the grammar in English
 If there is no syntax error
error, the compiler translates it into
low-level language, the object code.

c1117 lecture 1 15
Steps for running a program
 Link necessary library codes by linker
 The object code produced by the compiler usually is
not enough,
enough e.g.
e g your program may need codes for
system input/output, which you don’t know how to
write,, but have been written byy some experts.
p The
linker program helps to link them to your program
automatically.
 We seldom deal with linker in this course.
 An executable is produced after linking.
 Usually, compile-and-link in one single step.

 Put the generated executable into execution


c1117 lecture 1 16
IDE
 Software that integrates the steps of
developing a program (e.g. editing,
compiling linking,
compiling, linking debugging) is called an
Integrated Development Environment (IDE)
 netbeans/C++
 An window-base IDE for C++ programming
(originally designed for Java programs)
 This software is free and can be easily
downloaded from the Internet
 You will learn more on this in workshop

c1117 lecture 1 17
A simple C++ program
 Here is a simple C++ program, hello.cc

// This is a simple c++ program.


#include <iostream>
using namespace std;
int main(){
cout << "Hello World!" << endl;
return 0;
}

c1117 lecture 1 18
Layout of a C++ program
 // Specifying
//…: p y g the Comments. The whole line is
ignored by the compiler.
 A Comment line starts with a double slash and is valid
till the end of the line.
 They make a program more readable.
 #include <…>: Statement tells ll the
h compiler
l that
h
the program requires external library components.
 U i library
Using lib can speed
d up development
d l t
 int main(){…}: The main function. A essential
componentt off a C++ program.
 When a program is put into execution, it starts at the
main function
c1117 lecture 1 19
Layout of a C++ program
 cout: It is used to print the output to screen.
 Imagine that cout is connected to screen, text to be
printed are inserted into cout using
p g <<,, the insertion
operator.
 endl: It is used to p
produce a newline ((end of line).
)
 return 0: Indicating the end of execution of the
main function,
function and the control is returned to OS
 using namespace std: you will know about it
later

c1117 lecture 1 20
String literals
 A string literal (or constant) is a sequence of
characters enclosed by a pair of double quotes
 e g “This is a string”
e.g. string”, “Hello world!”
 You can think literals as something not interpreted
b the
by th computer,
t it iis the
th same as what
h t we see.
 cout << “Hello world!” << endl;
 Meant prints the string Hello world! to the screen
followed by a newline (equivalent to press the “Enter”).

Hello world!
New line

c1117 lecture 1
Output of the program hello.cc
21
Layout of a simple C++ program
#include <iostream>
using namespace std;
int main () {
statement 1;
statement 2;
. . . . .
statement n;
return 0;;
}

 Statements in a program are executed sequentially.


sequentially
 There are many different kinds of statements in
C++ we will
C++, ill describe
d ib them
th later
l t
c1117 lecture 1 22
Statements
 The C++ Statement is the basic building block of
a function, which contains a sequence of
statements.
statements
 Note that each statement inside a function ends
with a semi-colon.
semi colon
 DON’T forget the semi-colon in C++!

c1117 lecture 1 23
Expressions
 C++ statements usually contain expressions.
expressions
 Expressions are composed of operators and
operands and they contain values.
values
 e.g. 23*89/2,(2+4)*(30-99) (* -- multiplication in
programming language)
 Radius*2*3.14159 (radius is called a variable)
 Besides arithmetic operators
operators, there are logical
logical,
relational, bit shifting, etc.
 S quote.cc as an example
See l in
i illustrating
ill t ti theth
different between quoted strings and simple
expressions.
i
c1117 lecture 1 24
Free formatting
 The amount of whitespaces
p ((spaces/
p / newlines//
tabs) are not important in C++
 You mayy add whitespaces
p at suitable p
places to make
your program easier to read.
 For example, the program hello.cc can be written as
the following. It’s not easy to be read.
#include <iostream>
using namespace std;int main(){cout <<
"Hello World!" << endl; return 0;}
 However for some cases
However, cases, space is important
important.
 e.g. it is wrong to write "#in clude <iostream>"

No space is allowed here


c1117 lecture 1 25
Testing and Debugging
 A mistake in a pprogram
g is called a bug,
g, and the
process of eliminating bugs is called debugging.
 Three
ee kinds
ds oof poss
possible
be pprogramming
og a g errors:
e os
 Syntax errors: Errors resulting from violating of
programming syntax.
 Syntax error can be caught during compilation
 Run-time errors: Errors occur during the execution of
the
h program. It I may cause theh program stops at the h
middle, e.g. divide by zero.
 Logical errors: Errors in your program due to incorrect
logic flow. It is the error that most difficult to locate, as
it is not reported
p byy compiler,
p , but incorrect result mayy
be produced.
c1117 lecture 1 26
Logical error
 An Example of logical error
 If you mistakenly use the addition sign instead of
multiplication sign in the program in p.10
 The program would compile and run normally, but
would ggive a wrongg answer.
int main( ) {
int width = 5;
int height = 10;
int area = width + height;
}

W
Wrong area will
ill be
b calculated
l l d
c1117 lecture 1 27
Debugging Tool
 A Debugger is a tool to help debugging programs
 It is included in the IDE of netbeans/C++ (using
the gnu gdb debugger)
 Features available
 Set break points – stop the execution at a specified
place.
 Si l step
Single t – execution
ti one statement
t t t att a ti
time
 Variable watch – watch the content of variables.
 Increase productivity significantly.
 Debugger
gg is an indispensible tool for programmers,
g
and should learn to use it as early as possible.
c1117 lecture 1 28

Das könnte Ihnen auch gefallen