Chapter 2 Flow of Control

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 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 next slide Evaluating Boolean Expressions: Display Truth Tables Display Precedence of Operators (1 of 4) Display Precedence of Operators (2 of 4) Display Precedence of Operators (3 of 4) Display 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 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 next slide Evaluating Boolean Expressions: Display Truth Tables Display Precedence of Operators (1 of 4) Display Precedence of Operators (2 of 4) Display Precedence of Operators (3 of 4) Display 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 + *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 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
476    17    1    27-11-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.