0% found this document useful (0 votes)
12 views10 pages

#02 Lecture PF NUML 2024-09-17

programming fundamental

Uploaded by

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

#02 Lecture PF NUML 2024-09-17

programming fundamental

Uploaded by

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

National University of Modern Languages

Computer Science Department


Aiwan-e-Iqbal Campus Lahore

PROGRAMMING FUNDAMENTALS
Introduction
This course is designed for undergraduate students in computer science who need to learn fundamental
programming skills quickly using C++. Students with no programming background are welcome. Topics include
control structures, arrays, functions, classes, objects, file handling, and simple algorithms for common tasks.

Introduction to C++
C++ is a general-purpose programming language that was developed as an enhancement of the C language to
include object-oriented paradigm. It is an imperative and a compiled language.

The basic syntax and code structure of both C and C++ are the same.
One of the defining features of C++ is that it makes heavy use of pointers, variables which store the numerical
memory addresses of pieces of other data.
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

Some of the features & key-points to note about the programming language are as follows:
• Simple: It is a simple language in the sense that programs can be broken down into logical units
and parts, has a rich library support and a variety of data-types.
• Machine Independent but Platform Dependent: A C++ executable is not platform-independent
(compiled programs on Linux won’t run on Windows), however they are machine independent.
• Mid-level language: It is a mid-level language as we can do both systems-programming (drivers,
kernels, networking etc.) and build large-scale user applications (Media Players, Photoshop,
Game Engines etc.)
• Rich library support: Has a rich library support (Both standard ~ built-in data structures,
algorithms etc.) as well 3rd party libraries (e.g., Boost libraries) for fast and rapid development.
• Speed of execution: C++ programs excel in execution speed. Since, it is a compiled language,
and also hugely procedural. Newer languages have extra in-built default features such as
garbage-collection etc. which slow the execution of the program overall. Since there is no
additional processing overhead like this in C++, it is blazing fast.
• Pointer and direct Memory-Access: C++ provides pointer support which aids users to directly
manipulate storage address. This helps in doing low-level programming (where one might need to
have explicit control on the storage of variables).
• Object-Oriented: One of the strongest points of the language which sets it apart from C. Object-
Oriented support helps C++ to make maintainable and extensible programs. i.e., Large-scale
applications can be built. Procedural code becomes difficult to maintain as code-size grows.
• Compiled Language: C++ is a compiled language, contributing to its speed.
Some interesting facts about C++:
1. The name of C++ signifies the evolutionary nature of the changes from C. “++” is the C increment
operator.
2. C++ is one of the predominant languages for the development of all kind of technical and
commercial software.
3. C++ introduces Object-Oriented Programming, not present in C. Like other things, C++ supports
the four primary features of OOP: encapsulation, polymorphism, abstraction, and inheritance.
4. C++ got the OOP features from Simula67 Programming language.
5. A function is a minimum requirement for a C++ program to run. (at least main() function)
Advantages of using C++
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

Character set White space characters

ASCII and Unicode


Two standard character sets are ASCII (American Standard Code for Information Interchange) and
Unicode.

ASCII
The ASCII character set is a 7-bit set of codes that allows 128 different characters. That is enough for every
upper-case letter, lower-case letter, digit and punctuation mark on most keyboards. ASCII is only used for the
English language.
This table shows some examples of letters represented using the ASCII character set:
Character Denary value Binary value Hex
N 78 1001110 4E
O 79 1001111 4F
P 80 1010000 50
Q 81 1010001 51
R 82 1010010 52
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

NON PRINTABLE CHARACTERS


DEC HEX CHARACTER (CODE) DEC HEX CHARACTER (CODE)
0 0 NULL 16 10 DATA LINK ESCAPE (DLE)
1 1 START OF HEADING (SOH) 17 11 DEVICE CONTROL 1 (DC1)
2 2 START OF TEXT (STX) 18 12 DEVICE CONTROL 2 (DC2)
3 3 END OF TEXT (ETX) 19 13 DEVICE CONTROL 3 (DC3)
4 4 END OF TRANSMISSION (EOT) 20 14 DEVICE CONTROL 4 (DC4)
5 5 END OF QUERY (ENQ) 21 15 NEGATIVE ACKNOWLEDGEMENT (NAK)
6 6 ACKNOWLEDGE (ACK) 22 16 SYNCHRONIZE (SYN)
7 7 BEEP (BEL) 23 17 END OF TRANSMISSION BLOCK (ETB)
8 8 BACKSPACE (BS) 24 18 CANCEL (CAN)
9 9 HORIZONTAL TAB (HT) 25 19 END OF MEDIUM (EM)
10 A LINE FEED (LF) 26 1A SUBSTITUTE (SUB)
11 B VERTICAL TAB (VT) 27 1B ESCAPE (ESC)
12 C FF (FORM FEED) 28 1C FILE SEPARATOR (FS) RIGHT ARROW
13 D CR (CARRIAGE RETURN) 29 1D GROUP SEPARATOR (GS) LEFT ARROW
14 E SO (SHIFT OUT) 30 1E RECORD SEPARATOR (RS) UP ARROW
15 F SI (SHIFT IN) 31 1F UNIT SEPARATOR (US) DOWN ARROW

What are escape sequences used for C++?


The escape sequences are special non-printing characters that are used to control the printing
behavior of the output stream objects (such as 'cout'). These characters are not displayed in the
output. An escape sequence is prefixed with a backslash () and a coded character is used to control
the printing behavior.
The following escape sequences are available:

Escape
Description Representation
sequence
Simple escape sequences
\' single quote byte 0x27 in ASCII encoding
\" double quote byte 0x22 in ASCII encoding
\? question mark byte 0x3f in ASCII encoding
\\ backslash byte 0x5c in ASCII encoding
\a audible bell byte 0x07 in ASCII encoding
\b backspace byte 0x08 in ASCII encoding
\f form feed - new page byte 0x0c in ASCII encoding
\n line feed - new line byte 0x0a in ASCII encoding
\r carriage return byte 0x0d in ASCII encoding
\t horizontal tab byte 0x09 in ASCII encoding
\v vertical tab byte 0x0b in ASCII encoding
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

PRINTABLE CHARACTERS
DEC HEX CHARACTER DEC HEX CHARACTER DEC HEX CHARACTER
32 0x20 <SPACE> 64 0x40 @ 96 0x60 `
33 0x21 ! 65 0x41 A 97 0x61 a
34 0x22 " 66 0x42 B 98 0x62 b
35 0x23 # 67 0x43 C 99 0x63 c
36 0x24 $ 68 0x44 D 100 0x64 d
37 0x25 % 69 0x45 E 101 0x65 e
38 0x26 & 70 0x46 F 102 0x66 f
39 0x27 ' 71 0x47 G 103 0x67 g
40 0x28 ( 72 0x48 H 104 0x68 h
41 0x29 ) 73 0x49 I 105 0x69 i
42 0x2A * 74 0x4A J 106 0x6A j
43 0x2B + 75 0x4B K 107 0x6B k
44 0x2C , 76 0x4C L 108 0x6C l
45 0x2D - 77 0x4D M 109 0x6D m
46 0x2E . 78 0x4E N 110 0x6E n
47 0x2F / 79 0x4F O 111 0x6F o
48 0x30 0 80 0x50 P 112 0x70 p
49 0x31 1 81 0x51 Q 113 0x71 q
50 0x32 2 82 0x52 R 114 0x72 r
51 0x33 3 83 0x53 S 115 0x73 s
52 0x34 4 84 0x54 T 116 0x74 t
53 0x35 5 85 0x55 U 117 0x75 u
54 0x36 6 86 0x56 V 118 0x76 v
55 0x37 7 87 0x57 W 119 0x77 w
56 0x38 8 88 0x58 X 120 0x78 x
57 0x39 9 89 0x59 Y 121 0x79 y
58 0x3A : 90 0x5A Z 122 0x7A z
59 0x3B ; 91 0x5B [ 123 0x7B {
60 0x3C < 92 0x5C \ 124 0x7C |
61 0x3D = 93 0x5D ] 125 0x7D }
62 0x3E > 94 0x5E ^ 126 0x7E ~
63 0x3F ? 95 0x5F _ 127 0x7F <DEL>
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

Naming rules for Identifiers in C++

All C++ variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and maintainable code:

Variables
An entity whose value changes over time within a program

Constants
An entity whose value does NOT change over time within a program

The general rules for naming variables are:

• Names can contain letters, digits and underscores


• Names must begin with a letter or an underscore (_)
• Names are case sensitive (myVar and myvar are different variables)
• Names cannot contain whitespaces or special characters like !, #, %, etc.
• Reserved words (like C++ keywords, such as int) cannot be used as names
• Identifiers should use CamelCase, with the first letter always capitalized
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

Data types

Data types in C++ is mainly divided into three types:

1. Primitive Data Types: These data types are built-in or predefined data types and can be used
directly by the user to declare variables. example: int, char, float, bool etc. Primitive data types
available in C++ are:
• Integer
• Character
• Boolean
• Floating Point
• Double Floating Point
• Valueless or Void
• Wide Character
2. Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are
referred to as Derived Data Types. These can be of four types namely:
• Function
• Array
• Pointer
• Reference
3. Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining
a class in C++ or a structure. C++ provides the following user-defined datatypes:
• Class
• Structure
• Union
• Enumeration
• Typedef defined Datatype
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

The primitive data types and their size available in C++ are:

• Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory
space and ranges from -2147483648 to 2147483647.

• Character: Character data type is used for storing characters. Keyword used for character data
type is char. Characters typically requires 1 byte of memory space and ranges from -128 to 127
or 0 to 255.

• Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean variable can
store either true or false. Keyword used for Boolean data type is bool.

• Floating Point: Floating Point data type is used for storing single precision floating point values
or decimal values. Keyword used for floating point data type is float. Float variables typically
requires 4 byte of memory space.

• Double Floating Point: Double Floating Point data type is used for storing double precision
floating point values or decimal values. Keyword used for double floating point data type
is double. Double variables typically require 8 byte of memory space.

• void: Void means without any value. void datatype represents a valueless entity. Void data type
is used for those functions that do not return a value.

Some more data types supported by C++ are


• short
• long
• unsigned char
• unsigned short
• unsigned int
• unsigned long

Types of C/C++ Instructions

There are basically three types of C instructions.


• Type Declaration Instructions
• Arithmetic Instructions
• Control Instructions

Type Declaration Instructions


These instructions inform the compiler about the type of variables used. That means, whenever a variable is
used in the program, we have to specify what types of data it can hold – like integer, float, double, character
etc. This makes the compiler to store only those specific types of values in it.

For example, a variable intVar1 declared as int will accept and store only integer values. It will never accept
float or character values.
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

Declaration of variable is done at the beginning, inside a function. The “main()” function is the starting point of
any C program and all the variables used with main function needs to be declared at the beginning of it, before
using it in the function.

There can be many other functions in the program and they can have variables. These also need to be
declared at the beginning of respective functions.

void main() {
int intVar1, intVar2, intSum;
float flAvg;
char chrArr [10];
….
}

Arithmetic Instructions
These instructions are used to perform some arithmetic calculation within the program. It uses arithmetic
operators like +, -, X, /, %, =, ++, –, +=, -= etc.

The variables that participate in the arithmetic operations are termed as operands. These instructions can have
any simple to complex arithmetic calculations using these symbols.

sum = var1+var2+var3;
result = var1+var2/var3;
result = (var1+var2)/var3;
result = (var1*100) + var2 - var3;
result = a+5*30+sum/ 20

Control Instructions
these instructions are used to control the flow of the program execution. They maintain certain order in which
program needs to be executed. This order of execution may be based on certain conditions or may be based
on certain values – may be input values or some result values.

Some examples of Control instructions are


Sequence Control Instructions
These instructions are responsible for executing the instructions one after the other, as they appear in
the program. There are not any checks on the condition or values to control the execution.
National University of Modern Languages
Computer Science Department
Aiwan-e-Iqbal Campus Lahore

For example, a normal program in which all the arithmetic operation like addition, subtraction,
multiplication and division is carried out one after the other. Here there is no check on user option, or
any other conditions. It will display results of all the operations in the order they appear in the program.

Decision / Selection Control Instructions


This will have set of conditions to execute the instructions within it. If the condition is true, then it will
execute the instructions, else it will execute other set of instructions. This type of instructions use if or
while statements to make the decision.
For example, suppose user enters the options like A, S, M or D to indicate addition, subtraction,
multiplication and division. Depending on the option entered by the user, the program may add,
subtract, multiply or divide the numbers. Here it will not execute all the operation but only one of them
entered by the user.

Loop Control Instructions


There will be certain need for executing a set of instruction for certain number of times. This is done by
using loop control statement. These loops will have limited number of iterations or will have certain
conditions which in turn will give iterations for the instructions to be executed. These instructions will
use for loops or while or do/while statements to get the loop iterations.

For example, suppose we need to display the first 5 entries of name in the array. This will use for loop
to iterate through the array from its beginning till 5th entry to display the names in it.

Similarly, suppose we need to display all names in sorted array where average marks are not more
than 35%. This will use do/while loop to iterate through the array (here we do not know the exact
number of iterations) until the condition is matched.

You might also like