201Spring08-L8

201Spring08-L8 - CMPSC 201C Spring 2008 Lecture 8 February...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 8 February 1, 2008 Flow of Control There are 4 types of control structures that affect the "flow of control" of the program, i.e. in what order the statements are executed. Sequence - statements are executed in order they are written Branched/Selection (decision) - some statements may be executed only when a specific condition is met. Loops (repetition) - statements are repeated Functions- a group of statements are executed at various times in the program Branch (Decision) false Condition true Statement(s) Statement(s) Relational Operators Used to compare, or relate, two values (variables or constants) == equal to (do not confuse with =) != not equal to > greater than < less than >= greater than or equal to <= less than or equal to If used with char variables the ASCII equivalents are compared. Conditions Evaluate to true of false x < y alpha >= beta a + b >10 Note that the relational operators are binary operators they require two operands When testing for equality, make sure that you use == , not =. ( x = y) will assign the value of y to x and may evaluate to true or false depending on the value of y. Combining Relational Expressions In C++ relational operators only evaluate 2 operands, therefore the expression 5 < x < 10 is invalid in C++. What the above expression is really stating is that 5 is less than x and x is less than 10, a combination of 2 expressions. To combine or negate relational expressions, logical operators are employed. Logical Operators The mathematical operator AND is represented as && in C++ and means both relationships (conditions) must be true. The mathematical operator OR is represented as || in C++ and means either relationship (conditions) may be true. The mathematical operator NOT is represented as ! in C++ and complements, negates or reverses, the result of an expression Logical operators have lower precedence than relational operators. Truth Tables List possible combinations plus the resultant true/ false value Cond 1 Cond 2 Cond 1 && Cond 2 T T T T F F F T F F F F Truth Tables (Cont.) Cond 1 T T F F Cond 1 T F Cond 2 T F T F !(Cond 1) F T Cond 1 || Cond 2 T T T F Complementing a Condition The complement, or reverse condition may be accomplished by using the logical not operator, !, or by using a different realtional operator. Consider the condition (a > b) . Two compliments are !(a > b) and (a <= b) (note the opposite of > is <= ) What would be the complements of (x >= y)? Caution Be careful using the NOT operator because it has high precedence and may not mean what you first think. (Refer to table 3.4 on page 77) !x > y is not the same as !(x > y) !(x > y) is not the same as (x < y) !((x == y) && (z == u)) is the same as (x != y) || (z != u) !((a < b) || (c > d)) is the same as (a >= b) && (c <= d) Short Circuit Evaluation If multiple conditions are combined with logical operators, evaluation of the conditions is stopped as soon as overall truth can be determined. Therefore not all conditions may be evaluated. Consider: (x < 5) || (y >10) (c >= b) && (c < a) Questions ????? Decision with One Branch Statement(s) are executed when a condition is true. "Simple If" General Format if (condition) { statement(s) } Statements inside braces are called a block and should be indented Example //test of reasonable input value if (radius < 0 ) { cout<<"A negative number is not valid!" <<endl<<"Please enter a new value" <<endl; cin>>radius; } Items to Note If only one statement is to be executed when the condition is true, the braces may be omitted and the statement may occur on the same line. if (x > 100) x = x- 100; // or even if (alpha < beta) cout<<"alpha is less than beta \n"; If more than one statement is to be executed, braces must be used. Otherwise only the first statement is part of the if. Statements that belong to the if should be indented. Questions ????? Decision with Two Branches Sometimes we would like to take one action if a condition is true and another if the condition is false. Use If/Else structure if (condition) { statement(s) //to be executed if true } else { statement(s)//to be executed if false } Example if (grade >= 70) { cout<<"Congratulations! You are passing" <<endl; } else { cout<<"You need to work harder"<<endl; } Complement Expressions if (grade <70) { cout<<"You need to work harder"<<endl; } else { cout<<"Congratulations! You are passing" <<endl; } Questions??? ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online