conditions, logical expressions, and selection control structures robert reaves

10
Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

Upload: suzanna-parsons

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

Conditions, Logical Expressions, and Selection Control StructuresROBERT REAVES

Page 2: 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. It is a statement used to alter the normally sequential flow of control.

Page 3: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

Selection

Used when we want the computer to choose between alternative actions.

Assertion

Statement 1B

Statement 1A

false true

Page 4: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

Bool Data Type

Built-in type consists of just TWO values, the constants true and false. bool is short for boolean.

Used for testing conditions in a program so the computer can make a decision.

Ex: bool dataOK;

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.

Page 5: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

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?

Page 6: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

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.

Page 7: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

Relational Operator

Compare like types to avoid conflicts.

What do you think true and false represent in numerical form?

VERY VERY VERY VERY VERY VERY VERY VERY VERY IMPORTANT (=) and (==) are COMPLETELY different.

(=) assigns a value to some variable.

(==) compares two variables. (equals-equals)

Page 8: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

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.

Page 9: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

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.

Page 10: Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES