600 likes | 961 Views
Relational Operators. A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point number User-defined classes
E N D
Relational Operators • A relational operator compares two values. The values can be any built-in C++ data type, such as • Character • Integer • Floating point number • User-defined classes • The result of the comparison is true or false; for example, either two values are equal (true), or they’re not (false).
Logical (Boolean) Operators C++ has three logical (Boolean) operators, as shown in following table
Operator ! (not) When we use the ! operator, !true is false and !false is true. Putting ! in front of a logical expression reverses the value of that logical expression.
Operator && (AND) Expression1 && Expression2 is true if and only if both Expression1 and Expression2 are true; otherwise, Expression1 && Expression2 evaluates to false.
Operator | | (OR) Expression1 || Expression2 is true if and only if at least one of the expressions, Expression1 or Expression2, is true; otherwise, Expression1 || Expression2 evaluates to false.
Short-circuit evaluation of a logical expression • A process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known. (x > y) || (x == 5)
Short-circuit evaluation of a logical expression No need to evaluate this suppose x = 7 and y = 5 (x > y) || (x == 5) evaluate to true But if ( x > y ) evaluate to false then we need to evaluate (x == 5)
Short-circuit evaluation of a logical expression (a == b) && (x >= 7) Then no need to evaluate this evaluate to false But if (a == b) evaluate to true then we need to evaluate (x >= 7)
Order of Precedence To work with complex logical expressions, there must be some priority scheme for evaluating operators. For Example
Control Structures • A computer can process a program in different ways for example
The if Statement The if statement is the simplest of the decision statements. The statements following the if are executed only once if the test expression is true
The if Statement int main() { int number, temp; cout << "Line 1: Enter an integer: "; cin >> number; cout << endl; temp = number; if (number < 0) number = -number; cout << “Absolute value of“<< temp << " is " << number; return 0; }
The if Statement: single statement The if structures control only one statement at a time.
The if Statement: compound statements To permit more complex statements, C++ provides a structure called a compound statement or a block of statements.
Output? If anila_age = 19 ali_age = 23 The if Statement int main() { int anila_age, ali_age; cout << “Plz enter anila’s age: "; cin >> anila_age<<endl; cout << “Plz enter ali’s age: "; cin >> ali_age<<endl; if (anila_age > ali_age) cout<<“Anila is older than ALI”; cout<<“ALI is older than Anila”; return 0; }
The if Statement What's wrong with program output? int main() { int anila_age, ali_age; cout << “Plz enter anila’s age: "; cin >> anila_age<<endl; cout << “Plz enter ali’s age: "; cin >> ali_age<<endl; if (anila_age > ali_age) cout<<“Anila is older than ALI”; cout<<“ALI is older than Anila”; return 0; } Output? If anila_age = 23 ali_age = 19 Can We find any solution?
Still Any Problem with output? The if Statement int main() { int anila_age, ali_age; cout << “Plz enter anila’s age: "; cin >> anila_age<<endl; cout << “Plz enter ali’s age: "; cin >> ali_age<<endl; if (anila_age > ali_age) cout<<“Anila is older than ALI”; if (ali_age > anila_age) cout<<“ALI is older than Anila”; return 0; }
The if Statement int main() { int anila_age, ali_age; cout << “Plz enter anila’s age: "; cin >> anila_age<<endl; cout << “Plz enter ali’s age: "; cin >> ali_age<<endl; if (anila_age > ali_age) cout<<“Anila is older than ALI”; if (ali_age > anila_age) cout<<“ALI is older than Anila”; if (ali_age = = anila_age) cout<<“Both are age fellow”; return 0; }
The if Statement: Home Task # 1 Write a program to determines an employee’s weekly wages. If the hours worked exceed 40, wages include overtime payment with the rate 30 PKR per extra hour. Take hours and over time rate as input from key board. Also do this with if else statement.
Nested if statements • When one control statement is located within another, it is said to be nested. If (a > b ) if ( a > c ) cout<< “a is the largest number”; If (b > a ) if ( b > c ) cout<< “b is the largest number”; If (c > a ) if ( c > b ) cout<< “c is the largest number”;
If statement using Logical AND • When one control statement is located within another, it is said to be nested. If ( (a > b) && (a > c)) cout<< “a is the largest number”; If ((b > a) && (b > c)) cout<< “c is the largest number”; If ((c > a) && (c > b) ) cout<< “c is the largest number”;
More Interesting Examples Nested If Statements With if and logical operators int m1, m2, m3, m4, m5, per ; cout<<"Enter marks in five subjects"; cin>>m1>>m2>>m3>>m4>>m5; per = ( m1 + m2 + m3 + m4 + m5 )/5; if ( per >= 60 ) cout<<“First division"; if ( per >= 50 ) if ( per < 60 ) cout<<"Second division" ; if ( per >= 40 ) if ( per < 50 ) cout<<"Third division"; if ( per < 40 ) cout<<“Fail"; int m1, m2, m3, m4, m5, per ; cout<<"Enter marks in five subjects"; cin>>m1>>m2>>m3>>m4>>m5; per = ( m1 + m2 + m3 + m4 + m5 )/5; if ( per >= 60 ) cout<<“First division"; if ( ( per >= 50 ) && ( per < 60 ) ) cout<<"Second division" ; if ( ( per >= 40 ) && ( per < 50 ) ) cout<<"Third division"; if ( per < 40 ) cout<<“Fail";
Nested if...Statement : home Task # 2 • Any character is entered through the keyboard, write a program to determine whether the character entered through the keyboard is a lower case alphabet or not. (using nested if statement) • Repeat above problem using Logical AND operator
The if...else Statement • There are many programming situations in which we must choose between two alternatives. • For example, if a part-time employee works overtime, the paycheck is calculated using the overtime payment formula; otherwise, the paycheck is calculated using the regular formula.
The if...else Statement (Two-Way Selection) It consists of an if statement, followed by a statement or block of statements, followed by the keyword else, followed by another statement or block of statements.
The if...else Statement int main() { int x; cout << “\n Enter a number: “; cin >> x; if( x > 100 ) cout << “That number is greater than 100\n”; else cout << “That number is not greater than 100\n”; return 0; }
The if...else Statement : Class Task • If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary. Draw Flowchart First
The if...else Statement : Class Task Convert it to C++ Program
The if...else Statement : Class Task • Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.
The if...else Statement : Home Task • If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.
HOME Task Write a program to calculate the salary as per the following table:
Nested if ….. else • Some problems require the implementation of more than two alternatives. • For example: Suppose that if the loan taken by a customer is more than PKR 50,000, the interest rate is 7%; if the loan amount is between PKR 25,000 and PKR 49,999.99, the interest rate is 5%; if the amount is between PKR 1,000 and PKR 24,999.99, the interest rate is 3%; otherwise the interest rate is 0%. • This particular problem has four alternatives—that is, multiple selection paths.
Suppose that loanAmountand interestRateare variables of type double. The following statements determine the interestRate depending on the value of the loan amount taken. if (loanAmount> 50000.00) interestRate= 0.07; else if (loanAmount>= 25000.00) interestRate= 0.05; else if (loanAmount>= 1000.00) interestRate= 0.03; else interestRate= 0.00; Nested if ….. else
To avoid excessive indentation, the code in previous example can be rewritten as follows: if (loanAmount > 50000.00) interestRate = 0.07; else if (balance >= 25000.00) interestRate = 0.05; else if (balance >= 1000.00) interestRate = 0.03; else interestRate = 0.00; Nested if ….. else
Nested if ….. else • In nested if….. else structure How do you know which else is paired with which if? • Remember that in C++, there is no stand-alone else statement. Every else must be paired with an if. • Pairing an else with an if: In a nested if statement, C++ associates an else with the most recent incomplete if
Assume that score is a variable of type int. Based on the value of score, the following code outputs the grade of a student. if (score >= 90) cout << "The grade is A." << endl; else if (score >= 80) cout << "The grade is B." << endl; else if (score >= 70) cout << "The grade is C." << endl; else if (score >= 60) cout << "The grade is D." << endl; else cout << "The grade is F." << endl; Nested if ….. else
Comparing if...else Statements with a Series of if Statements if (month == 1) cout<< "January" << endl; else if (month == 2) cout<< "February" << endl; else if (month == 3) cout<< "March" << endl; else if (month == 4) cout<< "April" << endl; else if (month == 5) cout<< "May" << endl; else if (month == 6) cout<< "June" << endl; if (month == 1) cout<< "January" << endl; if (month == 2) cout<< "February" << endl; if (month == 3) cout<< "March" << endl; if (month == 4) cout<< "April" << endl; if (month == 5) cout<< "May" << endl; if (month == 6) cout<< "June" << endl;
The switch Statement • If you have a large decision tree, and all the decisions depend on the value of the same variable, you will probably want to consider a switchstatement instead of a ladder of if...else or else if constructions.
The switch Statement: Rules • In C++, switch, case, break, and default are reserved words. • In a switch structure, first the expression is evaluated. The value of the expression is then used to perform the actions specified in the statements that follow the reserved word case. • A particular case value should appear only once.
The switch Statement: Rules • One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement. • The break statement may or may not appear after each statement. • When the value of the expression is matched against a case value (also called a label), the statements execute until either a break statement is found or the end of the switch structure is reached.
The switch Statement: Rules • If the value of the expression does not match any of the case values, the statements following the default label execute. • If the switch structure has no default label and if the value of the expression does not match any of the case values, the entire switch statement is skipped. • A break statement causes an immediate exit from the switch structure. • Don’t forget the break; without it, control passes down (or “falls through”) to the statements for the next case
The switch Statement: Example switch (grade) { case 'A': cout << "The grade point is 4.0."; break; case 'B': cout << "The grade point is 3.0."; break; case 'C': cout << "The grade point is 2.0."; break; case 'D': cout << "The grade point is 1.0."; break; case 'F': cout << "The grade point is 0.0."; break; default: cout << "The grade is invalid."; }