Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu

Content

 Introduction

 if. statements

 if.else. statements

 Nested if./if.else. statements

 switch.case. statements

 Summary

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 1

Trang 1

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 2

Trang 2

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 3

Trang 3

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 4

Trang 4

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 5

Trang 5

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 6

Trang 6

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 7

Trang 7

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 8

Trang 8

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 9

Trang 9

Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu trang 10

Trang 10

Tải về để xem bản đầy đủ

pdf 32 trang xuanhieu 5260
Bạn đang xem 10 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

Tóm tắt nội dung tài liệu: Bài giảng Introduction to Computer Programming (C language) - Chapter 4: Selection Statements - Võ Thị Ngọc Châu

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 
 (chauvtn@cse.hcmut.edu.vn, 
 chauvtn@hcmut.edu.vn) 
 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 
 end of a function 5 
 Introduction 
 Given a void main() { 
 double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12}; 
 set of n 
 int n = 10; 
 positive double minNumber = positiveNumber[0]; 
 int iteration = 1; 
 numbers, 
 while (iteration < n) { 
 find the if (minNumber <= positiveNumber[iteration]) 
 Single 
 iteration = iteration + 1; 
 smallest statement 
 else { 
 one. 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 
 Given a void main() { 
 double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12}; 
 set of n 
 int n = 10; 
 positive double minNumber = positiveNumber[0]; 
 int iteration = 1; 
 numbers, 
 while (iteration < n) { 
 find the if (minNumber <= positiveNumber[iteration]) 
 iteration = iteration + 1; 
 smallest 
 else { 
 one. minNumber = positiveNumber[iteration]; 
 iteration = iteration + 1; 
(Chapter 1 – 
 } Control Statements for Selection 
Real code in C) } 
 } 
 8 
if.. statements 
 if () 
 false (0) 
 if ( ) 
 { true ( 0) 
 } 
 is performed (selected) 
 if is true. 
 Otherwise, ignored. 9 
if.. statements 
 false (0) 
 grade>=5.0 
 true ( 0) 
 printf(“Passed”); 
 Print “Passed” if grade >= 5.0. 
 Otherwise, ignored. 
 10 
if..else.. statements 
if () 
else 
if () false (0) 
else 
{ 
} 
 true ( 0) 
if ( ) 
{ 
} 
else 
if ( ) 
{ 
} 
else is performed (selected) 
{ 
 if is true. 
} 
 Otherwise, is performed.11 
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: 
 ?: 
 can be regarded as: 
 if () ; 
 else ; 
 13 
if..else.. statements 
 Which one do you prefer: 
 conditional expressions or 
 if..else.. statements? 
 14 
Nested if../if..else.. statements 
if () if () 
{ { 
 if ()  if ()  
} } 
 else 
 { 
 if ()  
 } 
 15 
Nested if../if..else.. statements 
 16 
Nested if../if..else.. statements 
 17 
Nested if../if..else.. statements 
 A multi-way decision if () 
 else if () 
 else if () 
 else if () 
 else 
 18 
Nested if../if..else.. statements 
Be careful with specifying “else” for “which if”: 
if () 
 if () 
else 
should be: 
if () { 
 d = ? 5? 10? 20? 
 if () 
} 
else 
or: 
if () { 
 if () 
 else 
 d = ? 5? 10? 2019? 
} 
 switch..case.. statements 
 false (0) false (0) false (0) 
 true ( 0) true ( 0) true ( 0) 
switch () { a multi-way decision 
 case : ; break; that tests whether an 
 case : ; break; 
 expression matches one 
 of a number of constant 
 case : ; break; 
 integer values, and 
 [default: ] 
 branches accordingly 
} 20 
switch..case.. statements 
 switch () { 
 case : ; break; 
 case : ; break; 
 case : ; break; 
 [default: ] 
 } 
 can be regarded as: 
 if ( == ) 
 else if ( == ) 
 else if ( == ) 
 [else ] 
 21 
 switch..case.. statements 
 has a type of integer numbers, 
 enumerated data, characters. 
 , , are constants of one 
 of the aforementioned types. 
  Cases serve as labels. 
 [default: ] is optional. 
 “fall-through” property of switch..case.. 
  After the code for one case is done, execution falls 
 through to the next unless an explicit action is 
 taken to escape. 
 break (return) statement 
 22 
switch..case.. statements 
 false (0) false (0) false (0) 
 aChar==„a‟ aChar==„b‟  
 true ( 0) true ( 0) true ( 0) 
 printf „a‟; printf „b‟; printf 
 break; break; default; 
 23 
switch..case.. statements 
 false (0) false (0) false (0) 
 true ( 0) true ( 0) true ( 0) 
 switch..case.. statement with “fall-through” property 
 switch () { 
 case : 
 case : 
 case : 
 [default: ] 
 } 24 
 switch..case.. statements 
switch..case.. statement with “fall-through” property 
 can be regarded as: 
 if ( == ) { 
switch () { 
 case : 
 case :  
 case : } 
 [default: ] else if ( == ) { 
} 
 } 
 else if ( == ) { 
 } 
 [else ] 25 
switch..case.. statements 
 false (0) false (0) false (0) 
 aChar==„a‟ aChar==„b‟  
 true ( 0) true ( 0) true ( 0) 
 printf 
 printf „a‟; printf „b‟;  
 default; 
 switch..case.. statement with “fall-through” property 
 26 
27 
28 
Put them all together 
 Given a problem: build your timetable in a 
 week. Input a day in a week and output its 
 corresponding activities. 
 string.h: a standard library file for strings 
  Compare two strings 
 int strcmp(const char *str1, const char *str2) 
 . < 0 if str1 < str2 (less than) 
 . > 0 if str1 > str2 (greater than) 
 . = 0 if str1 = str2 (equal) 
  Copy a string to another one 
 char *strcpy(char *destination, const char *source) 
 29 
Add more codes to make such an input valid (?!) 
 30 
Summary 
 Control statements for selection 
  if.. statements 
  if..else.. statements 
  switch..case.. statements 
 Statements can be selected for execution 
 according to a “TRUE” ( 0) value of a 
 condition (expression). 
 Selection statements play an important role 
 in programming. 
 31 
Chapter 4: Selection Statements 
 32 

File đính kèm:

  • pdfbai_giang_introduction_to_computer_programming_c_language_ch.pdf