1 / 13

Making Decisions (True or False)

Making Decisions (True or False). Relational Operators > greater than < less than >= greater than or equal to <= less than or equal to == equal to != not equal to Note: Remember to put spaces around operators for readability. Boolean Expressions.

veta
Download Presentation

Making Decisions (True or False)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Making Decisions(True or False) Relational Operators > greater than < less than >= greater than or equal to <= less than or equal to == equal to != not equal to Note: Remember to put spaces around operators for readability.

  2. Boolean Expressions Given: a = 4, b = -3, c = 0, d = 8 Evaluate the following boolean expressions as True or False Ex. a < b means is 4 < -3 and the answer is false a – b <= d – 1 ? a + b > c ? d/4 != c ? d – 2*a == c ?

  3. Comparison of non-integer Numbers Remember that floating point numbers may not be stored exactly. Avoid comparing the EQUALITY of two non-integer expressions. Use <= or >= where feasible.

  4. Value of True & False The boolean value ‘true’ is stored as 1. The boolean value ‘false’ is stored as 0. Boolean expressions evaluate to either ‘true’ or ‘false’ (i.e. 1 or 0) ONLY.

  5. Single-sided alternative (if… ) Syntax: if (test condition) { stuff to execute if test condition is true } OR (if there is only one statement to execute) if (test condition) statement to execute; Note: The body of the single-sided alternative is indented. As a matter of preference braces {} may be used even if there is only one statement.

  6. Examples int x, y, z; cout << “Enter three integers: “; cin >> x >> y >> z; if (x != 0) z = y / x; ________________________________________ (What is wrong here?) What about ? if (x = 0) cout >> “You cannot divide by 0”;

  7. Double Sided Alternative if (test condition) { statements to execute if the test condition evaluates to true } else { statements to execute if the test condition evaluates to false } or if (test condition) statement to execute (true leg); else statement to execute (false leg); Note: DO NOT mix {} and no {} even if one side of the double-sided alternative has only one statement. This is a style requirement!

  8. More Truth • true and false are boolean values and bool variables can be initialed Ex. bool ok; ok = false; • A relational value that is true has value 1 • A relational value that is false has value 0 • In if (test condition) structures, if the test condition evaluates to anything other than 0, it is considered true for decision making purposes. • Because real numbers are not necessarily stored exactly you should never use == to compare two floats, two doubles, etc.

  9. File Open Errors Filestream objects can be tested like a Boolean expression. This fact can be used to make sure that a file is actually opened successfully before the program tries to use its contents. ifstream fin; fin.open(“inputfile.txt”); if (fin) { process the file as the open was successful } else { display a message that the input file failed to open }

  10. Nested if Statements Where appropriate, ‘if’ statements of all kinds can be nested inside another ‘if’ statement. Note the indentation used in the syntax example. Syntax: if (test condition 1) { if (test condition2) { … } } else { … }

  11. Careful! • Indentation becomes even more critical • Braces MUST be paired correctly • Can become confusing so documentation of logic may be needed • Check to see if multi-layered embedded ‘if’ statements can be simplified with an if … else if …. else structure (next slide)

  12. if … else if … else if … else This structure may be useful if there are a series of conditions to be met. Note that it is just a cleaner appearing permutation of nested ifs. Syntax: if (test condition 1) { … statement(s) to be executed if test condition 1 is true } else if (test condition 2) { … statement(s) to be executed if test condition 1 is false and test condition 2 is true } else if (test condition 3) { … statement(s) to be executed if all previous test conditions are false and test condition 3 is true } else //optional { …statement(s) to be executed if all previous test conditions are false }

  13. Boolean Flags A ‘flag’ is a value that is used to signal when a particular condition occurs in a program. • Typically a flag is a boolean (bool) variable or named constant • A flag can also be of type int since 0 is false and all other int values are considered true

More Related