TAILIEUCHUNG - A Complete Guide to Programming in C++ part 14

A Complete Guide to Programming in C++ part 14. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | CONDITIONAL EXPRESSIONS 109 Conditional Operator The conditional operator is used to form an expression that produces either of two values depending on the value of some condition. Because the value produced by such an expression depends on the value of a condition it is called conditional expression. In contrast to the if-else statement the selection mechanism is based on expressions one of two possible expressions is selected. Thus a conditional expression is often a concise alternative to an if-else statement. Syntax expression expressionl expression2 expression is evaluated first. If the result is true expressionl is evaluated if not expression2 is executed. The value of the conditional expression is therefore either the value of expressionl or expression2. Example z a 0 a -a This statement assigns the absolute value of a to the variable z. If a has a positive value of 12 the number 12 is assigned to z. But if a has a negative value for example - 8 the number 8 is assigned to z. Since this sample program stores the value of the conditional expression in the variable z the statement is equivalent to if a 0 z a else z -a Precedence The conditional operator is the only C operator with three operands. Its precedence is higher than that of the comma and assignment operators but lower than all other operators. In other words you could omit the brackets in the first example. You can use the result of a conditional evaluation without assigning it as the sample program on the opposite page shows. In this example x is printed on screen if x is greater than y and y is printed otherwise. However you should assign the result of complex expressions to a variable explicitly to improve the readability of your program. 110 CHAPTER 6 CONTROL FLOW SELECTING WITH switch Structogram for the switch statement Example Evaluates given input. int command menu switch command case a case A action1 break case b case B action2 break default cout a The function menu reads a command. Evaluate

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.