1 / 23

Logic: Flow of Control, Selection, and Iteration

Learn about the flow of control in computer programs, including selection and iteration. Understand how to use logical expressions, boolean data types, and relational operators. Explore the if statement and compound statements.

garzal
Download Presentation

Logic: Flow of Control, Selection, and Iteration

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. Chapter 5 Logic; Got Any?

  2. Flow of Control • Flow of Control • The order in which the computer executes statements in a program • Control Structure • A statement used to alter the normally sequential flow of control • Selection (Chapter 5) • Iteration (Chapter 6)

  3. Selection • Choosing between one or more options • Example: • If statements • Switch statements

  4. Boolean Data • We state an assertion and check it’s validity • If the assertion is true, we do one thing. • Else we do some other thing. • We use bool data types • Two values • true and false

  5. Logical Expressions • Boolean Variables and Constants • eventFound = false; • Relational Operators • lessThan = ( i < j );

  6. Operator == != > < >= <= Relationship Tested Equality Inequality Greater Than Less Than Greater Than or Equal To Less Than or Equal To Relational Operators

  7. Logical Operators • Three operators • And && • Both relationship must be true for the and to be true • Or || • One relationship must be true for the or to be true • Not ! • Inverts the relationships truth value

  8. And Truth Table

  9. Or Truth Table

  10. Not Truth Table

  11. Short Circuit • If the value of a statement can be determined before the entire statement must be evaluated, than do it

  12. Precedence of Operators

  13. The if Statement • If-Then-Else Form if ( Expression ) Statement A else Statement B

  14. Expressions and Statements • An expression is a statement that evaluates to a logic value • A statement is what you want the computer to do

  15. Example if ( hours <= 40.0 ) pay = rate * hours; else pay = rate * ( 40.0 + ( hours -40.0 ) * 1.5); cout << pay;

  16. Compound Statements • Not limited to one statement • Use { and } to indicate statements that are to be grouped together

  17. Example if ( divisor != 0 ) { result = dividend / divisor; cout << “Division performed.\n”; } else { cout << “Division by zero is not allowed\n”; result = 9999; }

  18. If-Then Form • Sometimes there is only one choice • Use: if ( Expression ) Statement

  19. Nested If’s • You can stack ifs as deep as you want if ( month == 1 ) cout << “January”; else if ( month == 2 ) cout << “February”; … else cout << “December”;

  20. Dangling Else • Where does the else go? if ( average < 70.0 ) if ( average < 60.0 ) cout << “FAILING”; else cout << “Barely Passing ”;

  21. How about this one if ( average >= 60.0 ) if ( average < 70.0 ) cout << “Barely Passing ”; else cout << “FAILING”;

  22. Correct Version if ( average >= 60.0 ) { if ( average < 70.0 ) cout << “Barely Passing ”; } else cout << “FAILING”;

  23. Testing streams • You can test whether the last operation performed on a stream failed or not • if ( cin ) • if ( cin.fail() ) • if ( !inFile ) • if ( !inFile.fail() )

More Related