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.
Selection Used when we want the computer to choose between alternative actions. Assertion Statement 1BStatement 1A false true
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.
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?
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.
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)
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.
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.