Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.

Similar presentations


Presentation on theme: "Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point."— Presentation transcript:

1 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).

2 Relational Operators

3 Relational Operators

4 Relational Operators: Integer

5 Relational Operators: Float

6 Relational Operators: Char

7 Logical (Boolean) Operators
C++ has three logical (Boolean) operators, as shown in following table

8 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. A !A true false

9 Operator && (AND) Expression1 && Expression2 is true if and only if both Expression1 and Expression2 are true; otherwise, Expression1 && Expression2 evaluates to false. A B A && B true false

10 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. A B A && B true false

11 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)

12 Short-circuit evaluation of a logical expression
No need to evaluate this evaluate to true suppose x = 7 and y = 5 (x > y) || (x == 5) But if ( x > y ) evaluate to false then we need to evaluate (x == 5)

13 Short-circuit evaluation of a logical expression
evaluate to false Then no need to evaluate this (a == b) && (x >= 7) But if (a == b) evaluate to true then we need to evaluate (x >= 7)

14 Order of Precedence To work with complex logical expressions, there must be some priority scheme for evaluating operators. For Example

15 Order of Precedence

16 Control Structures A computer can process a program in different ways for example

17 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

18 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; }

19 The if Statement: single statement
The if structures control only one statement at a time.

20 The if Statement: compound statements
To permit more complex statements, C++ provides a structure called a compound statement or a block of statements.

21 The if Statement Output? If anila_age = 19 ali_age = 23 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; }

22 What's wrong with program output? Can We find any solution?
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; } What's wrong with program output? Output? If anila_age = 23 ali_age = 19 Can We find any solution?

23 Still Any Problem with output?
The if Statement Still Any Problem with 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”; if (ali_age > anila_age) cout<<“ALI is older than Anila”; return 0; }

24 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; }

25 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.

26 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”;

27 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) )

28 With if and logical operators
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";

29 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

30 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.

31 The if...else Statement (Two-Way Selection)

32 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.

33 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; }

34 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

35 The if...else Statement : Class Task
Convert it to C++ Program

36 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.

37 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.

38 HOME Task Write a program to calculate the salary as per the following table:

39 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.

40 Nested if ….. else Suppose that loanAmount and interestRate are variables of type double. The following statements determine the interestRate depending on the value of the loan amount taken. if (loanAmount > ) interestRate = 0.07; else if (loanAmount >= ) interestRate = 0.05; if (loanAmount >= ) interestRate = 0.03; interestRate = 0.00;

41 Nested if ….. else To avoid excessive indentation, the code in previous example can be rewritten as follows: if (loanAmount > ) interestRate = 0.07; else if (balance >= ) interestRate = 0.05; else if (balance >= ) interestRate = 0.03; else interestRate = 0.00;

42 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

43 Nested if ….. else 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;

44 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;

45 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 switch statement instead of a ladder of if...else or else if constructions.

46 The switch Statement: Syntax

47 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.

48 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.

49 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

50 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."; case 'C': cout << "The grade point is 2.0."; case 'D': cout << "The grade point is 1.0."; case 'F': cout << "The grade point is 0.0."; default: cout << "The grade is invalid."; } The switch Statement: Example

51 Conditional Operator (?:)
Certain if. . .else statements can be written in a more concise way by using C++’s conditional operator. The conditional operator, written as ? : and is a ternary operator, which means that it takes three arguments. The syntax for using the conditional operator is: expression1 ? expression2 : expression3 This type of statement is called a conditional expression.

52 Conditional Operator (?:)
expression1 ? expression2 : expression3 The conditional expression is evaluated as follows If expression1 evaluates to true (that is, nonzero integer), the result of the conditional expression is expression2. Otherwise, the result of the conditional expression is expression3.

53 Conditional Operator (?:)

54 Conditional Operator (?:)

55 Conditional Operator (?:)
alpha = 67; int result = 0, beta = 30, gamma= 90; cout<< “result =“ << result;

56 Conditional Operator (?:)
alpha = 78; int result = 0, beta = 30, gamma= 90; cout<< “result =“ << result;

57 Conditional Operator (?:) Class Task
Write a statement that uses a conditional operator to find the absolute value of a variable n. Solution ? absvalue = n<0 ? -n : n;

58 Home work A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not. Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees. Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter. Any character is entered through the keyboard, write a program to determine whether a character entered through the keyboard is a special symbol or not.


Download ppt "Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point."

Similar presentations


Ads by Google