tailieunhanh - Absolute C++ (4th Edition) part 7

Absolute C++ (4th Edition) part 7. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | Branching Mechanisms 61 cout Hello from the second if. n else cout Hello from the else. n cout End again n 12. What output will be produced by the following code int extra 2 if extra 0 cout small else if extra 0 cout medium else cout large 13. What would be the output in Self-Test Exercise 12 if the assignment were changed to the following int extra -37 14. What would be the output in Self-Test Exercise 12 if the assignment were changed to the following int extra 0 15. Write a multiway if-else statement that classifies the value of an int variable n into one of the following categories and writes out an appropriate message. n 0 or 0 n 100 or n 100 THE switch STATEMENT The switch statement is the only other kind of C statement that implements multi- switch way branches. Syntax for a switch statement and a simple example are shown in the statement accompanying box. When a switch statement is executed one of a number of different branches is exe- controlling cuted. The choice of which branch to execute is determined by a controlling expres- expression sion given in parentheses after the keyword switch. The controlling expression for a switch statement must always return either a bool value an enum constant discussed later in this chapter one of the integer types or a character. When the switch statement is executed this controlling expression is evaluated and the computer looks at the constant values given after the various occurrences of the case identifiers. If it finds a constant that equals the value of the controlling expression it executes the code for that case. You cannot have two occurrences of case with the same constant value after them because that would create an ambiguous instruction. 62 Flow of Control switch Statement Syntax switch Controlling_Expression case Constant_1 Statement_Sequence_1 break case Constant_2 Statement_Sequence_2 break You need not place a break statement in each case. If you omit a break that case continues until a break or the end

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG