Microsoft Visual C++ Windows Applications by Example phần 2

Không phải là tác giả, xuất bản Packt, cũng không phải các đại lý hoặc nhà phân phối sẽ được tổ chức chịu trách nhiệm về bất kỳ thiệt hại nào gây ra hoặc bị cáo buộc gây ra trực tiếp hoặc gián tiếp bởi cuốn sách này. Packt Publishing đã cố gắng để cung cấp thông tin thương hiệu về tất cả các công ty và | Introduction to C In the examples above it is not strictly necessary to surround the output statements with brackets. However it would be necessary in the case of several statements. In this book brackets are always used. The brackets and the code in between is called a block. if i 0 int j i 1 cout j is j A warning may be in order. In an if statement it is perfectly legal to use one equals sign instead of two when comparing two values. As one equals sign is used for assignment not comparison the variable i in the following code will be assigned the value one and the expression will always be true. if i 1 Always true. . One way to avoid the mistake is to swap the variable and the value. As a value can be compared but not assigned the compiler will issue an error message if you by mistake enter one equals sign instead of two signs. if 1 i Compile-time error. . The switch statement is simpler than the if statement and not as powerful. It evaluates the switch value and jumps to a case statement with the same value. If no value matches it jumps to the default statement if present. It is important to remember the break statement. Otherwise the execution would simply continue with the code attached to the next case statement. The break statement is used to jump out of a switch or iteration statement. The switch expression must have an integral or pointer type and two case statements cannot have the same value. The default statement can be omitted and we can only have one default alternative. However it must not be placed at the end of the switch statement even though it is considered good practice to do so. switch i case 1 cout i is equal to 1 endl break 28 Chapter 1 case 2 cout i is equal to 2 endl break case 3 cout i is equal to 3 endl int j i 1 cout j j break default cout i is not equal to 1 2 or 3. endl break In the code above there will be a warning for the introduction of the variable j. As a variable is valid only in its closest surrounding scope the following code

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
10    64    2    29-04-2024
Đã 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.