program control repetition
TRANSCRIPT
-
8/12/2019 Program Control Repetition
1/33
-
8/12/2019 Program Control Repetition
2/33
Program Control: Repetition
Subject : T0016 ALGORITHM AND PROGRAMMING
Year : 2012
-
8/12/2019 Program Control Repetition
3/33
3
Learning Outcomes
At the end of this session, student will be able to:
Demonstrate usage of repetition/looping controloperation in C programming language (LO2 & LO3)
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
4/33
4
Sub Topics
Program Control - Repetition:
Repetition Definition
For
While Do-While
Repetition Operation
Break vs Continue
Program Examples
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
5/33
5
Repetition Definition
One or more instruction repeated for certain amount oftime
Number of repetition can be predefined (hard-coded in
program) or defined later at run time
Repetition/looping operation: for
while do-while
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
6/33
6
Repetition: FOR
Syntax:
for(exp1; exp2; exp3) statement;or:
for(exp1; exp2; exp3){statement1;statement2;.
}
exp1: initializationexp2: conditionalexp3: increment or decrementexp1, exp2 and exp3 are optional
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
7/33
7
Repetition: FOR
exp1 and exp3 can consist of several expression separated withcomma
Example:
void reverse(char ss[]){
int c,i,j;
for(i=0, j=strlen(ss)-1; i
-
8/12/2019 Program Control Repetition
8/33
8
Repetition: FOR
Flow Chart of FORStatement
T0016 - Algorithm andProgramming
true
false
statements
exp2
exp3
exp1
-
8/12/2019 Program Control Repetition
9/33
9
Repetition: FOR
Example:
for (x=1; x
-
8/12/2019 Program Control Repetition
10/33
10
Repetition: FOR
Example:
Program to print out numbers from 1 to 10
Program to print out numbers from 10 to 1
T0016 - Algorithm andProgramming
#include
int main()
{int x;
for( x =1 ; x=1 ; x--) printf( "%d\n", x);
return(0);
}
-
8/12/2019 Program Control Repetition
11/33
11
Repetition: FOR
Example:Calculating average of monthly expenses:
Week Expenses
1 Rp. 32.000,-
2 Rp. 29.000,-
3 Rp. 33.000,-
4 Rp. 24.000,-
Algorithm :
1. Create and empty total2. Read from keyboard and save to data
3. Add data to total
4. Repeat 2 and 3 four times
5. Average = total / 4T0016 - Algorithm and
Programming
-
8/12/2019 Program Control Repetition
12/33
12
Repetition: FOR
Example: (Calculating average of monthly expenses)
T0016 - Algorithm andProgramming
/*----------------------------------*/
/* Program Averaging */
/*----------------------------------*/
#include
int main(){
float data, total, average;
int x;
total = 0.0;
for( x = 1; x
-
8/12/2019 Program Control Repetition
13/33
13
Repetition: FOR
Infinite Loop
Loop with no stop condition can use for-loop byremoving all parameters (exp1, exp2, exp3). To end
the loop use break.
Nested Loop
Loop in a loop. The repetition operation will start from
the inner side loop.
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
14/33
14
Repetition: FOR
T0016 - Algorithm andProgramming
for (int x=1;x=1; y--)
printf(%d %d ,x,y);
Output :
1 5 1 4 1 3 .. 2 5 2 4 .. 5 1
int x, y;
for (x=1;x=1; y--)
printf(%d %d ,x,y);
C
C++
NESTED LOOP
-
8/12/2019 Program Control Repetition
15/33
15
Repetition: FOR
Example: (Truth Table)
T0016 - Algorithm andProgramming
/*-----------------------------------
Program: the truth table;
------------------------------------*/
#include
int main()
{
int P,Q;
printf(==============================\n);
printf(P Q P or Q P and Q Not P P xor Q\n);
printf(==============================\n);
for(P=1; P>=0; P--)
for(Q = 1; Q>=0; Q--)printf(%4d %4d %4d %4d %4d %4d\n, P, Q, P||Q, P&&Q, !P, P^Q
);
printf(==============================\n);
}
-
8/12/2019 Program Control Repetition
16/33
16
Repetition: WHILE
Syntax :
while (exp) statements;
or:
while(exp){
statement1;
statement2;
..
}
T0016 - Algorithm andProgramming
Example :int counter = 1;
while ( counter
-
8/12/2019 Program Control Repetition
17/33
-
8/12/2019 Program Control Repetition
18/33
-
8/12/2019 Program Control Repetition
19/33
19
Repetition: WHILE
Example :
while(product
-
8/12/2019 Program Control Repetition
20/33
20
Repetition: WHILE
T0016 - Algorithm andProgramming
#includevoid main() {
int x;
for(x = 1; x
-
8/12/2019 Program Control Repetition
21/33
21
Repetition: DO-WHILE
Syntax :
do{
< statements >;
} while(exp);
Keep executing while expis true
expevaluation done after executing the statement(s)
T0016 - Algorithm andProgramming
Example :int counter=0;
do {
printf( "%d ", counter );
++counter;
} while (counter
-
8/12/2019 Program Control Repetition
22/33
22
Repetition: DO-WHILE
Flow Chart of DO-WHILE Statement
T0016 - Algorithm andProgramming
true
false
statements
condition
-
8/12/2019 Program Control Repetition
23/33
23
Repetition: DO-WHILE
Example:
do{printf(%d\n,counter);
} while(++counter
-
8/12/2019 Program Control Repetition
24/33
24
Repetition Operation
In while operation, statement block of statements maynever be executed at all if exp value is false
In do-whileon the other hand statement block of
statements will be executed min once
To end the repetition, can be done through several ways:
Sentinel
Question, should the repetition continue?
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
25/33
25
Repetition Operation
Example: (Question)
T0016 - Algorithm andProgramming
#include
int main()
{
int width, height, area; char repeat;
printf(Continue ? (Y/N) :);scanf(%c,&repeat);
while((toupper(repeat)) == Y) {
printf( Width : );
scanf(%d,&width);
printf( Height : );
scanf(%d,&height);
area = width*height;
printf( Area = %d\n\n,area);
printf(Continue ?(Y/N):);
scanf(%c,&repeat);
}
return(0);
}
-
8/12/2019 Program Control Repetition
26/33
26
Repetition Operation
Example: (Sentinel)
As sentinel, used 0 for width or height
T0016 - Algorithm andProgramming
#include
int main()
{
int width, height, area;
do{
printf( Width : );
scanf(%d,&width);
printf( Height : );
scanf(%d,&height);area = width*height;
printf( Area = %d\n\n,area);
} while((width != 0) && (height != 0));
return(0);
}
-
8/12/2019 Program Control Repetition
27/33
27
Repetition Operation
T0016 - Algorithm andProgramming
#include
int main() {
int x = 1;
while (x
-
8/12/2019 Program Control Repetition
28/33
28
Break vs Continue
break:
ending loop (for, while and do-while)
end the switch operation
continue:
skip all the rest of statements (subsequent to the skipstatement) inside a repetition, and continue normally to
the next loop
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
29/33
-
8/12/2019 Program Control Repetition
30/33
30
Break vs Continue
Example using break:
T0016 - Algorithm andProgramming
#include
int main() {
int x;for(x=1; x
-
8/12/2019 Program Control Repetition
31/33
31
Example Labeling
Example using labeling:
T0016 - Algorithm andProgramming
#include
int main() {
int x;for(x=1; x
-
8/12/2019 Program Control Repetition
32/33
32
Summary
Repetition is a condition which is one or more instructionrepeated for certain amount of time
3 types of repetition/looping in C:
for while do-while
T0016 - Algorithm andProgramming
-
8/12/2019 Program Control Repetition
33/33
33
References
Paul J. Dietel,Harvey M. Deitel,. 2010. C : how to program. PEAPH.New Jersey. ISBN:978-0-13-705966-9 Chapter 3 & 4
Doing the Same Thing Over and Over:http://aelinik.free.fr/c/ch07.htm
T0016 - Algorithm andProgramming
http://aelinik.free.fr/c/ch07.htmhttp://aelinik.free.fr/c/ch07.htm