Introduction To Computer Programming: Jehangir Arshad Meo
Introduction To Computer Programming: Jehangir Arshad Meo
Programming
1
Programming Languages:
A computer program is written in a specific
manner and according to a set of rules, these
rules are called programming Language.
2
Low Level Languages:
The Languages which are easily understandable for the
machine (computer) are called Low level languages.
3
High Level Languages:
These languages are closer to human beings because
these are easily understandable by human user.
Every high level language has its own set of rules for
writing a program, these set of rules are called syntax of
the language.
4
Advantages and Disadvantages of
Low Level Languages:
Easily understandable by the computer.
5
Advantages and Disadvantages of
high Level Languages:
Machine independent, can run on other devices or
software, before implementing it on the device for
which it was written.
Memory issues.
6
Why C ++?
C++ is a language designed by and for programmers
Once mastered, C++ will give you complete control over the
computer
7
Compiler
C++
C++Program
Program C++
C++ Compiler
Compiler Machine
Machine
Language
(e.g.
(e.g. g++)
g++) Language
Program
Program
int
int main()
main() {{
int
int i=1;
i=1;
.. .. .. 01001001
01001001
10010100
10010100
If a pre-made function exists, generally best to use it rather than write your own
Library functions carefully written, efficient, and portable
9
Phases of C++ Programs:
Program is created in
Editor hard disk the editor and stored
1. Edit on hard disk.
4. Link Primary
creates a.out and
stores it on hard disk
Memory
Loader
5. Load Loader puts program
in main memory for CPU.
6. Execute hard disk ..
..
..
Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program
..
executes.
10
C Preprocessor:
C++ Compilers automatically invoke a
preprocessor that takes care of #include
statements and some other special directives.
11
Structure of C++ program:
Preprocessor Directives
Header files
Comments
Main () function of the program
Delimiters
Statement terminator
Variables (declaration, initialization)
Input/ output statements
Expressions
Etc…
12
Structure of C++ program:
Preprocessor directives
Processed by preprocessor before compiling
Begin with #
# include
# define
Header Files
It is the part of compiler and have the definitions of standard library functions
There are several header files each have function of one type: such as math.h
contains header files containing definitions of mathematical functions.
Each header file has extension (.h). The name of the file is written in angle brackets(<
>).
#include <math.h>
#Include is used to add header file to the program.
#define Identifier Constant value
It is used to assign a constant value to an identifier :a, b or any variable
13
Structure of C++ program(contd..)
Example:
#define square(a) (a * a)
y = square(x);
becomes y = (x * x);
z = square(y*x);
14
Structure of C++ program(contd..):
Comments:
Document programs
Improve program readability
Ignored by compiler
Single-line comment:
Begin with //
Multiple-line comment:
15
Structure of C++ program(contd..)
main() Function:
It indicates the start of the C++ language program.
It must be include in every C++ program.
We can say it is the entering point to the program.
Delimiters:
Symbols that are used to specify the limits of the C++ language program.
Curly brackets are used to start( { ) and Terminate ( } ) the body of the
program.
statement terminators:
Statement that is used to terminate the statement of a C++
Language program is called statement terminator.
( ; ) semicolon is used to terminate the statements.
16
Structure of C++ program(contd..):
Escape Sequences:
Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To
represent a newline character, single quotation mark, or certain other characters in a character constant, we must use escape sequences .
17
1 // code1.cpp
2 // A first program in C++. Single-line comments.
3 #include <iostream> Function main returns an
4 integer { begins Preprocessor
value.
Left brace function directive to
5 // function main begins program execution include input/output
Statements stream
end with a
body. Function main appears
6 int main() header filesemicolon
<iostream>.
exactly once in every C++ ;.
7 {
program..
8 cout << "Welcome to C++!\n";
9 Corresponding right brace }
10 return 0; // indicateends
that program
functionended
body.successfully
11 Stream insertion operator.
12 } // end function main
Keyword return is one of
Welcome to C++!
several means to exit
function; value 0 indicates
program terminated
successfully.
18
1 // code3.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4
5 // function main begins program execution Using newline characters to
6 int main() print on multiple lines.
7 {
8 std::cout << "Welcome\nto\n\nC++!\n";
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main
Welcome
to
C++!
19
Structure of C++ program(contd..):
Input/output
◦ Cin
20
A Simple Program: Printing a Line of Text
Standard output stream object
◦ std::cout
◦ “Connected” to screen
◦ <<
Stream insertion operator
Value to right (right operand) inserted into output stream
Namespace
◦ std:: specifies using name that belongs to “namespace”
std
◦ std:: removed through use of using statements
using namespace std;
Escape characters
◦ \
◦ Indicates “special” character output
21
1 // code2.cpp
2 // Printing a line with multiple statements.
3 #include <iostream>
4
5 // function main begins program execution
Multiple stream insertion
6 int main()
statements produce one line of
7 {
output.
8 std::cout << "Welcome ";
9 std::cout << "to C++!\n";
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main
Welcome to C++!
22
Another Simple Program:
Adding Two Integers
Input stream object
◦ >> (stream extraction operator)
Used with std::cin
Waits for user to input value, then press Enter (Return)
key
Stores value in variable to right of operator
Converts value to variable data type
= (assignment operator)
◦ Assigns value to variable
◦ Binary operator (two operands)
◦ Example:
sum = variable1 + variable2;
23
Variables
24
1 // code4.cpp
2 // Addition program.
3 #include <iostream>
4 // function main begins program execution
6 int main()
7 { Declare integer variables.
8 int integer1; // first number to be input by user
9 int integer2; // second number to be input by user
10 int sum; // variable in which sum will Use
be stored
stream extraction
11 operator with standard input
12 std::cout << "Enter first integer\n"; // prompt
stream to obtain user input.
13 std::cin >> integer1; // read an integer
14
Calculations can be performed in output statements: alternative for
15 std::cout << "Enter second integer\n"; // prompt
lines 18 and 20:
16 std::cin >> integer2; // read an integer
17
std::cout << "Sum is " << integer1 + integer2 << std::endl;
18 sum = integer1 + integer2; // assign result to sum Stream manipulator
19 std::endl outputs a
20 std::cout << "Sum is " << sum << std::endl; // print sum newline, then “flushes output
21 return 0; // indicate that program ended successfully buffer.”
23 } // end function main
Concatenating, chaining or
cascading stream insertion
operations.
25
For output:
cout << "Hello"; // prints Hello
cout << Hello; // prints the content of Hello variable
The insertion operator (<<) may be used more than once in a single statement:
cout << "Hello, " << "I am " << "a C++ statement";
This last statement would print the message Hello, I am a C++ statement on the screen.
The utility of repeating the insertion operator (<<) is demonstrated when we want to
print out a combination of variables and constants or more than one variable:
cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;
26
For Input:
The standard input device is usually the keyboard. Handling the standard
input in C++ is done by applying the overloaded operator of extraction (>>)
on the cin stream. The operator must be followed by the variable that will
store the data that is going to be extracted from the stream.
int age;
cin >> age;
You can also use cin to request more than one datum input from the user:
27
IMPORTANT LINK:
http://www.cplusplus.com/doc/tutorial/basic_io/
28
Memory Concepts
Variable names:
scanf(“%d”,&integer2); integer1 45
integer1 45
sum = integer1 + integer2; integer2 72
sum 117
30
Arithmetic
Arithmetic calculations
◦ *
Multiplication
◦ /
Division
Integer division truncates remainder
7 / 5 evaluates to 1
◦ %
Modulus operator returns remainder
7 % 5 evaluates to 2
31
Arithmetic
Rules of operator precedence
◦ Operators in parentheses evaluated first
Nested/embedded parentheses
Operators in innermost pair first
◦ Multiplication, division, modulus applied next
Operators applied from left to right
◦ Addition, subtraction applied last
Operators applied from left to right
Operator(s) Operation(s) Order of evaluation (precedence)
33
Decision Making: Equality and
Relational Operators
34
Confusing Equality (==) and
Assignment (=) Operators
Common error
Aspects of problem
35
Confusing Equality (==) and
Assignment (=) Operators
Example
if ( payCode == 4 )
printf("You get a bonus!“);
◦ If paycode is 4, bonus given
36
C Data Types
37
Integral Types
char, short, int, long
Different sizes of integers - different memory size
Dependent upon the compiler
Integer values: Sequence of one or more digits
22 129 -67 0
commas are not allowed: 100,000
38
Type Name Bytes Other Names Range of Values
int 4 signed –2,147,483,648 to 2,147,483,647
40
Precedence
Precedence controls the order of evaluation of
operators.
41
Precedence
Operators Precedence
() highest (applied first)
* / %
+ -
< <= > >=
== !=
= lowest (applied last)
42
const
You can add the const modifier to the declaration
of a variable to tell the compiler that the value
cannot be changed:
43
What if you try to change a const?
The compiler will complain if your code tries to
modify a const variable:
44
Why use const?
Const tells the compiler that a variable should
never be changed.
You already know the variable should never be
changed!
45
Integer vs. floating point math
How does C know whether to use floating
point or integer math operators?
46
Literals (fixed values)
Literals are fixed values used by a program.
Some examples of literals:
22 3.14159
false "Hi Kurt" 'c'
You can initialize a variable in the declaration by
assigning it a value:
int temp = 17;
double PI = 3.14159;
char alpha = ‘a';
47