1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Lecture 4 selection statements

32 1 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Selection statements
Tác giả TS. Võ Thị Ngọc Châu
Trường học Ho Chi Minh City University of Technology
Chuyên ngành Computer Science and Engineering
Thể loại Giáo trình
Năm xuất bản 2017 – 2018
Thành phố Ho Chi Minh City
Định dạng
Số trang 32
Dung lượng 1,32 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 4: Selection Statements chauvtn@hcmut.edu.vn... References Deitel and Harvey Deitel, Prentice Hall, 2012... Introduction  ended with a semicolon ;  stretched on multiple lines

Trang 1

Chapter 4: Selection Statements

chauvtn@hcmut.edu.vn)

Trang 3

References

Deitel and Harvey Deitel, Prentice Hall, 2012

– Brian W Kernighan and Dennis M Ritchie, Prentice Hall, 1988

3

Trang 5

Introduction

 ended with a semicolon (;)

 stretched on multiple lines with a backslash \ at the end

 able to be grouped in the brackets {}

 not consider spaces

 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

Trang 6

double minNumber = positiveNumber[0];

iteration = iteration + 1;

} } }

Block Single statement

Trang 8

double minNumber = positiveNumber[0];

iteration = iteration + 1;

} } }

Control Statements for Selection

Trang 12

Print “Passed” if grade >= 5.0

Otherwise, print “Failed”

Trang 14

if else statements

Which one do you prefer: conditional expressions or if else statements?

Trang 15

Nested if /if else statements

Trang 16

Nested if /if else statements

Trang 17

Nested if /if else statements

17

Trang 18

Nested if /if else statements

Trang 19

Nested if /if else statements

Trang 20

switch (<expression>) {

case <case 1>: <Statements 1>; break;

case <case 2>: <Statements 2>; break;

of a number of constant

integer values, and branches accordingly

Trang 21

switch case statements

21

switch (<expression>) {

case <case 1>: <Statements 1>; break;

case <case 2>: <Statements 2>; break;

case <case N>: <Statements N>; break;

[default: <Default>]

}

if (<expression> == <case 1>) <Statements 1>

else if (<expression> == <case 2>) <Statements 2>

Trang 22

switch case statements

enumerated data, characters

of the aforementioned types

through to the next unless an explicit action is

taken to escape

 break (return) statement

Trang 23

true (0) true (0) false (0) false (0) false (0)

Trang 24

switch (<expression>) {

case <case 1>: <Statements 1>

case <case 2>: <Statements 2>

Trang 25

switch case statements

25

switch (<expression>) {

case <case 1>: <Statements 1>

case <case 2>: <Statements 2>

Trang 26

true (0) true (0) false (0) false (0) false (0)

switch case statement with “fall-through” property

Trang 27

27

Trang 29

Put them all together

week Input a day in a week and output its corresponding activities

 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)

 char *strcpy(char *destination, const char *source)

29

Trang 30

Add more codes to make such an input valid (?!)

Trang 31

according to a “TRUE” ( 0) value of a

condition (expression)

in programming

31

Trang 32

Chapter 4: Selection Statements

Ngày đăng: 11/04/2023, 18:54

w