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

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 4: Control Structures I (Selection)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Computer Science Department Relational Operators And Decisions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection Structures (if & switch statements) (CS1123)
Flow of Control Part 1: Selection
1 CSC103: Introduction to Computer and Programming Lecture No 7.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
TK 1914 : C++ Programming Control Structures I (Selection)
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 CS161 Introduction to Computer Science Topic #8.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
‘C’ Programming Khalid Jamal.
The if…else Selection Statement
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Decisions Given hours worked and pay rate, calculate total pay
The Selection Structure
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
DKT121: Fundamental of Computer Programming
Chapter 4: Making Decisions.
Decisions Given hours worked and pay rate, calculate total pay
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
Chapter 7 Conditional Statements
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Control Structure.
Programming Fundamental
Presentation transcript:

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

Relational Operators

Relational Operators

Relational Operators: Integer

Relational Operators: Float

Relational Operators: Char

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

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

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

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

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)

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

Order of Precedence

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.

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

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?

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

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

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

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)

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.

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 > 50000.00) interestRate = 0.07; else if (loanAmount >= 25000.00) interestRate = 0.05; if (loanAmount >= 1000.00) interestRate = 0.03; 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 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

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;

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

The switch Statement: Syntax

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."; 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

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.

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.

Conditional Operator (?:)

Conditional Operator (?:)

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

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

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;

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.