Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition statements - Võ Thị Ngọc Châu
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.. Statements
for.. Statements
Nested Repetition Statements
continue Statements
break Statements
Summary
4
Introduction
Control statements in C
Sequence
Assignment
Function calling
…
Selection
if
if..else..
switch..case..
Repetition
for..
while..
do..while..
5
Introduction
void main() {
Given a
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n
positive
numbers,
find the
smallest
one.
int n = 10;
double minNumber = positiveNumber[0];
int iteration = 1;
while (iteration < n) {
if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
else {
minNumber = positiveNumber[iteration];
iteration = iteration + 1;
}
(Chapter 1 –
}
Control Statements for Repetition
Real code in C)
}
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)
<Condition>
while (<Condition>) {
<Statement1>;
true ( 0)
<Statements>
…
<Statementk>;
}
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.
8
4. End the repetition statement and move forward.
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
Fibonacci numbers smaller than N.
do..while.. Statements
do {
<Statement1>;
<Statements>
…
<Statementk>;
true(0)
<Condition>
}
while (<Condition>);
false(=0)
<Statements> are always
performed at least once!
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.
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
}
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Introduction to Computer Programming (C language) - Chapter 5: Repetition statements - Võ Thị Ngọc Châu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_introduction_to_computer_programming_c_language_ch.pdf

