Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering
Chapter 5: Repetition Statements
Introduction to Computer Programming
(C language)
TS. Võ Thị Ngọc Châu
(,
)
2017 – 2018, Semester 2
Course Content
C.1. Introduction to Computers and
Programming
C.2. C Program Structure and its
Components
C.3. Variables and Basic Data Types
C.4. Selection Statements
C.5. Repetition Statements
C.6. Functions
C.7. Arrays
C.8. Pointers
C.9. File Processing
2
References
[1] “C: How to Program”, 7th Ed. – Paul
Deitel and Harvey Deitel, Prentice Hall, 2012.
[2] “The C Programming Language”, 2nd Ed.
– Brian W. Kernighan and Dennis M. Ritchie,
Prentice Hall, 1988
and others, especially those on the Internet
3
Content
Introduction
while..
Statements
do..while..
for..
Statements
Statements
Nested
Repetition Statements
continue
break
Statements
Statements
Summary
4
Introduction
Control statements in C
Sequence
Selection
Assignment
Function calling
…
if
if..else..
switch..case..
Repetition
for..
while..
do..while..
5
Introduction
Given a
void main() {
set of n
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
positive
double minNumber = positiveNumber[0];
numbers,
int iteration = 1;
int n = 10;
while (iteration < n) {
find the
if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
smallest
else {
one.
minNumber = positiveNumber[iteration];
iteration = iteration + 1;
(Chapter 1 –
}
}
Real code in C)
Control Statements for Repetition
}
6
Introduction
A repetition statement allows you to specify
that an action is to be repeated while some
condition remains true.
Example 1: Input validation
Ask a user to input a value
While his/her input value is invalid, ask him/her
to input a value.
Example 2: Search for any of you who didn’t
submit homework (i.e. no attachment exists)
Example 3: Count the number of occurrences
of an important keyword in a text
7
while.. Statements
while (<Condition>) <Statement>;
false (= 0)
while (<Condition>) {
<Statement1>;
…
<Statementk>;
<Condition>
true ( 0)
<Statements>
}
1. <Condition> is evaluated.
2. If <Condition> is true (0), <Statements> are performed
and then go to step 3. Otherwise, i.e. if <Condition> is false
(=0), go to step 4.
3. Go back to step 1.
4. End the repetition statement and move forward.
8
while.. Statements
false (= 0)
i<=3
true ( 0)
printf(…);
i++;
false (= 0)
i>=3
true ( 0)
printf(…);
i++;
9
while.. Statements
Write a program to receive a natural number from a user. If an invalid
value is entered, ask the user to input again until a valid one is obtained.
10
while.. Statements
Given N, the number of the first natural numbers
greater than 0, calculate and print the total sum of
these N first natural numbers.
11
while.. Statements
Given N, a natural number greater than 0, calculate and print the
factorial of N: N! = 1*2*..*N = (N-1)!*N
12
Given a natural number N
greater than 0, list a series of
while.. Statements
Fibonacci numbers smaller than N.
13
do..while.. Statements
do {
<Statement1>;
…
<Statements>
<Statementk>;
}
true(0)
<Condition>
while (<Condition>);
false(=0)
<Statements> are always
1. <Statements> are performed.
2. <Condition> is evaluated.
3. If <Condition> is true (0), go to step 1. Otherwise, i.e. if
<Condition> is false (=0), go to step 4.
4. End the repetition statement and move forward.
performed at least once!
14
do..while.. Statements
printf(…);
i++;
true(0)
i<=3
false(=0)
printf(…);
i++;
true(0)
i>=3
false(=0)
do..while.. Statements
Write a program to receive a natural number from a user. If an invalid
value is entered, ask the user to input again until a valid one is obtained.
16
do..while.. Statements
Given N, the number of the first natural numbers
greater than 0, calculate and print the total sum of
these N first natural numbers.
17
do..while.. Statements
Given N, a natural number greater than 0, calculate and print the
factorial of N: N! = 1*2*..*N = (N-1)!*N
18
Given a sequence of N numbers input sequentially
by a user, find the minimum one.
19
for.. Statements
<expression_1>
false (=0)
<expression_2>
true (0)
<Statements>
<expression_3>
for (<expression_1>; <expression_2>; <expression_3>) <Statement>;
for (<expression_1>; <expression_2>; <expression_3>) {
<Statement1>;
…
<Statementk>;
}
20
for.. Statements
<expression_1>
false (=0)
<expression_2>
true (0)
<Statements>
<expression_3>
1.
<expression_1> is evaluated.
2.
<expression_2> is evaluated.
3.
If <expression_2> is true (0), <Statements> are performed,
<expression_3> is evaluated, and then go to step 2. Otherwise, i.e.
if <expression_2> is false (=0), go to step 4.
4.
End the repetition statement and move forward.
21
for.. Statements
for (<expression_1>; <expression_2>; <expression_3>) <Statement>;
is regarded as:
<expression_1>;
while (<expression_2>) {
<Statement>;
<expression_3>;
}
for (<expression_1>; <expression_2>; <expression_3>) {
<Statement1>;
…
<Statementk>;
}
is regarded as:
<expression_1>;
while (<expression_2>) {
<Statement1>;
…
<Statementk>;
<expression_3>;
}
22
for.. Statements
i=1;
false (=0)
i<=3
true (0)
printf(…);
i++;
No printing result exists due to a false value of the expression “i>=3”.
23
for.. Statements
Given N, the number of the first natural numbers greater than 0,
calculate and print the total sum of these N first natural numbers.
24
for.. Statements
Given N, a natural number greater than 0, calculate and print the
factorial of N: N! = 1*2*..*N = (N-1)!*N
25