1
COMPUTER
PROGRAMMING
Programming Basics
What will be discussed
2
Structures of C++ program
Variable and identifier
Standard Data Types
Standard Streams
Constants and Operators
Standard Streams
About TheLectureNote
3
Large portion of the materials is based on the lecture note
supplied by the authors of the textbook “Computer Science : A
Structured Approach Using C++, 2nd Edition.”
Hierarchy of Programming
4
Language
Hierarchy of Programming
5
The movie “Matrix”
Can’t you
see? Neo is
here.
Computer Programmin g(CSC-113) - Fall 2019
Hierarchy of Programming
6
The movie “Matrix”
Now you can
pick Neo, but
not clearly.
Computer Programmin g(CSC-113) - Fall 2019
Hierarchy of Programming
7
The movie “Matrix”
Examples of Programming Language
- HTML(Hyper Text MarkupLanguage)
<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=euc-kr">
<title>▒경희대학교 전자정보대학▒</title>
</head>
<frameset rows="1*" cols="100%" border="0">
<frame name="electronic" scrolling="auto"
marginwidth="0" marginheight="0"
src="electronic/main.php">
<noframes>
<body bgcolor="#FFFFFF" text="#000000"
link="#0000FF" vlink="#800080" alink="#FF0000">
<p> </p>
</body>
</noframes>
</frameset>
</html>
Examples of ProgrammingLanguage
- COBOL (COmmon Business-Oriented Language)
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. Multiplier.
AUTHOR. Michael Coughlan.
* Example program using ACCEPT, DISPLAY and MULTIPLY to
* get two single digit numbers from the user and multiply them together
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE ZEROS.
01 Num2 PIC 9 VALUE ZEROS.
01 Result PIC 99 VALUE ZEROS.
PROCEDURE DIVISION.
DISPLAY "Enter first number (1 digit) : " WITH NO ADVANCING.
ACCEPT Num1.
DISPLAY "Enter second number (1 digit) : " WITH NO ADVANCING.
ACCEPT Num2.
MULTIPLY Num1 BY Num2 GIVING Result.
DISPLAY "Result is = ", Result.
STOP RUN.
Brief History of C++
11
Structure of a C++ Program
11
Pre-compiler directive
Opening brace
Closing brace
Opening brace
Closing brace
Hello World!
Namespace std contains Without namespace
all the classes, objects and
functions of the standard #include <iostream>
C++ library. int main () {
std::cout << "Hello world!\n";
return 0;
}
Preprocessor Directives
#include <iostream>
“I want to use a predefined library called iostream”
Always start with a ‘#’
iostream: a library for inputs (from e.g., a user) and outputs
(to e.g., the monitor)
“using” Directives
14
using namespace s t d ;
“I want to use objects in a name group
‘std’ ”
Tells the compiler where to look for names
in the library
Can deal with the situation where two or
more objects in different libraries share a
samename (namingconfliction).
Read Appendix N for more about
namespace
main function
15
i n t main()
The main body of the program.
Compiler first tries to locate
“main()” to find where to begin the
program
In the form of a function
I will cover “function” soon
Comment
16
Internal program document
Not considered as a program code
Start of comment
End of comment
Start of comment
End of comment
Nested BlockCommentsareInvalid
17
Variables
18
Named memory locations that have a type
Named: identifier
Type: needs declaration
What you can do with variables
Storing data
Modifying data
Reading data
Variables and Identifiers
19
Memory
Address of memory:
Hard to remember
Identifier: name of
address
Variables and Identifiers
20
Memory
Identifiers
studentID
studentGrade1
studentGrade2
Variables and Identifiers
Memory
studentID
studentGrade
studentName
Compiler keeps track
of [identifier-address]
table
Variables and Identifiers
22
In program
stu dent I D_Tot a l _Gr ade = s t udent G rade1 +
studentGrade2
Naming Identifiers
23
Allowed characters: A-Z, a-z, 0-9, _ (underscore)
Not allowed to start with a digit. E.g., 3class (x),
class3(o)
The identifier cannot duplicate a reserved word.
e.g., if, case, while…
Good names descriptive but short
C++ is case sensitive; PI, Pi and pi aredifferent.
Standard Data Types
Integer and Floating Point Types
25
2 Bytes 4 Bytes
2 or 4 Bytes 8 Bytes
4 Bytes 10 Bytes
Size of value type depends on computer architecture
Maximum/Minimum of Integer
26
Value Type
Type Sign Byte Minimum value Maximum value
signed -32,768 32,767
short int/short 2
unsigned 0 65,535
signed -32,768 32,767
int (PC) 2
unsigned 0 65,535
signed -2,147,483,648 2,147,483,647
int (Mainframe) 4
unsigned 0 4,294,967,295
signed -2,147,483,648 2,147,483,647
long int/long 4
unsigned 0 4,294,967,295
27
Variables Declaration
28
Variable Initialization
29
Variable declaration ≠ variable initialization
Should be initialized by a programmer before it is
used
e.g.,
i n t count; declaration (o), initialization(x)
char grade = ‘ d ’ ; declaration (o),initialization(o)
Constants
30
Data values that cannot be changed during
program execution
E.g.,
3.141592
‘d’
“Hello word”
‘\0’
ToRemember
32
A character constant is enclosed by the single
quotes. (e.g. ‘a’)
Use double quotes for string constants. (e.g. “Jeon,
Seokhee”)
bool types are treated as a number. True: non-zero.
False: zero.
Operators
33
Assignment Operators
Arithmetic operators
+ (Addition)
- (Subtraction
* (Multiplication)
/ (Division)
% (modulo)
34
C++ ExpressionFormat
Operators
Operators
36
Increment and decrementoperators
Pre/ post increment
Pre / postdecrement
Sizeof operator
Example
A=sizeof(b);
Compound Assignment
37
Shorthand notation for a simple assignment
Examples
Standard streams
38
A mapping between data and input/output device
Usingiostream.h
39
Include iostream.h instead of stdio.h
Standard iostream objects:
cout - object providing a connection to the monitor
cin - object providing a connection to the keyboard
cerr - object providing a connection to error streem
Toperform input and output we send messages to one
of these objects (or one that is connected to a file)
TheInsertion Operator (<<)
40
Tosend output to the screen we use the insertion
operator on the object cout
Format: cout << Expression;
The compiler figures out the type of the object and
prints it out appropriately
cout << 5; // Outputs 5
cout << 4.1; // Outputs 4.1
cout << “String”; // Outputs String
cout << ‘\n’; // Outputs a newline
TheExtraction Operator (>>)
41
Toget input from the keyboard we use the extraction
operator and the object cin
Format: cin >> Variable;
No need for & in front of variable
The compiler figures out the type of the variable and
reads in the appropriate type
int X;
float Y;
cin >> X; // Reads in an integer
cin >> Y; // Reads in a float
More about cout
42
w i d t h ( i n t ) function sets the width for printing a
value
Only works until the next insertion command comes
i n t x = 42;
cou t. w idth (5 ) ;
cout << x << ‘ \ n ’ ; // Outputs 42
cout << x << ‘ \ n ’ ; // Outputs 42
More about cout
44
f i l l ( c h a r ) function sets the fill character.
The character remains as the fill character until set
again.
i n t x = 42;
cout.width(5);
cou t. f i l l ( ‘ * ’ ) ;
cou t << x << ‘ \ n’ ; / / Ou tput s * * * 42
More about cout
47
Output Manipulators (not a function)
endl - outputs a new line character, flushesoutput
dec - sets int output to decimal
hex - sets int output to hexadecimal
o c t - sets int output tooctal
#include <iomanip.h>
i n t x = 42;
cout << o c t << x << e n d l ; / / Outputs 52\n
cout << hex << x << e n d l ; / / Outputs 2a\n
cout << dec << x << e n d l ; / / Outputs 42\n
Example codesreading (Program 2-2)
#include <iostream>
using namespace s t d ;
Welcome. This program adds
int main (void)
three numbers. Enter three numbers
{
in the form: nnn nnn nnn <return>
11 22 33
int a;
int b; The total is: 66
int c;
int sum; Thank you. Have a good day.
cout << "Welcome. This program adds\n";
cout << " t h r e e numbers. Enter three numbers\n";
cout << " i n the form: nnn nnn nnn < r e t u r n > \ n " ;
c i n >> a >> b >> c ;
/ / Numbers are now stored i n a , b , and c . Add them.
sum = a + b + c ;
cout << "\nThe t o t a l i s : " << sum << " \ n " ;
cout << "\nThank you. Have a good d a y. \ n " ;
return 0;
} / / main
Example
Example
51
Conclusion
52
In this lecture …
What is structure of C++?
What is variable and Identifier?
What Is Standard Data Types?
What are operators in the language?
What are strems?
ANY QUERY?
53 Sample examples
See examples provided in chapter 4 on introduction to
programming in C++ BY Dian Zak
Exercise given on page 104 -107