1. Lecture 4
Procedural Abstraction
and
Predefined Functions
Computer Programming I 1
2. Outline
for loop
switch
casting
predefined functions
Computer Programming I 2
3. for loop
A for-loop is another loop mechanism in C++
Designed for common tasks such as adding
numbers in a given range
Is sometimes more convenient to use than a
while loop
Does not do anything a while loop cannot do
Computer Programming I 3
4. for/while Loop Comparison
sum = 0;
n = 1;
while(n <= 10) // add the numbers 1 - 10
{
sum = sum + n;
n++;
}
sum = 0;
for (n = 1; n <= 10; n++) //add the numbers 1 - 10
sum = sum + n;
Computer Programming I 4
5. For Loop Dissection
The for loop uses the same components as the
while loop in a more compact form
for (n = 1; n <= 10; n++)
Initialization Action Update Action
Boolean Expression
Computer Programming I 5
6. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{
int i;
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 6
7. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 1
int i;
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 7
8. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 1
int i; 2
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 8
9. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 1
If true then execute body
int i; 2
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 9
10. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 12
int i; 2
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 10
11. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 12
int i; 2
3
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 11
12. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 12
If true then execute body
int i; 2
3
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 12
13. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 123
int i; 2
3
for (i=1; i<=3; i++)
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 13
14. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 123
int i; 2
3
for (i=1; i<=3; i++) 4
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 14
15. for loop (tracing)
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 123
If true then execute body
int i; 2
3
for (i=1; i<=3; i++) 4
{ cout<<i<<“ “;
}
system(“pause”);
return 0;
}
Computer Programming I 15
16. for loop
#include <iostream>
#include <conio.h>
using namespace std;
i output
int main()
{ 1 123
If true then execute body
int i; 2
3
for (i=1; i<=3; i++) 4
{ cout<<i<<“ “;
}
system(“pause”);
Not True
return 0;
}
Computer Programming I 16
17. for loop i j
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t";
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 17
18. for loop i j
1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t";
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 18
19. for loop i j
1 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t";
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 19
20. for loop i j
1 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 20
21. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 21
22. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 22
23. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 23
24. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij ij
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 24
25. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij ij
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 25
26. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij ij
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 26
27. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij ij
cout << endl;
ij
}
system(“pause”);
return 0;
}
Computer Programming I 27
28. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
2
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij ij
cout << endl;
ij
}
system(“pause”);
return 0;
}
Computer Programming I 28
29. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
2
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij ij
cout << endl;
ij ij
}
system(“pause”);
return 0;
}
Computer Programming I 29
30. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
2
int main() 3
{
int i,j;
3 1
2
for (i=1; i<=3; i++) 3
{
output
for (j=1; j<=3; j++)
cout << "i" << "j" << "t"; ij ij ij
cout << endl;
ij ij ij
}
system(“pause”); ij ij ij
return 0;
}
Computer Programming I 30
31. for loop
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t";
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 31
32. for loop i j
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t";
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 32
33. for loop i j
1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t";
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 33
34. for loop i j
1 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t";
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 34
35. for loop i j
1 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 35
36. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 36
37. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 37
38. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 38
39. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std;
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12 13
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 39
40. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12 13
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 40
41. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12 13
cout << endl;
}
system(“pause”);
return 0;
}
Computer Programming I 41
42. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12 13
cout << endl;
21
}
system(“pause”);
return 0;
}
Computer Programming I 42
43. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
2
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12 13
cout << endl;
21
}
system(“pause”);
return 0;
}
Computer Programming I 43
44. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
2
int main()
{
int i,j;
for (i=1; i<=3; i++)
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12 13
cout << endl;
21 22
}
system(“pause”);
return 0;
}
Computer Programming I 44
45. for loop i j
1 1
#include <iostream>
#include <conio.h>
2
3
using namespace std; 2 1
2
int main() 3
{
int i,j;
3 1
2
for (i=1; i<=3; i++) 3
{
output
for (j=1; j<=3; j++)
cout << i << j << "t"; 11 12 13
cout << endl;
21 22 23
}
system(“pause”); 31 32 33
return 0;
}
Computer Programming I 45
46. for loop
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=0; i<=4; i++)
{
for (j=0; j<=4; j++) output
cout << "(" << i << "," << j << ")t";
cout << endl; (0,0) (0,1) (0,2) (0,3) (0,4)
} (1,0) (1,1) (1,2) (1,3) (1,4)
system(“pause”); (2,0) (2,1) (2,2) (2,3) (2,4)
return 0; (3,0) (3,1) (3,2) (3,3) (3,4)
} (4,0) (4,1) (4,2) (4,3) (4,4)
Computer Programming I 46
47. for loop
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=0; i<=4; i++)
{
for (j=0; j<=4; j++) i=j output i<j
cout << "(" << i << "," << j << ")t";
cout << endl; (0,0) (0,1) (0,2) (0,3) (0,4)
} (1,0) (1,1) (1,2) (1,3) (1,4)
system(“pause”); (2,0) (2,1) (2,2) (2,3) (2,4)
return 0; (3,0) (3,1) (3,2) (3,3) (3,4)
} i>j (4,0) (4,1) (4,2) (4,3) (4,4)
Computer Programming I 47
48. for loop
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
How to produce
?
int i,j;
for (i=0; i<=4; i++)
{ this output?
for (j=0; j<=4; j++)
cout << "(" << i << "," << j << ")t";
cout << endl;
} (1,0)
system(“pause”); (2,0) (2,1)
return 0; (3,0) (3,1) (3,2)
} (4,0) (4,1) (4,2) (4,3)
Computer Programming I 48
49. for loop
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j;
for (i=0; i<=4; i++)
{ for (j=0; j<=4; j++)
if (i>j)
output
cout << "(" << i << "," << j << ")t";
cout << endl;
} (1,0)
system(“pause”); (2,0) (2,1)
return 0; (3,0) (3,1) (3,2)
} (4,0) (4,1) (4,2) (4,3)
Computer Programming I 49
50. Multi-way if-else-statements
An if-else-statement is a two-way branch
Three or four (or more) way branches can be
designed using nested if-else-statements
Computer Programming I 50
51. Multi-way if-else
#include <iostream> if (no==1)
#include <conio.h> price=10;
else if (no==2)
using namespace std; price=9;
else if (no==3)
int main() price=7.5;
{ else if (no==4)
int no; price=6.5;
double price; else if (no==5)
double total; price=5.5;
else
cout << “Enter no. of students "; price=4.5;
cin >> no; total=no*price;
cout << "the total price is " << total;
system(“pause”);
return 0;
}
Computer Programming I 51
52. The switch-statement
The switch-statement is an alternative for
constructing multi-way branches
Computer Programming I 52
53. switch-statement Syntax
switch (controlling expression)
{
case Constant_1:
statement_Sequence_1
break;
case Constant_2:
Statement_Sequence_2
break;
...
case Constant_n:
Statement_Sequence_n
break;
default:
Default_Statement_Sequence
}
Computer Programming I 53
54. The break Statement
The break statement ends the switch-statement
Omitting the break statement will cause the
code for the next case to be executed!
Omitting a break statement allows the use of
multiple case labels for a section of code
case 'A':
case 'a':
cout << "Excellent.";
break;
Runs the same code for either 'A' or 'a'
Computer Programming I 54
55. The default Statement
If no case label has a constant that matches the
controlling expression, the statements following
the default label are executed
If there is no default label, nothing happens
when the switch statement is executed
It is a good idea to include a default section
Computer Programming I 55
56. Output:
switch (if-else to4switch)
Enter no. of students
the total<iostream> 26
#include price is //break will exit switch()
#include <conio.h> switch (no)
Note: 26 = 4 * 6.5 {
using namespace std; case 1: price=10.0; break;
case 2: price=9.0; break;
int main() case 3: price=7.5; break;
{ case 4: price=6.5; break;
int no; case 5: price=5.5; break;
double price; default: price=4.5;
double total; }
total=no*price;
cout << “Enter no. of students "; cout << "the total price is " << total;
cin >> no; return 0;
}
Computer Programming I 56
57. Output:
switch (if-else to 4
Enter no. of students
switch)
the total price is 18
#include <iostream> //break will exit switch()
#include <conio.h> switch (no)
{
Note: 18 = 4 * 4.5std;
using namespace case 1: price=10.0;
case 2: price=9.0;
int main() case 3: price=7.5;
{ case 4: price=6.5;
int no; case 5: price=5.5;
double price; default: price=4.5;
double total; }
total=no*price;
cout << “Enter no. of students "; cout << "the total price is " << total;
cin >> no; return 0;
}
Computer Programming I 57
58. #include <iostream>
switch #include <conio.h>
using namespace std;
int main() {
int no;
cout << "Please enter a number ";
cin >> no;
switch (no) //no break is used
{
case 1: cout << "1" << " your number is " << no << endl;
case 2: cout << "2" << " your number is " << no << endl;
case 3: cout << "3" << " your number is " << no << endl;
case 4: cout << "4" << " your number is " << no << endl;
case 5: cout << "5" << " your number is " << no << endl;
default:
cout << "No such number" << endl;
}
return 0;
}
Computer Programming I 58
59. #include <iostream>
switch #include <conio.h>
using namespace std;
int main() {
int no;
cout << "Please enter a number ";
cin >> no;
switch (no) //break will cause program to exit switch()
{
case 10: cout << "your number is 10" ; break;
case 20:
case 30: cout << "your number is 20 or 30" ; break;
case 40: cout << "your number is 40" ; break;
default: cout << "No such number" << endl;
}
return 0;
}
Computer Programming I 59
60. Static cast
Type casts are data conversions specified by the
programmer. A Type Cast produces a value of one type
from another type
var1 = static_cast <var1_type> (var2);
Example: int a = 5;
double b;
b = static_cast <double> (a);
b is now have a value 5.0 (note the decimal to indicate
floating point value)
Computer Programming I 60
61. Static cast
A problem with integer division:
int sugarInKg = 10, noOfPeople = 4;
double kgPerPerson;
kgPerPerson = sugarInKg / noOfPeople;
(2.0) (10) (4)
kgPerPerson should be 2.5, not 2.0!
Solutions:
1) declare sugarInKg as double OR
2) static_cast sugarInKg to double.
int sugarInKg = 10, noOfPeople = 4;
double kgPerPerson;
kgPerPerson = static_cast <double> (sugarInKg) / noOfPeople;
(2.5) (10.0) (4)
Computer Programming I 61
62. Predefined functions
C++ comes with libraries of predefined functions.
To make the functions available, the library must be
“included” in a program using include directive.
An include directive tells the compiler which
library header file to include.
Example: To use the sqrt function from the math library,
cmath, we need to define:
#include <cmath>
Computer Programming I 62
64. Predefined functions
Function call syntax:
functionName (argument1, argument2, …);
Example: sqrt function returns, or computes, the square root
of a number
answer = sqrt (9.0);
return value function name argument
answer now have a value of 3.0
Computer Programming I 64
65. Predefined functions
Function arguments can be
1) Constants: sqrt ( 4.0 );
from double
2) Variables: sqrt ( x ); data type
3) Expressions: sqrt ( sqrt ( x ) );
sqrt ( 3 – 6*x );
Computer Programming I 65
66. Predefined functions
pow (x, y)
Returns value of x to the power of y
Return value is of type double
Both arguments are of type double
Found in the library cmath
value = pow (2.0, 3.0);
return value function name argument 1 argument 2
Computer Programming I 66