SlideShare a Scribd company logo
Debre Tabor University
Department of Mathematics
Loop control statments
2016
By: Wudneh Tilahun Mengist
Out lines of the presentation
Introduction
Iteration/About loop
The for loop
The While loop
The Do…While loop
Nested Loop
User defined functions
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
INTRODUCTION
Generally a program executes its statements from beginning to
the end, but all programs don't follow this. This presentation will
tell us in detail about the program control statements.
STATEMENTS
Statements are the instructions given to the computer to perform
any kind of data movements, be it making decisions or repeating
actions. They form the smallest executable unit of the program.
They are terminated by a semicolon(;). The different types of
statements are:-
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
STATEMENT FLOW CONTROL
In a program, statements may be executed sequentially , selectively
or iteratively:-
Sequence:- This is the default flow of the statements and also
known as sequence construct and it also means that
the statements are being executed sequentially.
Selection:- The selection construct means that the execution of
the statements depends on a condition-test, if the
condition is true then the statements would be
executed else they would be skipped. An else-if
statement is a good example of selection statement.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Iteration:-  The iteration means repetition of a certain set
of statements based on a certain condition-test.
Looping statements can be used to perform the
repetition of a certain set of statements.
 Iteration statements in c++ allows a certain
number of instructions to be performed
repeatedly until a certain condition becomes
false or for a fixed number of times.
 Iteration statements are also called loops or
looping statements.
There are three C++ repetition statements:
 The For Statement or loop
 The While statement or loop
 The do…while statement or loopPrepared by: Wudneh Tilahun, Debre Tabor
University-2016
Elemnts that control a loop
Every loop has its elements that control and govern the execution.
Generally, a loop has 4 elements that have different purposes. These
elements are as given below:-
1. Initialization Expression(s):
Before entering the loop the initialization expression(s) initializes
the loop control variables with their first values.
2. Test Expression:
The test expression is that expression whosetruth value decides
whether the loop-body will be executed or not. If the value is true
then the body gets executed else it gets terminated.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
3. Update expression(s): The update expression(s) change the
value(s) of loop variable(s). The update expression execute at the
end of the loop.
4. The Body-of-the-loop: The statements that are to be
executed form the body-of-the-loop.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
THE FOR LOOP
The for loop is the easiest to understand of all because all its loop-
control elements are gathered in one place i.e. at the top. Its syntax is:-
For(initialization expression(s);test—expression; update expression)
body-of-the-loop;
Example of for loop
#include<iostream.h>
int main()
{
int i;
for(i=1;i<=10;i++)
cout<<“n”<<i;
return 0;
}
Condition
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
 Firstly, initialization expression is executed i.e., i=1 which
gives the first value 1 to variable i.
 Then, the test-expression is executed i.e., i<=10 which results
into true i.e., 1.
 Since, the test expression is true, the body-of-the-loop i.e.,
cout<<“n”<<i is executed which prints the current value of i
on the next line.
 After executing the loop-body, the update expression i.e., i++
is executed which increments the value of i.
 After the update expression is executed, the test-expression is
again evaluated. If it is true the sequence is repeated from
step no. 3, otherwise the loop terminates.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
ForExpr
Action
true false
ForInit
PostExpr
Evaluated once
at the beginning
of the for
statements's
execution
The ForExpr is
evaluated at the
start of each
iteration of the
loop
If ForExpr is
true, Action is
executed
After the Action
has completed,
the
PostExpression
is evaluated
If ForExpr is
false, program
execution
continues with
next statement
After evaluating the
PostExpression, the next
iteration of the loop starts
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i 0
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i 0
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i 0
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i 0
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i 1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i 1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i 1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i 1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i 2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i 2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i is 2
i 2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i is 2
i 2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i is 2
i 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i is 2
i 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example
for (int i = 0; i < 3; i++)
{
cout << "i is " << i << endl;
}
cout << "all done" << endl;
i is 0
i is 1
i is 2
all done
i 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Let us see how it’s work
#include<iostream.h>
int main()
{
return 0;
}
Example:- write a c++ program which evaluate
the sum of the first +ve n integer.
Alterna
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Let us see how it’s work
#include<iostream.h>
int main()
{
int n, i=1;
cout << "Enter a positive integer : ";
cin >> n;
for(long sum=0; i <= n;i++)
sum += i;
cout << "The sum of the first " << n << " integers is " << sum;
return 0;
}
Example:- write a c++ program which evaluate
the sum of the first +ve n integer.
Alterna
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i --
sum 0
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 1
sum 0
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 1
sum 0
1<=10 is True
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 1
sum 0
Sum=sum+i=0+1=1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 1
sum 1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 1
sum 1
i=i+1=1+1=2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 2
sum 1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 2
sum 1
2<=10 is True
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 2
sum 1
Sum=sum+i=1+2=3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 2
sum 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 2
sum 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 3
sum 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 3
sum 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 3
sum 3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 3
sum 6
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 3
sum 6
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 4
sum 6
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 4
sum 6
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 4
sum 6
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 4
sum 10
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 4
sum 10
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 5
sum 10
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 5
sum 10
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 5
sum 10
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 5
sum 15
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 5
sum 15
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 6
sum 15
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 6
sum 15
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 6
sum 15
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 6
sum 21
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 6
sum 21
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 7
sum 21
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 7
sum 21
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 7
sum 21
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 7
sum 28
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 7
sum 28
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 8
sum 28
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 8
sum 28
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 8
sum 28
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 8
sum 36
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 8
sum 36
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 9
sum 36
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 9
sum 36
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 9
sum 36
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 9
sum 45
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 9
sum 45
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 10
sum 45
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 10
sum 45
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 10
sum 45
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 10
sum 55
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 10
sum 55
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 11
sum 55
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 11
sum 55
11<=10 is False
The for loop terminated and
go to the next statement.
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
for(long sum=0; i <= 10;i++)
sum += i;
cout << "The sum of the first " << n
<< " integers is " << sum;
i 11
sum 55
Output
The sum of the first 10 integers is 55
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
THE WHILE LOOP
• The second loop provided by c++ is the while loop. It is an
entry controlled loop. The syntax for a while loop is:-
while (expression)
loop-body
• Where the loop may contain a single statement, a compound
statement or an empty statement.
• The loop iterates while the expression is true.
• When the expression becomes false, the program control
terminates the loop.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
• In a while loop, a variable should be initialized before
the loop as uninitialized variables cannot be used in the
loop.
• The loop variable should be updated inside the body-of-
the-loop.
• Following example explains the working of a while loop;
which is a c++ program which calculates the factorial of
a positive integer.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Let us see how it’s work
#include<iostream.h>
int main()
{
int i,n;
long fact=1;
cout<<“Enter a positive integer”;
cin>>n;
i=1;
while(i<=n)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
return 0;
}
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
i --
fact --
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
i 1
fact 1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 1
fact 1
1<=10 is True
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 1
fact 1
fact=1*1=1
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 1
fact 1
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 1
fact 1
i=i+1=1+1=2
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 2
fact 1
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 2
fact 1
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
2<=10 is True
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 2
fact 1
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
fact=1*2=2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 2
fact 2
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 2
fact 2
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 3
fact 2
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 3
fact 2
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 3
fact 2
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 3
fact 6
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 3
fact 6
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 4
fact 6
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 4
fact 6
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 4
fact 6
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 4
fact 24
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 4
fact 24
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 5
fact 24
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 5
fact 24
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 5
fact 24
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 5
fact 120
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 5
fact 120
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 6
fact 120
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 6
fact 120
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 6
fact 120
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 6
fact 720
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 6
fact 720
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 7
fact 720
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 7
fact 720
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 7
fact 720
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 7
fact 5040
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 7
fact 5040
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 8
fact 5040
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 8
fact 5040
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 8
fact 5040
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 8
fact 40320
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 8
fact 40320
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 9
fact 40320
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 9
fact 40320
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 9
fact 40320
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 9
fact 362880
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 9
fact 362880
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 10
fact 362880
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 10
fact 362880
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 10
fact 362880
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 10
fact 3628800
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 10
fact 3628800
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 11
fact 3628800
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 11
fact 3628800
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
11<=10 is False
The for loop terminated and
go to the next statement.
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example- let n=10
i 11
fact 3628800
while(i<=10)
{
fact=fact*i;
i=i++;
}
cout<<“The factorial of the num.=“<<fact;
Output
The factorial of the num.=3628800
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
While Semantics
Expression
Action
true false
Expression is
evaluated at the
start of each
iteration of the
loop
If Expression is
true, Action is
executed If Expression is
false, program
execution
continues w ith
next statement
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Computing an Average
#include<iostream.h>
int main()
{
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
cout<<"Enter the numbers to be calculated"<<endl;
while (numberProcessed < listSize)
{
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
return 0;
Let us see how it works
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Computing an Average
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
Suppose input contains: 1 5 3 1 6
listSize 4
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
Suppose input contains: 1 5 3 1 6
4listSize
0
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
Suppose input contains: 1 5 3 1 6
4listSize
0
0
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
Suppose input contains: 1 5 3 1 6
4listSize
0
0
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
0
0
--
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
0
0
1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
0
0
1
1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
0
1
1
1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
1
1
1
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
1
1
--
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
1
1
5
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
1
1
5
6
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
1
6
5
2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
2
6
5
6
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
2
6
--
2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
2
6
3
2
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
2
6
3
9
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
2
9
3
3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
3
9
3
3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
3
9
--
3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
3
9
1
3
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
3
9
1
10
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
3
10
1
4
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
value
Suppose input contains: 1 5 3 1 6
4listSize
3
10
1
4
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
Suppose input contains: 1 5 3 1 6
4listSize
3
10
average 2.5
4
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Execution Trace
int listSize = 4;
int numberProcessed = 0;
double sum = 0;
while (numberProcessed < listSize) {
double value;
cin >> value;
sum += value;
numberProcessed++;
}
double average = sum / numberProcessed ;
cout << "Average: " << average << endl;
numberProcessed
sum
average
Suppose input contains: 1 5 3 1 6
4listSize
3
10
2.5
4
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Example : while
int a,b,sum;
cout << “This program will return the ”;
cout << “summation of integers from a to b.nn”;
cout << “Input two integers a and b:”;
cin >> a >> b;
while (a <= b)
{
sum += a;
a++;
}
cout << “The sum is ” << sum << endl;
sum = 0;
sum = sum + a;
Don’t forget to set
sum = 0;
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
THE DO-WHILE LOOP
Unlike the for and while loop do-while loop is an exit controlled loop
i.e., it evaluates the test-expression at the bottom of the loop after
the body-of-the-loop. This means that the do-while loop always
executes at least once. The syntax for do-while loop is:-
do
{
statement;
}
while(test-expression)
The brackets { } are not necessary when there is only one statement in
the loop-body.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
An example for do-while loop is:-
int i=1,n=10002;
do
{
cout<<n;
n++;
i++;
}
while(i==1);
In the above example the test expression is never true but still
every time the loop is executed the loop would execute only once
and print “10002”.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
do-while
Condition
Statement
list
T
F
do
{
Statement list
} while (Condition);
string ans = “n”;
while (ans != “Y”)
{
cout << “Would you marry me?”;
cin >> ans;
}
cout << “Great!!”;
; is required
do
{
cout << “Would you marry me?”;
cin >> ans;
} while (ans != “Y”);
cout << “Great!!”;
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
Example 1: do-while
int i;
....
do
{
cout << “Please input a number between ”
<< “10 and 20:”;
cin >> i;
} while (i < 10 || i > 20);
......
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
Primality Test
A prime number is a positive integer that cannot be
factorized, i.e., no numbers other that 1 and itself
can divide it.
is 893 a prime?
Is (893 % 2 == 0) ?
Is (893 % 3 == 0) ?
Is (893 % 4 == 0) ?
Is (893 % 5 == 0) ?
Is (893 % 6 == 0) ?
.
.
.
Is (893 % 892 == 0) ?
We can write a loop
to test these.
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
Example: Silly Primality Test
#include<iostream.h>
int main()
{
long int p,r,i=2;
cout << “Enter a positive integer:";
cin >> p;
do
{
r = p % i;
i++;
}
while (i < p && r != 0);
if (i == p)
cout << " p is a prime number.";
else
{
cout << "p is not a prime number.";
cout << "The least factor is " << --i;
}
return 0;
}
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
NESTED LOOPS
C++ allows its users to use a number of loops inside a single loop,
hence it means that c++ allows the use of nested loops. A user can
use nested loops according to his own requirement. The following
example explains the use of nested loops:-
for(i=1;i<=4;i++)
{
cout<<“n”;
for(j=1;j<=i;j++)
cout<<“*”;
}
In the above program the inner loop will execute for i number of
times and i has values 1,2,3,4.
Left as Reading Assignment!!!!
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
The output of the above program segment would be:-
*
**
***
****
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Nested loops (loop in loop)
cin >> a >> b;
for (int i = 0; i < a; i++)
{
for (int j=0; j<b; j++)
{
cout << “*”;
}
cout << endl;
}
***
***
***
***
b
a
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
Nested loops (2)
int a,b;
cin >> a >> b;
for (int i = 0; i < a; i++)
{
for (int j=0; j<b; j++)
{
if (j > i)
break;
cout << “*”;
}
cout << endl;
}
*
**
***
****
b
a
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
Nested loops (3) *
**
***
****
b
a
int a,b;
cin >> a >> b;
for (int i = 0; i < a; i++)
{ for (int j=0; j<b && j < i; j++)
{
cout << “*”;
}
cout << endl;
}
j <= i;
if (j > i) break;
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
Nested loops (4)
int a,b;
cin >> a >> b;
for (int i = 0; i < a; i++)
{
for (int j=0; j<b; j++)
{
if (j < i)
cout << “ ”;
else
cout << “*”;
}
cout << endl;
}
*************
************
***********
**********
b
a
=
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
Nested loops (5)
int a,i,j;
cin >> a;
for (i = 0; i < a; i++) {
for (j=0; j<a; j++) {
if (j < a-i)
cout << " ";
else cout << "*";
}
for (j=0; j<a; j++) {
if (j > i) break;
cout << "*";
}
cout << endl;
}
*
***
*****
*******
*********
***********
Prepared by: Wudneh Tilahun, Debre
Tabor University-2016
User Defined functions
 Standard (Predefined) Functions
 User-Defined Functions
 Value-Returning Functions
 The return Statement
 Function Prototype
 Flow of Execution
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Top-Down Structured Design with Functions
Recall two types of functions
 Value returning function
 computes a single value
 returns value to calling code
 uses return command
 Void function (procedure)
 called as a statement
 executes some task
 This chapter focuses on the value returningPrepared by: Wudneh Tilahun,
Debre Tabor University-2016
Function definition …
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
• A function in C++ language is a block of code that
performs a specific task.
A function definition has the following syntax:
<type> <function name>(<parameter list>)
{
<local declarations>
<sequence of statements>
return <value>
}
User defined function
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Advantages of Using Functions
1. To help make the program more
understandable
2. To modularize the tasks of the program
 building blocks of the program
3. Write a module once
 those lines of source code are called multiple
times in the program
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Advantages of Using Functions
4. While working on one function, you can focus on
just that part of the program
 construct it,
 debug it,
 perfect it.
5. Different people can work on different functions
simultaneously.
6. If a function is needed in more than one place in
a program, or in different programs, you can
write it once and use it many times
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Standard (Predefined) Functions
 Predefined functions
 Part of the C++ language
 Provided in function libraries
 Examples:
abs(x), sin(x), log(x), pow( x, n)
 These functions will return a value
 To be printed cout << sin (x);
 To be assigned y = pow (3, 4.5);
 To be used in an expression 3.14 * sqr(r)
Make sure to use the
required #include file
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Value-Returning Functions
For the compiler to use a function you have
written, it must know when it finds that
function call in your source code …
1. The name of the function
2. The number of parameters, if any
3. The data type of each parameter
4. Data type of the value returned by the
function
5. The code required to do the calculation
Information provided by the
heading of the function
Information provided in the
body of the function
All the properties together
make up the definition of
the function
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Value-Returning Functions
 Consider a function for the area of a circle:
double circleArea (double radius)
{
return 3.14159 * radius * radius;
}
 Note the
 Heading (type, name, parameters)
 The body
 The return statement
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Value-Returning Functions
 Consider a function for the area of a circle:
float CircleArea(float r){
float Pi = 3.1415;
float Area= Pi * r * r;
return Area;}
 Note the
 Heading (type, name, parameters)
 The body
 The return statement
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Parameters
 Function definition syntax:
functionType functionName (formal parameter list)
{
statements
}
 Call (invocation of the function)
cout << "Enter radius for circle area -> ";
cin >> radius;
area = circleArea (radius);
Parameters in the declaration
: formal parameters
Parameters in the call:
actual parameters
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
The return Statement
 A value returning statement must have a
return statement
 Else a warning from the compiler
 Also the function will actually return a "garbage"
value
 Syntax:
return expression;
 View example
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Function Prototype
 Recall that the compiler must know certain
things about your function
 When it finds a function call in your source code
 Must know information in heading
 Your program must have at least the
heading of a function before it is invoked
 Usually listed before function main ( )
 View example
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
Flow of Control
 First statement executed in any program is
the first statement in the function main( )
 When another function called
 logical control passed to first statement in that
function’s body
 program proceeds through sequence of
statements within the function
 When last statement of function executed
 control returns to where function was called
 control given to next command after the call
Prepared by: Wudneh Tilahun,
Debre Tabor University-2016
How to call function ???
• A function call has the following syntax:
<function name>(<argument list>)
• A function call argument list must match in both data type
and number of parameter
• Example:- from the above function
– the function name is CircleArea and
– the argument list is radius.
so that we can call the above function as
float Area=CircleArea(radius);
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Example:-Write the program of the area of a circle?
#include <iostream.h>
#include <math.h>
float CircleArea(float r)
{
float Pi = 3.1415;
float Area= Pi * r * r;
return Area;
}
int main()
{
cout << "Enter radius: ";
float Radius;
cin >> Radius;
float Area = CircleArea(Radius);
cout << "Circle has area " << Area<<“Sqr units”;
return 0;
}
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Example:
The following example is the source code
for a function called max(). This
function takes two parameters x and y and
returns the maximum froom the two:
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
#include<iostream.h>
int max(int x, int y)
{ // returns larger of the two given integers:
if (x < y) return y; else return x;
}
int main()
{ // tests the max() function:
int m, n;
do
{ cin >> m >> n;
cout << "tmax(" << m << "," << n << ") = " <<
max(m,n) << endl;
}
while (m != 0);
return 0;
} Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Class activity
The Factorial Function
The factorial of a positive integer n is the
number n! obtained by multiplying n by all the
positive integers less than n:
n! = (n)(n - 1) • • • (3)(2)(1)
For example, 5! = (5)(4)(3)(2)(1) = 120.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Class activity
The Permutation Function
A permutation is an arrangement of elements
taken from a finite set. The permutation function
P(n,k) gives the number of different
permutations of any k items taken from a set of n
items. One way to compute this function is by the
formula
For example, P(5,2) =
𝟓!
𝟓−𝟐 !
=
(5)(4)3!
𝟑!
= 20.
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
#include<iostream.h>
long fact(int n)
{ // returns n!=n*(n-1)*(n-2)*...*(2)(1)
if (n< 0)
return 0;
int f= 1;
while (n >1)
f *= n-- ;
return f;
}
int main()
{
int m;
cout<<"Enter the posertive integern";
cin>>m;
cout << "The value of "<<m<<"!="<<fact(m)<< endl;
return 0;
} Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
#include<iostream.h>
long fact(int n)
{ // returns n!=n*(n-1)*(n-2)*...*(2)(1)
if (n< 0)
return 0;
int f= 1;
while (n >1)
f *= n-- ;
return f;
}
long perm(int n, int k)
{ // returns P(n,k), the number of permutations of k from n:
if (n < 0 || k < 0 || k > n)
return 0;
return fact(n)/fact(n-k);
} Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
int main()
{
int i,j;
cout<<"Enter two numbers"<<endl;
cin>>i>>j;
if(i<j)
cout<<"invalid entry, pls try again"<<endl;
else
cout<<"The value of P("<<i<<", "<<j<<")="<<perm(i,j);
return 0;
}
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Newton-Raphson Method
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Newton-Raphson Method
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
double f(double x)
{
return pow(x,3)-17 ;
}
double g(double x)
{
return 3*pow(x,2) ;
}
int main()
{
int i,n;
double x[100];
cout<<"enter the initial point x0n";
cin>>x[0]; Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Newton-Raphson Method cont …
cout<<"Enter the number of iteration: ";
cin>>n;
for(i=1;i<=n; i++)
{
x[i]=x[i-1]-f(x[i-1])/g(x[i-1]);
cout<<"The result of iteration number "<<i<< " is
"<<setprecision(5)<<x[i]<<endl;
}
return 0;
}
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Newton-Raphson Method updated
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
double f(double x)
{
return log(x)-cos(x);
}
double g(double x)
{
return (1/x)+sin(x) ;
}
int main()
{
int i,n;
double x[100], er;
cout<<"enter the initial point x0n";
cin>>x[0]; Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Newton-Raphson Method updated cont …
cout<<"Enter the number of iteration: ";
cin>>n;
cout<<"enter the tolerance ERROR";
cin>>er;
for(i=1;i<=n; i++)
{
x[i]=x[i-1]-f(x[i-1])/g(x[i-1]);
cout<<"The result of iteration number "<<i<< " is
"<<setprecision(5)<<x[i]<<endl;
if(fabs(x[i]-x[i-1])<er)
break;
else
continue;
}
return 0;
}
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Practice exercises
• Write and run a program that prints the sum, difference, product,
quotient, and remainder of
two integers that are input interactively.
int main()
{ // prints the results of arithmetic operators
int m,n;
cout << "Enter two integers: ";
cin >> m >> n;
cout << "The integers are " << m << " and " << n
<< endl;
cout << "Their sum is " << (m + n) << endl;
cout << "Their difference is " << (m - n) <<
endl;
cout << "Their product is " << (m * n) << endl;
cout << "Their quotient is " << (m / n) << endl;
cout << "Their remainder is " << (m % n) <<
endl;
}
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Practice exercises
• Write a program that converts inches to centimeters. For
example, if the user enters 16.9 for a length in inches, the
output would be 42.926 cm. (One inch equals 2.54
centimeters.)
• We use two variables of type float
int main()
{ // converts inches to centimeters:
float inches,cm;
cout << "Enter length in inches: ";
cin >> inches;
cm = 2.54*inches;
cout << inches << " inches = " << cm << "
centimeters.n";
}
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Practice exercises
• Write a program that implements the quadratic formula to solve
quadratic equations.
• int main()
{ // implements the quadratic formula
double a,b,c;
cout << "Enter the coefficients:" << endl;
cout << "ta: ";
cin >> a;
cout << "tb: ";
cin >> b;
cout << "tc: ";
cin >> c;
cout << "The equation is: " << a << "*x*x + " << b
<< "*x + " << c << " = 0" << endl;
double d = b*b - 4*a*c;
double sqrtd = sqrt(d);
double x1 = (-b + sqrtd)/(2*a);
double x2 = (-b - sqrtd)/(2*a);
cout << "The solutions are:" << endl;
cout << "tx1 = " << x1 << endl;
cout << "tx2 = " << x2 << endl;
cout << "Check:" << endl;
cout << "ta*x1*x1 + b*x1 + c = " << a*x1*x1 + b*x1 + c <<
endl;
cout << "ta*x2*x2 + b*x2 + c = " << a*x2*x2 + b*x2 + c <<
endl;
}
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
The End
Thank you for your attention!!
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Syntax of for Loop
• The initial statement, loop
condition, and update statement are
called for loop control statements
– initial statement usually initializes a variable
(called the for loop control, or for indexed, variable)
for (initialization; condition; update)
statement;
Back to for Loop
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Syntax of while Loop
• The loop continues to iterate until the condition
becomes true.
• The while loop tests the condition at the top of the loop
and so the statement will not execute if the condition is
initially false.
while(condition)
{
statement;
} Back to for Loop
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016
Syntax of do…while Loop
• The statement executes first, and then the expression is
evaluated
• To avoid an infinite loop, body must contain a statement
that makes the expression false
• The statement can be simple or compound
• Loop always iterates at least once
do
{
statement;
}
while(expression);
Back to for Loop
Prepared by: Wudneh Tilahun, Debre Tabor
University-2016

More Related Content

What's hot (20)

C++ programming
C++ programmingC++ programming
C++ programming
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Loops
LoopsLoops
Loops
 
Iteration
IterationIteration
Iteration
 
C# Loops
C# LoopsC# Loops
C# Loops
 
While loop
While loopWhile loop
While loop
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
Loops c++
Loops c++Loops c++
Loops c++
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Control statements
Control statementsControl statements
Control statements
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 

Viewers also liked (15)

Dev C++ Code Basic Loop
Dev C++ Code Basic LoopDev C++ Code Basic Loop
Dev C++ Code Basic Loop
 
C++ 11 range-based for loop
C++ 11   range-based for loopC++ 11   range-based for loop
C++ 11 range-based for loop
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statement
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
 
Flow chart programming
Flow chart programmingFlow chart programming
Flow chart programming
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Loops
LoopsLoops
Loops
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Flow charts
Flow chartsFlow charts
Flow charts
 
Flow chart
Flow chartFlow chart
Flow chart
 
ppt of flowchart
ppt of flowchartppt of flowchart
ppt of flowchart
 
Flow chart powerpoint presentation slides ppt templates
Flow chart powerpoint presentation slides ppt templatesFlow chart powerpoint presentation slides ppt templates
Flow chart powerpoint presentation slides ppt templates
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
Deep C
Deep CDeep C
Deep C
 

Similar to Loop control in c++

Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
Lect3-C--EEB.pptx
Lect3-C--EEB.pptxLect3-C--EEB.pptx
Lect3-C--EEB.pptxKIJAMALEGI
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptxNaumanRasheed11
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSAniruddha Paul
 
C code This program will calculate the sum of 10 positive .docx
 C code This program will calculate the sum of 10 positive .docx C code This program will calculate the sum of 10 positive .docx
C code This program will calculate the sum of 10 positive .docxaryan532920
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loopsManzoor ALam
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxJavvajiVenkat
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptxffyuyufyfufufufu
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_ifTAlha MAlik
 

Similar to Loop control in c++ (20)

Loops in c
Loops in cLoops in c
Loops in c
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
175035-cse LAB-04
175035-cse LAB-04 175035-cse LAB-04
175035-cse LAB-04
 
Lect3-C--EEB.pptx
Lect3-C--EEB.pptxLect3-C--EEB.pptx
Lect3-C--EEB.pptx
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
M C6java6
M C6java6M C6java6
M C6java6
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMS
 
C code This program will calculate the sum of 10 positive .docx
 C code This program will calculate the sum of 10 positive .docx C code This program will calculate the sum of 10 positive .docx
C code This program will calculate the sum of 10 positive .docx
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Python Lecture 5
Python Lecture 5Python Lecture 5
Python Lecture 5
 
Chapter08.pptx
Chapter08.pptxChapter08.pptx
Chapter08.pptx
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptx
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptx
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 

Recently uploaded

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxbennyroshan06
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfPo-Chuan Chen
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfYibeltalNibretu
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...Denish Jangid
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 

Recently uploaded (20)

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 

Loop control in c++

  • 1. Debre Tabor University Department of Mathematics Loop control statments 2016 By: Wudneh Tilahun Mengist
  • 2. Out lines of the presentation Introduction Iteration/About loop The for loop The While loop The Do…While loop Nested Loop User defined functions Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 3. INTRODUCTION Generally a program executes its statements from beginning to the end, but all programs don't follow this. This presentation will tell us in detail about the program control statements. STATEMENTS Statements are the instructions given to the computer to perform any kind of data movements, be it making decisions or repeating actions. They form the smallest executable unit of the program. They are terminated by a semicolon(;). The different types of statements are:- Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 4. STATEMENT FLOW CONTROL In a program, statements may be executed sequentially , selectively or iteratively:- Sequence:- This is the default flow of the statements and also known as sequence construct and it also means that the statements are being executed sequentially. Selection:- The selection construct means that the execution of the statements depends on a condition-test, if the condition is true then the statements would be executed else they would be skipped. An else-if statement is a good example of selection statement. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 5. Iteration:-  The iteration means repetition of a certain set of statements based on a certain condition-test. Looping statements can be used to perform the repetition of a certain set of statements.  Iteration statements in c++ allows a certain number of instructions to be performed repeatedly until a certain condition becomes false or for a fixed number of times.  Iteration statements are also called loops or looping statements. There are three C++ repetition statements:  The For Statement or loop  The While statement or loop  The do…while statement or loopPrepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 6. Elemnts that control a loop Every loop has its elements that control and govern the execution. Generally, a loop has 4 elements that have different purposes. These elements are as given below:- 1. Initialization Expression(s): Before entering the loop the initialization expression(s) initializes the loop control variables with their first values. 2. Test Expression: The test expression is that expression whosetruth value decides whether the loop-body will be executed or not. If the value is true then the body gets executed else it gets terminated. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 7. 3. Update expression(s): The update expression(s) change the value(s) of loop variable(s). The update expression execute at the end of the loop. 4. The Body-of-the-loop: The statements that are to be executed form the body-of-the-loop. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 8. THE FOR LOOP The for loop is the easiest to understand of all because all its loop- control elements are gathered in one place i.e. at the top. Its syntax is:- For(initialization expression(s);test—expression; update expression) body-of-the-loop; Example of for loop #include<iostream.h> int main() { int i; for(i=1;i<=10;i++) cout<<“n”<<i; return 0; } Condition Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 9.  Firstly, initialization expression is executed i.e., i=1 which gives the first value 1 to variable i.  Then, the test-expression is executed i.e., i<=10 which results into true i.e., 1.  Since, the test expression is true, the body-of-the-loop i.e., cout<<“n”<<i is executed which prints the current value of i on the next line.  After executing the loop-body, the update expression i.e., i++ is executed which increments the value of i.  After the update expression is executed, the test-expression is again evaluated. If it is true the sequence is repeated from step no. 3, otherwise the loop terminates. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 10. ForExpr Action true false ForInit PostExpr Evaluated once at the beginning of the for statements's execution The ForExpr is evaluated at the start of each iteration of the loop If ForExpr is true, Action is executed After the Action has completed, the PostExpression is evaluated If ForExpr is false, program execution continues with next statement After evaluating the PostExpression, the next iteration of the loop starts Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 11. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i 0 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 12. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i 0 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 13. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i 0 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 14. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i 0 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 15. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 16. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 17. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 18. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 19. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 20. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 21. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 22. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 23. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 24. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 25. Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 all done i 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 26. Let us see how it’s work #include<iostream.h> int main() { return 0; } Example:- write a c++ program which evaluate the sum of the first +ve n integer. Alterna Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 27. Let us see how it’s work #include<iostream.h> int main() { int n, i=1; cout << "Enter a positive integer : "; cin >> n; for(long sum=0; i <= n;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; return 0; } Example:- write a c++ program which evaluate the sum of the first +ve n integer. Alterna Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 28. Example- let n=10 i -- sum 0 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 29. Example- let n=10 i 1 sum 0 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 30. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 1 sum 0 1<=10 is True Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 31. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 1 sum 0 Sum=sum+i=0+1=1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 32. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 1 sum 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 33. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 1 sum 1 i=i+1=1+1=2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 34. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 2 sum 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 35. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 2 sum 1 2<=10 is True Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 36. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 2 sum 1 Sum=sum+i=1+2=3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 37. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 2 sum 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 38. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 2 sum 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 39. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 3 sum 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 40. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 3 sum 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 41. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 3 sum 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 42. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 3 sum 6 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 43. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 3 sum 6 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 44. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 4 sum 6 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 45. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 4 sum 6 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 46. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 4 sum 6 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 47. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 4 sum 10 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 48. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 4 sum 10 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 49. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 5 sum 10 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 50. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 5 sum 10 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 51. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 5 sum 10 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 52. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 5 sum 15 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 53. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 5 sum 15 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 54. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 6 sum 15 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 55. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 6 sum 15 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 56. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 6 sum 15 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 57. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 6 sum 21 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 58. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 6 sum 21 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 59. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 7 sum 21 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 60. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 7 sum 21 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 61. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 7 sum 21 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 62. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 7 sum 28 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 63. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 7 sum 28 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 64. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 8 sum 28 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 65. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 8 sum 28 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 66. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 8 sum 28 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 67. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 8 sum 36 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 68. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 8 sum 36 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 69. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 9 sum 36 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 70. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 9 sum 36 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 71. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 9 sum 36 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 72. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 9 sum 45 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 73. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 9 sum 45 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 74. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 10 sum 45 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 75. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 10 sum 45 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 76. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 10 sum 45 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 77. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 10 sum 55 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 78. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 10 sum 55 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 79. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 11 sum 55 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 80. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 11 sum 55 11<=10 is False The for loop terminated and go to the next statement. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 81. Example- let n=10 for(long sum=0; i <= 10;i++) sum += i; cout << "The sum of the first " << n << " integers is " << sum; i 11 sum 55 Output The sum of the first 10 integers is 55 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 82. THE WHILE LOOP • The second loop provided by c++ is the while loop. It is an entry controlled loop. The syntax for a while loop is:- while (expression) loop-body • Where the loop may contain a single statement, a compound statement or an empty statement. • The loop iterates while the expression is true. • When the expression becomes false, the program control terminates the loop. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 83. • In a while loop, a variable should be initialized before the loop as uninitialized variables cannot be used in the loop. • The loop variable should be updated inside the body-of- the-loop. • Following example explains the working of a while loop; which is a c++ program which calculates the factorial of a positive integer. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 84. Let us see how it’s work #include<iostream.h> int main() { int i,n; long fact=1; cout<<“Enter a positive integer”; cin>>n; i=1; while(i<=n) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 85. Example- let n=10 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; i -- fact -- Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 86. Example- let n=10 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; i 1 fact 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 87. Example- let n=10 i 1 fact 1 1<=10 is True while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 88. Example- let n=10 i 1 fact 1 fact=1*1=1 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 89. Example- let n=10 i 1 fact 1 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 90. Example- let n=10 i 1 fact 1 i=i+1=1+1=2 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 91. Example- let n=10 i 2 fact 1 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 92. Example- let n=10 i 2 fact 1 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; 2<=10 is True Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 93. Example- let n=10 i 2 fact 1 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; fact=1*2=2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 94. Example- let n=10 i 2 fact 2 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 95. Example- let n=10 i 2 fact 2 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 96. Example- let n=10 i 3 fact 2 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 97. Example- let n=10 i 3 fact 2 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 98. Example- let n=10 i 3 fact 2 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 99. Example- let n=10 i 3 fact 6 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 100. Example- let n=10 i 3 fact 6 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 101. Example- let n=10 i 4 fact 6 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 102. Example- let n=10 i 4 fact 6 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 103. Example- let n=10 i 4 fact 6 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 104. Example- let n=10 i 4 fact 24 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 105. Example- let n=10 i 4 fact 24 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 106. Example- let n=10 i 5 fact 24 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 107. Example- let n=10 i 5 fact 24 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 108. Example- let n=10 i 5 fact 24 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 109. Example- let n=10 i 5 fact 120 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 110. Example- let n=10 i 5 fact 120 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 111. Example- let n=10 i 6 fact 120 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 112. Example- let n=10 i 6 fact 120 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 113. Example- let n=10 i 6 fact 120 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 114. Example- let n=10 i 6 fact 720 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 115. Example- let n=10 i 6 fact 720 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 116. Example- let n=10 i 7 fact 720 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 117. Example- let n=10 i 7 fact 720 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 118. Example- let n=10 i 7 fact 720 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 119. Example- let n=10 i 7 fact 5040 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 120. Example- let n=10 i 7 fact 5040 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 121. Example- let n=10 i 8 fact 5040 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 122. Example- let n=10 i 8 fact 5040 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 123. Example- let n=10 i 8 fact 5040 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 124. Example- let n=10 i 8 fact 40320 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 125. Example- let n=10 i 8 fact 40320 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 126. Example- let n=10 i 9 fact 40320 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 127. Example- let n=10 i 9 fact 40320 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 128. Example- let n=10 i 9 fact 40320 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 129. Example- let n=10 i 9 fact 362880 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 130. Example- let n=10 i 9 fact 362880 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 131. Example- let n=10 i 10 fact 362880 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 132. Example- let n=10 i 10 fact 362880 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 133. Example- let n=10 i 10 fact 362880 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 134. Example- let n=10 i 10 fact 3628800 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 135. Example- let n=10 i 10 fact 3628800 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 136. Example- let n=10 i 11 fact 3628800 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 137. Example- let n=10 i 11 fact 3628800 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; 11<=10 is False The for loop terminated and go to the next statement. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 138. Example- let n=10 i 11 fact 3628800 while(i<=10) { fact=fact*i; i=i++; } cout<<“The factorial of the num.=“<<fact; Output The factorial of the num.=3628800 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 139. While Semantics Expression Action true false Expression is evaluated at the start of each iteration of the loop If Expression is true, Action is executed If Expression is false, program execution continues w ith next statement Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 140. Computing an Average #include<iostream.h> int main() { int listSize = 4; int numberProcessed = 0; double sum = 0; cout<<"Enter the numbers to be calculated"<<endl; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; return 0; Let us see how it works Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 141. Computing an Average int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 142. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; Suppose input contains: 1 5 3 1 6 listSize 4 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 143. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed Suppose input contains: 1 5 3 1 6 4listSize 0 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 144. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum Suppose input contains: 1 5 3 1 6 4listSize 0 0 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 145. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum Suppose input contains: 1 5 3 1 6 4listSize 0 0 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 146. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 0 0 -- Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 147. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 0 0 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 148. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 0 0 1 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 149. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 0 1 1 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 150. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 1 1 1 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 151. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 1 1 -- Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 152. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 1 1 5 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 153. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 1 1 5 6 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 154. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 1 6 5 2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 155. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 2 6 5 6 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 156. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 2 6 -- 2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 157. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 2 6 3 2 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 158. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 2 6 3 9 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 159. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 2 9 3 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 160. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 3 9 3 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 161. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 3 9 -- 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 162. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 3 9 1 3 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 163. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 3 9 1 10 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 164. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 3 10 1 4 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 165. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum value Suppose input contains: 1 5 3 1 6 4listSize 3 10 1 4 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 166. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum Suppose input contains: 1 5 3 1 6 4listSize 3 10 average 2.5 4 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 167. Execution Trace int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; numberProcessed++; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; numberProcessed sum average Suppose input contains: 1 5 3 1 6 4listSize 3 10 2.5 4 Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 168. Example : while int a,b,sum; cout << “This program will return the ”; cout << “summation of integers from a to b.nn”; cout << “Input two integers a and b:”; cin >> a >> b; while (a <= b) { sum += a; a++; } cout << “The sum is ” << sum << endl; sum = 0; sum = sum + a; Don’t forget to set sum = 0; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 169. THE DO-WHILE LOOP Unlike the for and while loop do-while loop is an exit controlled loop i.e., it evaluates the test-expression at the bottom of the loop after the body-of-the-loop. This means that the do-while loop always executes at least once. The syntax for do-while loop is:- do { statement; } while(test-expression) The brackets { } are not necessary when there is only one statement in the loop-body. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 170. An example for do-while loop is:- int i=1,n=10002; do { cout<<n; n++; i++; } while(i==1); In the above example the test expression is never true but still every time the loop is executed the loop would execute only once and print “10002”. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 171. do-while Condition Statement list T F do { Statement list } while (Condition); string ans = “n”; while (ans != “Y”) { cout << “Would you marry me?”; cin >> ans; } cout << “Great!!”; ; is required do { cout << “Would you marry me?”; cin >> ans; } while (ans != “Y”); cout << “Great!!”; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 172. Example 1: do-while int i; .... do { cout << “Please input a number between ” << “10 and 20:”; cin >> i; } while (i < 10 || i > 20); ...... Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 173. Primality Test A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it. is 893 a prime? Is (893 % 2 == 0) ? Is (893 % 3 == 0) ? Is (893 % 4 == 0) ? Is (893 % 5 == 0) ? Is (893 % 6 == 0) ? . . . Is (893 % 892 == 0) ? We can write a loop to test these. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 174. Example: Silly Primality Test #include<iostream.h> int main() { long int p,r,i=2; cout << “Enter a positive integer:"; cin >> p; do { r = p % i; i++; } while (i < p && r != 0); if (i == p) cout << " p is a prime number."; else { cout << "p is not a prime number."; cout << "The least factor is " << --i; } return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 175. NESTED LOOPS C++ allows its users to use a number of loops inside a single loop, hence it means that c++ allows the use of nested loops. A user can use nested loops according to his own requirement. The following example explains the use of nested loops:- for(i=1;i<=4;i++) { cout<<“n”; for(j=1;j<=i;j++) cout<<“*”; } In the above program the inner loop will execute for i number of times and i has values 1,2,3,4. Left as Reading Assignment!!!! Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 176. The output of the above program segment would be:- * ** *** **** Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 177. Nested loops (loop in loop) cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { cout << “*”; } cout << endl; } *** *** *** *** b a Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 178. Nested loops (2) int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j > i) break; cout << “*”; } cout << endl; } * ** *** **** b a Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 179. Nested loops (3) * ** *** **** b a int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b && j < i; j++) { cout << “*”; } cout << endl; } j <= i; if (j > i) break; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 180. Nested loops (4) int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j < i) cout << “ ”; else cout << “*”; } cout << endl; } ************* ************ *********** ********** b a = Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 181. Nested loops (5) int a,i,j; cin >> a; for (i = 0; i < a; i++) { for (j=0; j<a; j++) { if (j < a-i) cout << " "; else cout << "*"; } for (j=0; j<a; j++) { if (j > i) break; cout << "*"; } cout << endl; } * *** ***** ******* ********* *********** Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 182. User Defined functions  Standard (Predefined) Functions  User-Defined Functions  Value-Returning Functions  The return Statement  Function Prototype  Flow of Execution Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 183. Top-Down Structured Design with Functions Recall two types of functions  Value returning function  computes a single value  returns value to calling code  uses return command  Void function (procedure)  called as a statement  executes some task  This chapter focuses on the value returningPrepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 184. Function definition … Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 185. • A function in C++ language is a block of code that performs a specific task. A function definition has the following syntax: <type> <function name>(<parameter list>) { <local declarations> <sequence of statements> return <value> } User defined function Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 186. Advantages of Using Functions 1. To help make the program more understandable 2. To modularize the tasks of the program  building blocks of the program 3. Write a module once  those lines of source code are called multiple times in the program Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 187. Advantages of Using Functions 4. While working on one function, you can focus on just that part of the program  construct it,  debug it,  perfect it. 5. Different people can work on different functions simultaneously. 6. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 188. Standard (Predefined) Functions  Predefined functions  Part of the C++ language  Provided in function libraries  Examples: abs(x), sin(x), log(x), pow( x, n)  These functions will return a value  To be printed cout << sin (x);  To be assigned y = pow (3, 4.5);  To be used in an expression 3.14 * sqr(r) Make sure to use the required #include file Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 189. Value-Returning Functions For the compiler to use a function you have written, it must know when it finds that function call in your source code … 1. The name of the function 2. The number of parameters, if any 3. The data type of each parameter 4. Data type of the value returned by the function 5. The code required to do the calculation Information provided by the heading of the function Information provided in the body of the function All the properties together make up the definition of the function Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 190. Value-Returning Functions  Consider a function for the area of a circle: double circleArea (double radius) { return 3.14159 * radius * radius; }  Note the  Heading (type, name, parameters)  The body  The return statement Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 191. Value-Returning Functions  Consider a function for the area of a circle: float CircleArea(float r){ float Pi = 3.1415; float Area= Pi * r * r; return Area;}  Note the  Heading (type, name, parameters)  The body  The return statement Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 192. Parameters  Function definition syntax: functionType functionName (formal parameter list) { statements }  Call (invocation of the function) cout << "Enter radius for circle area -> "; cin >> radius; area = circleArea (radius); Parameters in the declaration : formal parameters Parameters in the call: actual parameters Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 193. The return Statement  A value returning statement must have a return statement  Else a warning from the compiler  Also the function will actually return a "garbage" value  Syntax: return expression;  View example Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 194. Function Prototype  Recall that the compiler must know certain things about your function  When it finds a function call in your source code  Must know information in heading  Your program must have at least the heading of a function before it is invoked  Usually listed before function main ( )  View example Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 195. Flow of Control  First statement executed in any program is the first statement in the function main( )  When another function called  logical control passed to first statement in that function’s body  program proceeds through sequence of statements within the function  When last statement of function executed  control returns to where function was called  control given to next command after the call Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 196. How to call function ??? • A function call has the following syntax: <function name>(<argument list>) • A function call argument list must match in both data type and number of parameter • Example:- from the above function – the function name is CircleArea and – the argument list is radius. so that we can call the above function as float Area=CircleArea(radius); Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 197. Example:-Write the program of the area of a circle? #include <iostream.h> #include <math.h> float CircleArea(float r) { float Pi = 3.1415; float Area= Pi * r * r; return Area; } int main() { cout << "Enter radius: "; float Radius; cin >> Radius; float Area = CircleArea(Radius); cout << "Circle has area " << Area<<“Sqr units”; return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 198. Example: The following example is the source code for a function called max(). This function takes two parameters x and y and returns the maximum froom the two: Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 199. #include<iostream.h> int max(int x, int y) { // returns larger of the two given integers: if (x < y) return y; else return x; } int main() { // tests the max() function: int m, n; do { cin >> m >> n; cout << "tmax(" << m << "," << n << ") = " << max(m,n) << endl; } while (m != 0); return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 200. Class activity The Factorial Function The factorial of a positive integer n is the number n! obtained by multiplying n by all the positive integers less than n: n! = (n)(n - 1) • • • (3)(2)(1) For example, 5! = (5)(4)(3)(2)(1) = 120. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 201. Class activity The Permutation Function A permutation is an arrangement of elements taken from a finite set. The permutation function P(n,k) gives the number of different permutations of any k items taken from a set of n items. One way to compute this function is by the formula For example, P(5,2) = 𝟓! 𝟓−𝟐 ! = (5)(4)3! 𝟑! = 20. Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 202. #include<iostream.h> long fact(int n) { // returns n!=n*(n-1)*(n-2)*...*(2)(1) if (n< 0) return 0; int f= 1; while (n >1) f *= n-- ; return f; } int main() { int m; cout<<"Enter the posertive integern"; cin>>m; cout << "The value of "<<m<<"!="<<fact(m)<< endl; return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 203. #include<iostream.h> long fact(int n) { // returns n!=n*(n-1)*(n-2)*...*(2)(1) if (n< 0) return 0; int f= 1; while (n >1) f *= n-- ; return f; } long perm(int n, int k) { // returns P(n,k), the number of permutations of k from n: if (n < 0 || k < 0 || k > n) return 0; return fact(n)/fact(n-k); } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 204. int main() { int i,j; cout<<"Enter two numbers"<<endl; cin>>i>>j; if(i<j) cout<<"invalid entry, pls try again"<<endl; else cout<<"The value of P("<<i<<", "<<j<<")="<<perm(i,j); return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 205. Newton-Raphson Method Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 206. Newton-Raphson Method #include<iostream.h> #include<iomanip.h> #include<math.h> double f(double x) { return pow(x,3)-17 ; } double g(double x) { return 3*pow(x,2) ; } int main() { int i,n; double x[100]; cout<<"enter the initial point x0n"; cin>>x[0]; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 207. Newton-Raphson Method cont … cout<<"Enter the number of iteration: "; cin>>n; for(i=1;i<=n; i++) { x[i]=x[i-1]-f(x[i-1])/g(x[i-1]); cout<<"The result of iteration number "<<i<< " is "<<setprecision(5)<<x[i]<<endl; } return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 208. Newton-Raphson Method updated #include<iostream.h> #include<iomanip.h> #include<math.h> double f(double x) { return log(x)-cos(x); } double g(double x) { return (1/x)+sin(x) ; } int main() { int i,n; double x[100], er; cout<<"enter the initial point x0n"; cin>>x[0]; Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 209. Newton-Raphson Method updated cont … cout<<"Enter the number of iteration: "; cin>>n; cout<<"enter the tolerance ERROR"; cin>>er; for(i=1;i<=n; i++) { x[i]=x[i-1]-f(x[i-1])/g(x[i-1]); cout<<"The result of iteration number "<<i<< " is "<<setprecision(5)<<x[i]<<endl; if(fabs(x[i]-x[i-1])<er) break; else continue; } return 0; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 210. Practice exercises • Write and run a program that prints the sum, difference, product, quotient, and remainder of two integers that are input interactively. int main() { // prints the results of arithmetic operators int m,n; cout << "Enter two integers: "; cin >> m >> n; cout << "The integers are " << m << " and " << n << endl; cout << "Their sum is " << (m + n) << endl; cout << "Their difference is " << (m - n) << endl; cout << "Their product is " << (m * n) << endl; cout << "Their quotient is " << (m / n) << endl; cout << "Their remainder is " << (m % n) << endl; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 211. Practice exercises • Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a length in inches, the output would be 42.926 cm. (One inch equals 2.54 centimeters.) • We use two variables of type float int main() { // converts inches to centimeters: float inches,cm; cout << "Enter length in inches: "; cin >> inches; cm = 2.54*inches; cout << inches << " inches = " << cm << " centimeters.n"; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 212. Practice exercises • Write a program that implements the quadratic formula to solve quadratic equations. • int main() { // implements the quadratic formula double a,b,c; cout << "Enter the coefficients:" << endl; cout << "ta: "; cin >> a; cout << "tb: "; cin >> b; cout << "tc: "; cin >> c; cout << "The equation is: " << a << "*x*x + " << b << "*x + " << c << " = 0" << endl; double d = b*b - 4*a*c; double sqrtd = sqrt(d); double x1 = (-b + sqrtd)/(2*a); double x2 = (-b - sqrtd)/(2*a); cout << "The solutions are:" << endl; cout << "tx1 = " << x1 << endl; cout << "tx2 = " << x2 << endl; cout << "Check:" << endl; cout << "ta*x1*x1 + b*x1 + c = " << a*x1*x1 + b*x1 + c << endl; cout << "ta*x2*x2 + b*x2 + c = " << a*x2*x2 + b*x2 + c << endl; } Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 213. The End Thank you for your attention!! Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 214. Syntax of for Loop • The initial statement, loop condition, and update statement are called for loop control statements – initial statement usually initializes a variable (called the for loop control, or for indexed, variable) for (initialization; condition; update) statement; Back to for Loop Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 215. Syntax of while Loop • The loop continues to iterate until the condition becomes true. • The while loop tests the condition at the top of the loop and so the statement will not execute if the condition is initially false. while(condition) { statement; } Back to for Loop Prepared by: Wudneh Tilahun, Debre Tabor University-2016
  • 216. Syntax of do…while Loop • The statement executes first, and then the expression is evaluated • To avoid an infinite loop, body must contain a statement that makes the expression false • The statement can be simple or compound • Loop always iterates at least once do { statement; } while(expression); Back to for Loop Prepared by: Wudneh Tilahun, Debre Tabor University-2016