Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection statements - Võ Thị Ngọc Châu
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering
Chapter 4: Selection 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
if.. statements
if..else.. statements
Nested if../if..else.. statements
switch..case.. statements
Summary
4
Introduction
Recall
Statement
ended with a semicolon (;)
stretched on multiple lines with a backslash \ at the end
able to be grouped in the brackets {}
not consider spaces
Block
specified by {} with no semicolon after the right brace
contains as many statements as required
is a compound statement, syntactically equivalent to a
single statement
Sequentially processed from the beginning to the
5
end of a function
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])
Single
statement
iteration = iteration + 1;
else {
minNumber = positiveNumber[iteration];
Block
iteration = iteration + 1;
(Chapter 1 –
}
}
Real code in C)
}
6
Introduction
Control statements in C
Sequence
Assignment
Function calling
…
Selection
if
if..else..
switch..case..
Repetition
for..
while..
do..while..
7
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 Selection
}
Real code in C)
}
8
if.. statements
if (<Condition>) <Statement>
false (0)
<Condition>
if (<Condition> )
{
true ( 0)
<Statements>
}
<Statements>
<Statements> is performed (selected)
if <Condition> is true.
Otherwise, ignored.
if.. statements
false (0)
grade>=5.0
true ( 0)
printf(“Passed”);
Print “Passed” if grade >= 5.0.
Otherwise, ignored.
10
if..else.. statements
if (<Condition>) <Statement T>
else <Statement F>
false (0)
if (<Condition>) <Statement T>
else
{
<Condition>
<Statements F>
}
true ( 0)
if (<Condition> )
{
<Statements T>
<Statements F>
<Statements T>
}
else <Statement F>
if (<Condition> )
{
<Statements T>
}
else
{
<Statements T> is performed (selected)
if <Condition> is true.
<Statements F>
}
Otherwise, <Statements F> is performed.
if..else.. statements
false (0)
grade >= 5.0
true ( 0)
printf(“Passed”);
printf(“Failed”);
Print “Passed” if grade >= 5.0.
Otherwise, print “Failed”.
12
if..else.. statements
Conditional expression:
<condition>?<expression T>:<expression F>
can be regarded as:
if (<condition>) <expression T>;
else <expression F>;
13
if..else.. statements
Which one do you prefer:
conditional expressions or
if..else.. statements?
14
Nested if../if..else.. statements
if (<condition 1>)
if (<condition 1>)
{
{
…
…
if (<condition 2>) …
if (<condition 2>) …
…
…
}
}
else
{
…
if (<condition 3>) …
…
}
15
Nested if../if..else.. statements
16
Nested if../if..else.. statements
17
Nested if../if..else.. statements
A multi-way decision
if (<condition 1>) <statements T1>
else if (<condition 2>) <statements T2>
else if (<condition 3>) <statements T3>
…
else if (<condition k>) <statements Tk>
else <statements Fk>
18
Nested if../if..else.. statements
Be careful with specifying “else” for “which if”:
if (<condition 1>)
if (<condition 2>) <statements T2>
else <statements F>
should be:
if (<condition 1>) {
d = ? 5? 10? 20?
if (<condition 2>) <statements T2>
}
else <statements F1>
or:
if (<condition 1>) {
if (<condition 2>) <statements T2>
else <statements F2>
d = ? 5? 10? 20?
}
switch..case.. statements
false (0)
false (0)
false (0)
<case 1>
<case 2>
<case N>
true (0)
true (0)
true (0)
<Statements 1>
<Statements 2>
<Statements N>
<Default>
switch (<expression>) {
case <case 1>: <Statements 1>; break;
a multi-way decision
that tests whether an
expression matches one
of a number of constant
integer values, and
case <case 2>: <Statements 2>; break;
…
case <case N>: <Statements N>; break;
[default: <Default>]
branches accordingly
}
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 4: Selection 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

