0% found this document useful (0 votes)
2 views31 pages

Week 02 Lecture01 ProgrammingBasics 01102020 033512pm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

Week 02 Lecture01 ProgrammingBasics 01102020 033512pm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

COMPUTER

PROGRAMMING

Lecture 2
Programming Basics
What will be discussed
 Structures of C++ program
 Variable and identifier
 Standard Data Types
 Standard Streams
 Constants
About The Lecture Note
• 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.”
Structure of a C++ Program

Opening brace

Closing brace

Opening brace

Closing brace
Preprocessor Directives
• A libraries that contain /* A program to print the string
functions and symbols that are Welcome to C++ to the screen
necessary to run a C++ */
program. #include <iostream>
• Every library has a name and is
int main()
referred as a header file. {
• All preprocessor commands /* print out the message */
cout<<"Welcome to C++"<<endl;
begin with #. return 0;
• In Standard C++, header files }

have the file extension .h


• There is no semicolon at the
end of these commands.
#include
#include <headerFileName>
<headerFileName>
• The general syntax to include a
header file
#include <iostream.h>
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;
}
Namespace
• A container for a set of identifiers (also known as symbols, names)
• The built in C++ library routines are kept in the standard namespace
• The statement:
using namespace std;
• pulls everything defined in the std namespace into the global
namespace, as if it had not been defined inside a namespace at all.
This makes it more convenient to access things that are defined in
the std namespace, without having to use the scope resolution
operator to qualify them. So, instead of having to say:
std::cout << myValue << std::endl;
• you can just say:
cout << myValue << endl;
The Program Part: The heading
The heading part has the following form
• Every C++ program has a function
main. typeOfFunction
typeOfFunction main(argument
main(argument list)
list)
• The basic parts of the function main
are:
1. The heading int main()
2. Body of the function {
/* print out the message */
cout<<"Welcome to C++"<<endl;
return 0;
}

• The statement

int main(void) int main()


means that the function main equivalent It is not necessary to put word
returns a value of the type int the void in parentheses, but
and it has no arguments. the parentheses are still
needed.
Comment
• Internal program document
• Not considered as a program code

Start of comment

End of comment
Start of comment

End of comment
Nested Block Comments are
Invalid
Variables
• 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
Memory

Address of memory:

Hard to remember

Identifier: name of
address
Variables and Identifiers
Memory
Identifiers

studentID
studentGrade1

studentGrade2
Variables and Identifiers
Memory

studentID

studentGrade

studentName

Compiler keeps track


of [identifier-address]
table
Variables and Identifiers
In program

studentID_Total_Grade = studentGrade1 +
studentGrade2
Naming Identifiers
• 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 are different.
Identifiers
Some of the predefined identifiers are cout and cin.
Unlike reserved words, pre-defined identifiers may be redefined, but it would
not be wise to do so.

Valid Identifiers Invalid Identifiers

A _a a123
ABcDf99_ sum average 1a_a a+2 a$1
Mark1 Mark_2 xy_1_4
A”bc -abc aa.bb
Sum_of_values avg_plus_sum
A abc a9!c
these are not the same
Aa aa aA AA aa Why they are invalid ?.
Keywords (reserve words)
• Keywords are words (like identifiers) that have a special meaning
to the compiler and cannot be used in your programs for other
purposes.

auto do goto signed unsigned


break double if sizeof void
case else int static volatile
char enum long struct while
continue extern register switch
default float return typedef
for short union const
Standard Data Types
Integer and Floating Point Types

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
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
Maximum/Minimum of C++ data
types
Variables Declaration
Variable Initialization
• Variable declaration ≠ variable initialization
• Should be initialized by a programmer before it is used
e.g.,
int count;  declaration (o), initialization(x)
char grade = ‘d’; declaration (o), initialization(o)
Constants
• Data values that cannot be changed during program execution
• E.g.,
• 3.141592
• ‘d’
• “Hello word”
• ‘\0’
To Remember
• 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.
The char Data Type
• To the computer, a character is any value that can be represented in the
computer’s alphabets
• Most computers use American Standard Code for Information Interchange
(ASCII) as computer alphabets
• A character requires one byte for the representations (28=256 characters,
extended ASCII table shown next slide)
• Characters are stored in memory as numbers (ASCII number)

Valid character literals (ASCII values) Invalid character literals


‘A’=65,‘B’=66,‘C’=67, … “a” a
‘a’=97,‘b’=98,‘c’=99, … ‘ab’ ‘a11111’
‘0’=48,‘1’=49,‘2’=50, … Why ?
The string Type
• The data type string is a programmer-defined type and is not part of the C++
language. The C++ standard library supplies it.
• A string is a sequence of zero or more characters.
• Strings in C++ are enclosed in double quote marks.
• A string with no characters is called a null or empty string.

Null or empty string

Valid string constants Invalid string constants


“” ‘ mary’ Use single quotes
“mary” “ hgshd hfd h” jsdjasdhajdk” for character
constants and
“Hello world\n” Double quotes for
“How are you John?” How to fix the problem? strings
“I have #1 pc costs 15$”
“……………….”
“\n\t\r\r\r\t\thi *&^%$%$###”
Conclusion
In this lecture …
• What is structure of C++?
• What is variable and Identifier?
• What Is Standard Data Types?

ANY QUERY ?

You might also like