Pro 1
Pro 1
Overview of programming
2
Introduction
o Computer is an electronic device that accepts data, performs
computations and makes logical decisions according to instructions that
have been given to it;
3
o Computer programs, software programs, or just programs are the
o Code – action
o Data – characteristics
semantics.
5
Cont…
For example:
#include <iostream.h>
int main() {
cout << "Hello Ethiopia!\n“;
return 0;
}
6
Levels of Programming Language
o A main purpose of programming languages is to provide instructions to a
computer.
High-level languages
o There are 3 types of programming language:
Machine language
Assembly language
High level language
7
Machine Languages
o Computers only understand one language and that is binary language or the
Advantage:
Limitation:
It is machine dependent
It is difficult to program
It is error prone
8 It is difficult to modify
Assembly Language
o It correspondences symbolic instructions and executable machine codes
9
Cont…
o Advantages Assembly Machine
Easier to modify
o Limitations
Machine dependent
computer's hardware.
They are symbolic languages that use English words and/or mathematical
11
Cont…
o Advantage
Machine independence
Fewer errors
Easier to maintain
o Limitation
Lack of flexibility
12
Cont…
o High-level languages also require translation to machine language
before execution.
o This translation is accomplished by either a compiler or an
interpreter.
Compilers translate the entire source code program before execution.
13
The Compiler vs Interpreter
Compiler
o Can diagnose the following kinds of errors
Illegal characters
Illegal combination of characters
Improper sequencing of instructions in a program
o But it can‟t diagnose logical errors .
15
Cont…
We use the Integrated Development Environment (IDE) of C++, like Turbo C++
allocation
new features.
o Object Oriented: C++ is object oriented programming language.
o Compiler based: C++ is a compiler based programming language
17
C++ Basic Elements
o A token is the smallest independent unit of meaning in a program, as
Identifiers
Variables
Keywords Constants
Literals Operators
o Tokens should be separated by white space to make the source code more
readable.
o White space includes blanks, horizontal and vertical tabs, new lines, form
o These symbols, the reserved words, must not be used for any other
purposes.
o All reserved words are in lower-case letters.
19
o
20
Identifier
string.
o For example the number 129.005, the character „A‟ and the string “hello
Comments
o A comment is a piece of descriptive text which explains some aspect of
a program.
o Program comments are totally ignored by the compiler and are only
22
Basic Data Types
o When you define a variable in C++, you must tell the compiler what
– This information tells the compiler how much room to set aside
23
o The data types supported by C++ can be classified as basic
o Both the numeric data types offer modifiers that are used to vary the
24
C++ Data Types and Ranges
25
Variables
o A variable is a symbolic name for a memory location in which data can
o Variables are used for holding data values so that they can be utilized in
– Value: The kind of values a variable can assume depends on its type.
o Variables in C++ are given names by the programmer, the way he/she
o Literal constants
These are values typed directly into the program when they are needed
There are also string constants which use double quotation marks.
1-27
Symbolic Constants
C++ introduces the concept of a named constant that is just like a
constant.
Any data type, built-in or user-defined, may be defined as const.
1-28
Operators
o Operators are symbols that tells the compiler to perform specific
Assignment
Arithmetic
Relational
Logical
29
Increment/ decrement operators
Assignment Operators (=)
o The assignment operator is used for storing a value at some memory
o 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.
o The assignment operator has a number of variants
31
Arithmetic Operators
o C++ provides five basic arithmetic operators.
o Except for remainder (%) all other arithmetic operators can accept a mix
integer. However, if one or both of the operands are real's then the result
o The remainder operator (%) expects integers for both of its operands.
33
Relational Operators
o C++ provides six relational operators for comparing numeric quantities.
34
Logical Operators
o C++ provides three logical operators for combining logical expression.
o Logical negation is a unary operator, which negates the logical value of its
single operand.
– If its operand is nonzero it produces 0, and if it is 0 it produces 1.
35
o Increment/Decrement Operators
o The auto increment (++) and auto decrement (--) operators provide
o When used in prefix form, the operator is first applied and the
o When used in the postfix form, the expression is evaluated first and
36
Escape operators
o The backslash causes an “ escape ” from the normal way characters are
interpreted and there are some basic characters that we use in escape sequence
i.e. preceding them with a backslash.
o Escape sequences are usually used with string constants that are sent to the cout.
37
Examples:
38
Precedence of Operators
o These rules divide the C++ operators into a number of precedence levels.
around it.
39
40
Expressions and Statements
o Expression is any statement or part of a statement that evaluates to a value
constants and variables, and punctuators, which specify a computation as per the
rules of the underlying language
o Expressions are evaluated based on the operators they contain and the context in
o C++ provides two useful operators for this purpose: >> for input and
<< for output. Example
#include <iostream.h>
int main() {
int a, b, sum;
cout << “Enter number a:” << endl;
cin>>a;
cout << “Enter number b: “ << endl;
cin>>b;
sum = a+b;
cout << “sum is: “ << sum << endl;
42
return 0; }
Control statements
o Flow control in a program is sequential, from one
43
condition.
Cont..
o Loop statements are used for specifying computations, which
44
Conditional Statements
o Selection statements are used for specifying alternate paths of execution,
o The following statements are used to perform the task of the conditional
operations.
if statement
If-else statement
else-if statement
Switch statement
45
if statement
o The if statement is used to express conditional expressions.
if (conditional expression) {
statement-1;
statement-2;
……… STATEMENT-N
o The expression is evaluated and if the expression is true the statements will be executed.
o If the expression is false the statements are skipped and execution continues with the
next statements.
46
Example: write the program To check percentage of a student and display pass if it is greater than
50.
#include <iostream.h>
int main() {
float percent;
cin>>percent;
if (percent>=50) {
return 0;
}
47
if - else statement
o It is a two way branching statement.
o It is similar to if statement but the only difference is if the condition is false then
if (condition)
{
statements;
... ... ...
}
else
{
statements;
... ... ...
48 }
Cont..
//write the program to Checking even/odd numbers
#include<iostream.h>
int main () {
int n;
cout<<"Enter a Number: " ;
cin>>n;
if (n%2= =0)
{
cout<<n<<" is Even!"; }
else
{
cout<<n<<" is Odd!"; }
return 0;
}
49
else-if statements
o It used when there are two or more conditions that needs to be checked to
51
example
52
Nested if-else statement
o It is possible to nest if-else statements, one within another.
o There are several different form that nested if-else statements can
take.
statements.
53
Cont…
54
Cont..
55
//Demo to check even no. and divisible by 5 else
#include <iostream.h> {
int main() { if (n%5 == 0)
int n;
{
cout<<"Enter a number: ";
cout<<"Number is not even
cin>>n; but divisible by 5";
if (n%2 == 0) }
{ else
if (n%5 == 0)
{
{
cout<<"Number is not even
cout<<"Number is even and divisible by 5"; and not divisible by 5";
} }
else { }
cout<<"Number is even but not divisible by 5";
return 0;
}
}
}
56
switch statement
o The switch statement provides a way of choosing between a set of
to each of the numeric constants (called case labels), in the order they appear, until
a match is found.
o The final default case is optional and is exercised if none of the earlier cases
provide a match.
59
60
61
Looping Statements
o Looping is a way of repeating a series of instructions several times
to execute a block of statements.
62
Cont..
A program loop consists of two statements:
1. Body of the loop
2. Control statements
The control statement tests certain conditions and then directs the repeated
execution of the statements contained in the body of the loop.
63
Cont..
Depending on the position of the control statement in the loop, a control
structure can be either as the entry-controlled loop or as exit-controlled loop.
In entry-controlled loop, the control conditions are tested before the start of
the loop execution.
In exit-controlled loop, the test is performed at the end of the body of the loop
and therefore the body is executed unconditionally for the first time.
1. while loops
2. do …while loops
3. for loops
64
While Statement
o The while statement (also called while loop) provides a way of
while (expression)
statement;
i = 1;
sum = 0;
while (i <= n)
{
sum += i;
i++;}
The while loop is top tested i.e., it evaluates the
condition before executing statements in the body.
66
Then it is called entry control loop.
//Adding whole numbers from 0 -20
#include<iostream.h>
int main()
{
int i=0;
int sum=0;
while(i<=10)
{
sum+=i;
i++;
}
cout<<"\n\t The Sum is "<<sum;
67 return 0;
for Statement
o The for statement (for loop) is similar to the while statement,
statement;
expression1;
while (expression2)
statement;
expression3;
}
69
o The general form of the for statement is:
70
Cont..
o The most common use of for loops is for situations where a variable is
o The following for loop, for example, calculates the sum of all integers from
1 to n.
sum = 0;
for (i = 1; i <= n; ++i)
sum += i;
72
// Demo add the first 10 natural numbers
#include<iostream.h>
int main()
{
int sum = 0;
for (int i=1; i<=10; i++)
{
sum += i;
}
cout<<"the sum is "<<sum<<endl;
return 0;
}
73
do…while Statement
o The do statement is similar to the while statement, except that its body
do
statement;
while (expression);
o First statement is executed and then expression is evaluated.
74
o For example, suppose we wish to repeatedly read a value and print its square,
do {
cin >> n;
cout << n * n << '\n';
}
while (n != 0);
o Unlike the while loop, the do loop is never used in situations where it would
o Nesting can be defined as the method of embedding one control structure with
o While making control structures to be reside one with in another, the inner and
outer control structures may be of the same type or may not be of same type.
78
//what is the output of the following program?
#include<iostream.h>
int main()
{
int i;
int j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<i;
}
cout<<endl;
}
return 0;
}
79
Jumping Statements
o Jump statements are used to divert the execution path to another part of the
program.
o The followings are the types of jumping statement used in C++ program.
1. Break statements
2. Continue statements
3. goto statements
4. Return statements
80
Break Statement
o Using break we can leave a loop even if the condition for its end
is not fulfilled.
structure.
Commonly used to escape early from a loop, or skip the
program.
goto label;
where label is an identifier which marks the jump destination
of goto.
#include<iostream.h>
void main() {
last:
return 0;
85
}
// goto loop example
#include <iostream.h>
int main ()
{
Output
int n=10;
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,FIRE!
loop:
cout << n << ", ";
n--;
if (n>0)
goto loop;
cout << "FIRE!\n";
return 0;
86
}
return Statement
o The return statement enables a function to return a value to its caller.
o The general form/syntax of return statement is:
return expression;
where expression denotes the value returned by the function.
o The type of this value should match the return type of the function.
o For a function whose return type is void, expression should be empty.
return;
o The main function may have return type int.
o The return value of main is what the program returns to the operating system when it
completes its execution.
For example:
#include<iostream.h>
int main (void)
{
cout << "Hello World\n";
return 0;
}
87
88