The document discusses loop control statements in C++. It describes the three main types of loops - for, while, and do-while loops. It provides examples of how a for loop works, including the initialization, test, and update expressions that control the loop execution. It also gives a sample program to calculate the sum of the first n positive integers using a for loop.
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
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
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
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