Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 12 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
12
Dung lượng
253 KB
Nội dung
C++ Programming Lecture Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides) Outline Introduction switch statement Examples The Hashemite University Introduction In this lecture we will study the last control structure in C++ This control structure is associated with selection It is used when there are multiple selection choices, i.e more than It can be used instead of nested if/else structure The Hashemite University The switch MultipleSelection Structure switch switch (variable or expression) Useful when variable or { expression is tested for multiplecase value1: //do something; values break; case value2: //do Consists of a series of case labels and an optional default something; break; case case value3: //do Used instead of nested if/else something; break; statements to make the code case value4: //do something; more readable and easier to break; trace default: // something The Hashemite University break; Flowchart Representation of switch case a true case a action(s) break case b action(s) break case z action(s) break false case b true false case z true false default action(s) The Hashemite University 5 10 11 12 13 14 15 16 17 18 19 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // Fig 2.22: fig02_22.cpp // Counting letter grades #include using std::cout; using std::cin; using std::endl; int main() { char grade, aCount = bCount = cCount = dCount = fCount = 0, 0, 0, 0, 0; // one grade // number of A's // number of B's // number of C's // number of D's // number of F's cout