Chapter 1
Chapter 1
By: Agerie B.
2015 E.C
11/29/2022 1
Introduction to programming
• A Computer is an electronic device that accepts data, performs
computations (process the input), and makes logical decisions according
to instructions that have been given to it; then produces meaningful
information in a form that is useful to the user.
11/29/2022 2
Cont.…
• In current world we live in,
– Computers are almost used in all walks of life for
different purposes.
– Computers have been deployed to solve different real
life problems, from the simplest game playing up to
the complex nuclear energy production.
– Computers are important and widely used in our society
• Because they are used to solve problem in business,
government, industry, education, etc.
11/29/2022 3
Cont…
• In order to solve a given problem, computers must be given the correct
instruction about how they can solve it.
• What is Computer Program?
– Computer programs, software programs, or programs are the
instructions that tells the computer what to do, when to do & how to do
a given task.
– Computer requires programs to function, and a computer programs
does nothing unless its instructions are executed by a CPU.
• What is Computer Programming or coding?
– Computer programming (programming or coding) is the process of
writing, testing, debugging or troubleshooting, and maintaining the source
code of computer programs.
11/29/2022 4
Cont…
• Writing computer programs means writing instructions, that will make the
computer follow and run a program based on those instructions.
• A computer program usually consists of two elements:
Data – characteristics
Code – action
• What is a Computer Programmer or Programmer ?
– Computer programs (source code) is written by professionals known as
Computer Programmers.
– Source code is written in one of programming languages.
• Example: C, C++, Java, VB.Net…. etc.
2. Semantics rules :
• It determine the meaning of the instructions (what the computer will do).
11/29/2022 6
Types of Programming languages
1. Machine language
– Computers only understand one language and that is binary language or the language
of 1s and 0s.
– Binary language is also known as machine language,
– It is one of low-level languages.
– In the initial years of computer programming, all the instructions were given
in binary form
– Example :
– Letter “A” is not known to the computer directly.
11/29/2022 7
Conti….
2. Assembly Language
It uses symbolic instructions and executable machine codes
It was created to use letters (called mnemonics)
Example
• ADD A, B – adds two numbers in memory location A and B
• SUB A, B
• LDA 4151 – Load the value to Accumulator
• MOV A,B
Assembly language is a symbolic representation of machine code
Which allows symbolic designation of memory locations
It still cannot be understood by Computers.
Assembly language must be translated to machine code by a separate program called
Assembler
The machine instruction created by the assembler from the original program (source code)
is called object code.
Source code Object code
11/29/2022 8
Conti…
3. High-level languages
• The programs written in alphabets, number and some special characters are also known as High-Level
Language Programs.
11/29/2022 9
Conti…
• Compiler: - is a program that translates a high level language into machine code. (Pascal, Fortran
Cobol)
• Interpreter: - is a program that translates each instruction of high level language & executes the
instruction before translating the next instruction.
11/29/2022 10
Problem solving Techniques
11/29/2022 11
Steps involved in Problem Solving
A computer cannot solve a problem by its own.
In order to solve a problem by the computer, one has to pass though certain stages or steps.
11/29/2022 12
Algorithm
It is a set of sequential steps usually written in Ordinary Language to solve a given problem.
It can be defined as “a complete, unambiguous, finite number of logical steps for solving a specific
problem”.
It may be possible to solve a problem in more than one ways, resulting in more than one
algorithm.
The choice of various algorithms depends on the factors like reliability, accuracy and easy to
modify.
11/29/2022 13
Cont….
• Flow chart
It is a step by step diagrammatic representation of the logic paths to solve a given problem.
It is used to solve a given problem and help a great deal to analyze the problem and plan its solution in a
systematic and orderly manner.
11/29/2022 14
Symbols used in Flow-Charts
11/29/2022 15
Example
• Draw flow chart of an algorithm to add two numbers and display
their result.
• Read the rules of the two numbers (A and B)
• Add A and B
• Assign the sum of A and B to C
• Display the result ( c)
11/29/2022 16
Cont…..
11/29/2022 17
Exercise
• Write an algorithm description and draw a flow chart to check
a number is negative or not.
11/29/2022 18
Basic Elements
• Keywords (reserved words)
Reserved/Key words have a unique meaning within a C++ program.
Reserved words, must not be used for any other purposes.
All reserved words are in lower-case letters.
The following are some of the reserved words of C++.
11/29/2022 19
Keywords…..
11/29/2022 20
Identifiers
Refers to name given to entities such as variables, functions, structures
etc.
It must be unique.
They are created to give a unique name to an entity to identify it during the
execution of the program.
Example:
int money;
Double accountBalance;
From the above example Identifiers are money and accountBalance.
11/29/2022 21
Rules for naming identifiers
1. A valid identifier can have letters (both uppercase and lowercase letters), digits and
underscores.
2. The first letter of an identifier should be either a letter or an underscore.
3. You cannot use keywords like int, while etc. as identifiers.
4. There is no rule on how long an identifier can be. However, you may run into problems
in some compilers if the identifier is longer than 31 characters.
11/29/2022 22
Literals
• Literals are data used for representing fixed values.
For Example:
• The number 129.005, the character ‘A’ and the string “hello world” are all
literals.
11/29/2022 23
Comments
• A comment is a piece of descriptive text which explains some aspect of a
program.
• Program comments are totally ignored by the compiler and are only
intended for human readers.
• C++ provides two types of comment delimiters:
• Anything after // (until the end of the line on which it appears) is considered
a comment.
• Anything enclosed by the pair /* and */ is considered a comment.
11/29/2022 24
Data Types, Variables, and Constants
Variables
• In programming, a variable is a container (storage area) to hold data.
• A variable is a symbolic name for a memory location in which data can
be stored and subsequently recalled.
• All variables have two important attributes:
– A type
• Once defined, the type of a C++ variable cannot be changed.
– A value
• The kind of values a variable can assume depends on its type.
11/29/2022 25
Variable Declaration
• A variable declaration is when you specify a type and an identifier, but
have not yet assigned a value to the variable.
Example
int myAge;
11/29/2022 26
Variable Definition
A variable definition is when you assign a value to a variable, typically with
the assignment operator =.
Syntax
type variableName = value;
Example
int Width;
Width = 5;
You can create more than one variable of the same type in one statement by
writing the type and then the variable names, separated by commas.
int myAge, myWeight; // two int variables
long area, width, length; // three longs
11/29/2022 27
Rules for naming a variable
•A variable name can only have alphabets, numbers, and the underscore _.
•A variable name cannot begin with a number.
•It is a preferred practice to begin variable names with a lowercase character.
•For example, name is preferable to Name.
•A variable name can start with an underscore. However, it's not considered a
good practice.
11/29/2022 28
Basic Data Types
When you define a variable in C++, you must tell the compiler what kind of
variable it is:
– This information tells the compiler how much room to set aside and what
kind of value you want to store in your variable.
The data types supported by C++ can be classified as
– basic (fundamental) data types
– user defined data types
– derived data types
– empty data types.
11/29/2022 29
Basic Data Types…..
Basic data types can be divided into:
– numeric and
– character types
Numeric variables can further be divided into
– integer variables – hold only integers
– floating-point variables – hold real numbers
Both the numeric data types offer modifiers that are used to vary the nature of the data to
be stored.
– short
– long
– signed
– unsigned.
11/29/2022 30
C++ data types and their ranges
Type Size Values
unsigned short int 2 bytes 0 to 65,535
short int(signed short int) 2 bytes -32,768 to 32,767
unsigned long int 4 bytes 0 to 4,294,967,295
long int(signed long int) 4 bytes -2,147,483,648 to 2,147,483,647
int 2 bytes -32,768 to 32,767
unsigned int 2 bytes 0 to 65,535
signed int 2 bytes -32,768 to 32,767
char 1 byte 256 character values
float 4 bytes 3.4e-38 to 3.4e38
double 8 bytes 1.7e-308 to 1.7e308
long double 10 bytes 1.2e-4932 to 1.2e4932
11/29/2022 31
Signed and Unsigned
• Integer types come in two varieties: signed and unsigned.
• Integers (short and long) without the word "unsigned" are assumed to
be signed.
11/29/2022 32
Example:
#include <iostream.h>
int main() {
unsigned short int Width = 5, Length;
Length = 10; Output:
/* create an unsigned short and initialize with result
Width:5
of multiplying Width by Length */
Length: 10
Area: 50
unsigned short int Area = Width * Length;
11/29/2022 34
Constants
• C++ introduces the concept of a named constant that is just like a variable, except
that its value cannot be changed.
• The qualifier const tells the compiler that a name represents a constant. Any data
type, built-in or user-defined, may be defined as const.
• If you define something as const and then attempt to modify it, the compiler will
generate an error.
• Example:
const float PI = 3.1416;
const double SALESTAX = 0.05;
const int MAXNUM = 100;
• Once declared, a constant can be used in any C++ statement in place of the number
it represents.
11/29/2022 35
Operators
• C++ provides operators for composing expressions
– arithmetic
– relational
– logical
– bitwise
– conditional
• It also provides operators which produce useful side-effects
– assignment
– increment
– decrement
11/29/2022 36
Assignment Operators
• The assignment operator is used for storing a value at some memory
location (typically denoted by a variable).
• Its left operand should be an lvalue (left value), and its right operand may
be an arbitrary expression.
– The latter is evaluated and the outcome is stored in the location denoted
by the lvalue.
– An lvalue is anything that denotes a memory location in which a value
may be stored.
• The assignment operator has a number of variants
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
11/29/2022 37
Arithmetic Operators
• C++ provides five basic arithmetic operators.
– Addition
– Subtraction
– Multiplication
– Division
– Remainder
• Except for remainder (%) all other arithmetic operators can accept a mix of integer and
real operands.
– Generally, if both operands are integers then the result will be an integer. However, if
one or both of the operands are reals then the result will be a real (or double to be
exact).
• The remainder operator (%) expects integers for both of its operands.
– It returns the remainder of integer-dividing the operands.
11/29/2022 38
Arithmetic …
• It is possible for the outcome of an arithmetic operation to be too large for
storing in a designated variable.
• This situation is called an overflow.
• The outcome of an overflow is machine-dependent and therefore undefined.
For example: unsigned char k = 10 * 92; // overflow: 920 > 255
11/29/2022 39
Relational Operators
• C++ provides six relational operators for comparing numeric quantities.
• Relational operators evaluate to 1 (true outcome) or 0 (false outcome).
– Equality ==
– Inequality !=
– Less Than <
– Less Than or Equal <=
– Greater Than >
– Greater Than or Equal >=
11/29/2022 40
Relational …
• The operands of a relational operator must evaluate to a number.
• Characters are valid operands since they are represented by
numeric values.
'A' < 'F' // gives 1 (is like 65 < 70)
• The relational operators should not be used for comparing strings,
because this will result in the string addresses being compared,
not the string contents.
• C++ provides library functions (e.g., strcmp) for the lexicographic
comparison of string.
11/29/2022 41
Logical Operators
11/29/2022 42
Logical …
• Logical and produces 0 if one or both of its operands evaluate to
0. Otherwise, it produces 1.
• Logical or produces 0 if both of its operands evaluate to 0.
Otherwise, it produces 1.
• valid logical expressions:
!20 // gives 0
10 && 5 // gives 1
10 || 5.5 // gives 1
10 && 0 // gives 0
11/29/2022 43
Logical….
• C++ does not have a built-in boolean type. It is customary to
use the type int for this purpose instead.
• For example:
int sorted = 0; // false
intbalanced = 1; // true
11/29/2022 44
Bitwise operators
• The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two
numbers.
– The result of AND is 1 only if both bits are 1.
• The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers.
– The result of OR is 1 if any of the two bits is 1.
• The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two
numbers.
– The result of XOR is 1 if the two bits are different.
• The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second
operand decides the number of places to shift.
• The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second
operand decides the number of places to shift.
• The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.
11/29/2022 45
Bitwise …
• Bitwise operators expect their operands to be integer quantities and treat them
as bit sequences.
– Bitwise negation is a unary operator which reverses the bits in its operands.
– Bitwise and compares the corresponding bits of its operands and produces a 1 when both
bits are 1, and 0 otherwise.
– Bitwise or compares the corresponding bits of its operands and produces a 0 when both
bits are 0, and 1 otherwise.
– Bitwise exclusive or compares the corresponding bits of its operands and produces a 0
when both bits are 1 or both bits are 0, and 1 otherwise.
– Bitwise left shift operator and bitwise right shift operator both take a bit sequence as
their left operand and a positive integer quantity n as their right operand.
• The former produces a bit sequence equal to the left operand but which has been shifted n bit
positions to the left.
• The latter produces a bit sequence equal to the left operand but which has been shifted n bit
positions to the right. Vacated bits at either end are set to 0.
11/29/2022 46
Increment/decrement Operators
• The auto increment (++) and auto decrement (--) operators provide a
convenient way of, respectively, adding and subtracting 1 from a
numeric variable.
– Auto Increment (prefix) ++a
– Auto Increment (postfix) a++
– Auto Decrement (prefix) --a
– Auto Decrement (postfix) a--
11/29/2022 47
Precedence of Operators
• The order in which operators are evaluated in an expression is significant
and is determined by precedence rules.
• These rules divide the C++ operators into a number of precedence levels.
• Operators in higher levels take precedence over operators in lower levels.
11/29/2022 48
Simple Type Conversion
• A value in any of the built-in types we have see so far can be converted
(type-cast) to any of the other types. For example:
– (int) 3.14 // converts 3.14 to an int to give 3
– (long) 3.14 // converts 3.14 to a long to give 3L
– (double) 2 // converts 2 to a double to give 2.0
– (char) 122 // converts 122 to a char whose code is 122
– (unsigned short) 3.14// gives 3 as an unsigned short
11/29/2022 49
Cont…
• Type operators are unary (i.e., take one operand) and appear inside
brackets to the left of their operand. This is called explicit type conversion.
• When the type name is just one word, an alternate notation may be used in
which the brackets appear around the operand:
– int(3.14) // same as: (int) 3.14
11/29/2022 50
Type conversion
• In some cases, C++ also performs implicit type conversion.
• This happens when values of different types are mixed in an expression.
For example:
– double d = 1; // d receives 1.0
– int i = 10.5; // i receives 10
– i = i + d; // means: i = int(double(i) + d)
11/29/2022 51