1 / 10

Conditions, Logical Expressions, and Selection Control Structures

Conditions, Logical Expressions, and Selection Control Structures. Robert reaves. Flow of Control. The order in which the computer executes statements in a program. Normally sequential. How to make it not sequential, we use control structures .

steve
Download Presentation

Conditions, Logical Expressions, and Selection Control Structures

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. Conditions, Logical Expressions, and Selection Control Structures Robert reaves

  2. Flow of Control • The order in which the computer executes statements in a program. • Normally sequential. • How to make it not sequential, we use control structures. • It is a statement used to alter the normally sequential flow of control.

  3. Selection • Used when we want the computer to choose between alternative actions. Assertion false true Statement 1A Statement 1B

  4. Bool Data Type • Built-in type consists of just TWO values, the constants true and false. • boolis short for boolean. • Used for testing conditions in a program so the computer can make a decision. • Ex: • booldataOK; • bool done; • bool taxable; • True and false are not variable names and they aren’t strings. They are special constants in C++, and are reserved words.

  5. Logical Expressions • These assertions are also called boolean expressions. • Assigning a Boolean variable: • done = true; // This is valid because we are assuming it is pre-declared. • Another way, with a relational operator: • lessThan = (5 < 3); // What do you think is in the variable lessThan?

  6. Relational Operator • == Equal to • != Not equal to • > Greater than • < Less than • >= Greater than or equal to • <= Less than or equal to • An expressions follow by a relational operator followed by an expression is called a relational expression.

  7. Relational Operator • Compare like types to avoid conflicts. • What do you think true and false represent in numerical form? • VERY VERYVERYVERYVERYVERYVERYVERYVERY IMPORTANT • (=) and (==) are COMPLETELY different. • (=) assigns a value to some variable. • (==) compares two variables. (equals-equals)

  8. Comparing Strings • myStr < yourStr// string object and string object, OKAY • myStr >= “Johnson” // string object and c string, OKAY • “NO” <= “STOP NOW” // cannot both be C strings, NOOOOOO • String comparisons begin with the first character of each string. • Continues to compare a character at a time, until a mismatch is found or the final characters have been compared and are equal. • Character values based on ASCII chart.

  9. If-then & if-then-else statement • if(expression) • statement1A • else • statement1B • Notice there are no (;) after the else or if! • Only used on simple statements such as assignment statements, input statements, and output statements.

More Related