The document discusses relational operators, logical operators, and control flow statements in C++. It defines common relational operators like <, >, ==, etc. and explains that they return true or false. It also covers logical operators like &&, ||, and ! and how they short-circuit evaluations. Finally, it describes control flow statements like if-else, switch, and the ternary operator (?:) for conditional expressions.
The document discusses relational operators, logical operators, and control flow statements in C++. It defines common relational operators like <, >, ==, etc. and explains that they return true or false. It also covers logical operators like &&, ||, and ! and how they short-circuit evaluations. Finally, it describes control flow statements like if-else, switch, and the ternary operator (?:) for conditional expressions.
are binary operators that compare the values of 2 expressions.
1. result of comparisons is true if condition being tested is satisfied. 2. result of comparison is false if condition begin tested is not satisfied. 3. result is of type bool. false is also equated with zero. true is also equated with non-zero.
example. (15 > 10) has the value of true (15 <= 10) has the value of false (5-3) is 2 but equates to true used in a relational operator because it is non-zero.
(7-3-4) is 0 and equates to false if used in a relational operator because it is exactly zero.
common errors == =
1. x == 10 /*test if x has value 10 */ 2. x = 10 /*assign 10 to x */
Precedence of operators Highest Associativity * / % left to right + - left to right < <= > >= left to right == != left to right Lowest = %= += -= *= /= right to left
Example:
1. x + 1 < y -2 if x = 1, y = 7 the addition and subtraction operators have higher precedence and are evaluated first. The problem is than visualized:
2 < 5
Since 2 is less than 5 the entire expression is true.
Logical operators in C
logical AND && OR || NOT !
result in bool (false or true).
AND (&&) exp1 && exp2
short circuited: evaluate exp1
if exp1 is false, then return false (does not evaluate exp2)
if exp1 is true, then evaluate exp2, return true if exp2 returns true, false otherwise.
example int a, b, c; a=b=c=10; 1. a<20 && b<11 =>true && true
2. a==11 && a++ false&&(not calculated) Since both operands must be true, the righ-hand operand is not calculated if the left-hand is false to save time. In this example a is never incremented. (This type of coding, while useful for demonstrating short-circuiting, is NOT recommended)
NOT ( ! ) ! exp 1. if exp is 0, then !exp returns 1 2. if exp is non-zero, then !exp returns 0
short circuited evaluation is useful as in
if ( x != 0 && y/x > 3.0) { ... }
Evaluation of logical expressions
int a,b,c;
a=b=c=10;
--a || --b && --c;
Evaluation proceeds left to right :
9 || (--b && --c) 1
=> a = 9, b = 10, c = 10
int a,b,c;
a=b=c=10;
--b && --c || --a;
=> a = 10, b = 9, c = 9
FLOW CHARTING
Flow charts are graphical method of showing the logical path through a program. Up until now all our programs have had a starting point, some basic procedures, and and ending. Ex:
int main() { int x;
cout<<"Enter x ->"; cin>>x; cout<<"You entered "<<x<<endl;
}
The above program could be represented in a graphical form using the following chart:
Note the ovals (rounded boxes actually, denote the start and stop of the program and that the order of the boxes specifies the order of operations of events.
Selection control structure: Conditional statements
Form1. simple if statement
syntax: if (exp) statement;
if (exp) { statement1; . . statementn; } Action. if exp is non-zero, statement is executed. if exp is 0, statement is skipped. A statement can be a single statement or compound statement in { } and every statement must end with ; (exp) parenthesis is required.
example #include <iostream> int main ( ) { int number;
cout<<" Enter a Number -> "; cin>>number; if (number < 0) number = -number; cout<<"absolute value = "<<number;
return 0; }
Note that the Diamond diagrams the test. If Number is less than zero, the flow goes one way, otherwise the flow goes another
Form 2. if_else statement syntax: if (exp) statement_1; else statement_2;
#include <iostream> int main( ) { int number, remainder; cout<<" Enter a Number -> "; cin>>number;
remainder = number % 2; if (remainder == 0) cout<<"Number is even"<<endl; else cout<<"Number is odd"<<endl; return 0; }
nested if if (college_student == 1 ) // if(college_student) also okay if (compSci == 1) cout<<"Happy Computing!"<<endl; same as if (college_student == 1 && compSci == 1 ) cout<<"Happy Computing!"<<endl;
Now, add an else clause: if (college_student == 1) if (compSci == 1) cout<<"Happy Computing!"<<endl; else cout<<"Ahh More time for fun"<<endl;
if we add another else clause:
if (college_student) if (compSci) cout<<"Happy Computing"<<endl; else cout<<"Ahh.. More time for fun"<<endl; else cout<<"Sign up today at Moorpark Coll...."<<endl;
1. An else clause is always associated with the last if statement that does not contain an else.
2. Use proper indentation.
if (college_student) if (compSci == 1) cout<<"Happy Computing"<<endl; else // WRONG!!! cout<<" Sign up today at Moorpark Coll"<<endl;
3. Need to use { } to associate an else clause with its intended if statement in the above case. if (college_student) { if (compSci) cout<<"Happy computing"<<endl; } else cout<<"Sign up today at Moorpark Coll"<<endl;
Special case of nested if-else statement where the condition for branching is a simple integer or char constant expression: switch statement.
1. No 2 const values can have the same values. 2. break causes an exit from the switch statement. 3. The switch expression can ONLY be integral (short, int, or char value).
/*program to evaluate a simple expression of the form: operand_1 operator operand_2 */
#include <iostream> int main ( ) { float op1, op2; char oper;
cout<<"Type in your expression -> "; cin>>op1>>oper>>op2; switch (oper) { case '+' : cout<<op1+op2; break; case '-' : cout<<op1-op2; break; case '*' : case 'x' : cout<<op1*op2; break; case '/' : if (op2 == 0) cout<<"Divide by zero\n"; else cout<<op1/op2; break; default : cout<<"unknown operator\n"; break; } return 1; }
1. condition is a relational expression that will be evaluated. 2. exp1 becomes result of operation if condition is true. 3. exp2 becomes result of operation if condtion is false.
4. max_value = (a > b) ? a : b; 5. Sometimes discouraged due to readability problem.