Đang chuẩn bị liên kết để tải về tài liệu:
Chapter 2 Flow of Control

Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG

Recall: ANY valid C++ statements can be inside body of loop This includes additional loop statements! Called "nested loops". | Chapter 2 Flow of Control Learning Objectives Boolean Expressions Building, Evaluating & Precedence Rules Branching Mechanisms if-else switch Nesting if-else Loops While, do-while, for Nesting loops Boolean Expressions: Display 2.1 Comparison Operators Logical Operators Logical AND (&&) Logical OR (||) Evaluating Boolean Expressions Data type bool Returns true or false true, false are predefined library consts Truth tables Display 2.2 next slide Evaluating Boolean Expressions: Display 2.2 Truth Tables Display 2.3 Precedence of Operators (1 of 4) Display 2.3 Precedence of Operators (2 of 4) Display 2.3 Precedence of Operators (3 of 4) Display 2.3 Precedence of Operators (4 of 4) Precedence Examples Arithmetic before logical x + 1 > 2 || x + 1 2 || (x + 1) = 0) && (y > 1) Be careful with increment operators! (x > 1) && (y++) Integers as boolean values All non-zero values true Zero value false Branching Mechanisms if-else . | Chapter 2 Flow of Control Learning Objectives Boolean Expressions Building, Evaluating & Precedence Rules Branching Mechanisms if-else switch Nesting if-else Loops While, do-while, for Nesting loops Boolean Expressions: Display 2.1 Comparison Operators Logical Operators Logical AND (&&) Logical OR (||) Evaluating Boolean Expressions Data type bool Returns true or false true, false are predefined library consts Truth tables Display 2.2 next slide Evaluating Boolean Expressions: Display 2.2 Truth Tables Display 2.3 Precedence of Operators (1 of 4) Display 2.3 Precedence of Operators (2 of 4) Display 2.3 Precedence of Operators (3 of 4) Display 2.3 Precedence of Operators (4 of 4) Precedence Examples Arithmetic before logical x + 1 > 2 || x + 1 2 || (x + 1) = 0) && (y > 1) Be careful with increment operators! (x > 1) && (y++) Integers as boolean values All non-zero values true Zero value false Branching Mechanisms if-else statements Choice of two alternate statements based on condition expression Example: if (hrs > 40) grossPay = rate*40 + 1.5*rate*(hrs-40); else grossPay = rate*hrs; if-else Statement Syntax Formal syntax: if () else Note each alternative is only ONE statement! To have multiple statements execute in either branch use compound statement Compound/Block Statement Only "get" one statement per branch Must use compound statement { } for multiples Also called a "block" stmt Each block should have block statement Even if just one statement Enhances readability Compound Statement in Action Note indenting in this example: if (myScore > yourScore) { cout << "I win!\n"; wager = wager + 100; } else { cout << "I wish these were golf scores.\n"; wager = 0; } Common Pitfalls Operator "=" vs. operator "==" One means "assignment" (=) One means "equality" (==) VERY different in C++! Example: if (x = 12) Note operator used! Do_Something else .

Đã 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.