Relational Operators Relational operators are used to compare two numeric values and create a...
-
Upload
gary-mckenzie -
Category
Documents
-
view
226 -
download
2
Transcript of Relational Operators Relational operators are used to compare two numeric values and create a...
![Page 1: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/1.jpg)
Relational Operators
• Relational operators are used to compare two numeric values and create a boolean result.– The result is dependent upon the
relationship between the two operators.– Relational operators are evaluated after all
arithmetic operators.
![Page 2: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/2.jpg)
Relational Operators
Operator Expression Interpretation> x > y if x > y then true< x < y if x < y then true>= x >= y if x >= y then true<= x <= y if x <= y then true
== x == y if x == y then true!= x != y if x != y then true
Equality Operators
![Page 3: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/3.jpg)
Relational Operators
4 > 6
7 + 3 / 2 == 5 + 5 / 2
Assume a = 4 and x = 9, a <= b
![Page 4: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/4.jpg)
Logical Operators
• Logical Operators are used to compare boolean values and create a boolean result.– Logical operators are usually used to compare
the results of relational operators.– Logical operators are evaluated after all
relational operators.– Logical operators are && (logical And), & (boolean
And), || (logical Or), | (boolean Or), ^ (boolean Exclusive Or), and ! (logical Not).
![Page 5: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/5.jpg)
Logical Operators
• && (logical And):
Expression1 Expression2 Expression1 && Expression2
True True True
True False False
False True False
False False False
![Page 6: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/6.jpg)
Logical Operators
• II (logical Or):
Expression1 Expression2 Expression1 || Expression2
True True True
True False True
False True True
False False False
![Page 7: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/7.jpg)
Logical Operators
• && vs. & and || vs. |– Both && and || will only evaluate the
expressions until it knows a result while the & and | operators will evaluate all the expressions before they return the result.
– Gender == 1 || age >= 65– Gender == 1 | age >= 65
![Page 8: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/8.jpg)
Logical Operators
• ^ (logical Exclusive Or):
Expression1 Expression2 Expression1 ^ Expression2
True True False
True False True
False True True
False False False
![Page 9: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/9.jpg)
Logical Operators
• ! (logical Not):
Expression1 ! Expression1
True False
False True
![Page 10: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/10.jpg)
Logical Operators
• (4 >= 7) && (3 + 4 == 7)
• (4 >= 7) && (3 + 4 == 7) || (4 < 7)
• (4 >= 7) && (3 + 4 == 7) || (4 < 7) && true
![Page 11: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/11.jpg)
Control Structures
• Control structures are used to organize actions (statements).
• Examples:– Sequence Structure– Selection Structure– Repetition Structure
![Page 12: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/12.jpg)
Sequence Structures
public static void main(String[] args)
{
MainWindow mainWindow =
new MainWindow;
OutputBox outputBox = new
OutputBox(mainWindow);
outputBox.printLine(
“Welcome to”);
outputBox.printLine(
"Java Programming!”);
}
outputBox.printLine(“Welcome to”);
outputBox.printLine(“Java Programming!”);
MainWindow mainWindow = new MainWindow();
OutputBox outputBox = new OutputBox(mainwindow);
![Page 13: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/13.jpg)
Selection Structures
• Selection Structures allow you to write code that will select and execute specific code statements instead of other code statements.
![Page 14: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/14.jpg)
If Selection Structure
• The basic structure of an if statement in Java is:if (boolean_expression) {
statement;
}
![Page 15: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/15.jpg)
If Selection Structure
• If the boolean_expression is true then the statement is executed.
• If the boolean_expression is false then the statement is skipped and the next statement in the sequence is executed.
![Page 16: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/16.jpg)
If Selection Structure
If (hungry == true) {
outputBox.printLine(“find some food”);
}
outputBox.printLine(“find some food”);
hungry == true
False
True
![Page 17: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/17.jpg)
If Selection Structure
• Let’s create the statements for the following problem: We want to categorize grades such that we will print out the corresponding letter grade. Assume <60 = F, 60 – 69 = D, 70 - 79 = C, 80 - 89 = B, and >90 = A.
![Page 18: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/18.jpg)
If/else Selection Structure
• The if/else statement allows you to write code that executes one statement if the boolean_expression is true and different statement if the boolean_expression is false.
![Page 19: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/19.jpg)
If/else Selection Structure
• The basic structure of an if/else statement in Java is:if (boolean_expression) }
statement 1;
} else {
statement 2;
}
![Page 20: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/20.jpg)
If/else Selection Structure
If (hungry == true) {
outputBox.printLine(“find some food”);
} else {
outputBox.printLine(“get more work done”);
}
outputBox.printLine(“find some food”);Hungry == true
False TrueoutputBox.printLine(“get more work done);
![Page 21: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/21.jpg)
If/else Selection Structure• The statement inside an if/else structure may be
another if/else statement. In Java it looks like:if (boolean_expression_1) {
statement1;
} else {
if (boolean_expression_2) {
statement2;
} else {
statement 3;
}
}
![Page 22: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/22.jpg)
If/else Selection Structure
• It may also be written as:if (boolean_expression_1) {
statement1;
} else if (boolean_expression_2) {
statement2;
} else {
statement3;
}
![Page 23: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/23.jpg)
If/else Selection Structure
• Let’s look at our grade program again and rewrite it using the if/else structure.
![Page 24: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/24.jpg)
If/else Selection Structure
• The if/else and if statements we have worked with so far execute only one statement. We can also execute multiple statements using compound statements.– Compound statements are actually multiple
statements enclosed in brackets { }
![Page 25: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/25.jpg)
If/else Selection Structures• The format for an if/else statement which
includes a compound statement in Java is:if (boolean_expression) {
statement1;
statement2;
} else {
statement3;
statement4;
statement5;
statement6;
}
![Page 26: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/26.jpg)
Nesting If Statements• Nesting if statements makes your
program more powerful because it can handle many different situations.– Nesting occurs when one or more if
structures are inside of another if statement.– The else statement is always associated with
the previous if statement unless { } are used to change the associativity.
• Dangling else
![Page 27: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/27.jpg)
Nested If Statements• What is printed when the following is
evaluated: if (y == 8)
if (x == 5)
outputBox.printLine(“1”);
else
outputBox.printLine(“2”);
outputBox.printLine(“3”);
outputBox.printLine(“4”);
![Page 28: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/28.jpg)
The Conditional Operator
• The conditional operator is basically a short cut to the if/else structure.– It is called a ternary operator because it
has three arguments that form the conditional expression.
(boolean_expression? true_operation :
false_operation)
![Page 29: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/29.jpg)
The Conditional Operator
If (grade > 70) {
outputBox.printLine (“C”);
} else {
outputBox.printLine(“F”);
}
outputBox.printLine(grade > 70 ? “C” : “F”);
![Page 30: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/30.jpg)
The Conditional Operator
Write the conditional operator for the following if/else statement.
boolean a = true, b = false, c = true;
if ((a && c) && c || b) {
outputBox.printLine(“true”);
} else {
outputBox.printLine(“false”);
}
![Page 31: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/31.jpg)
The Conditional OperatorWrite the conditional operator for the following if/else
statement.If (grade >= 90) {
outputBox.printLine(“The grade is an A”);
} else if (grade >= 80) {
outputBox.printLine(“The grade is an B”);
} else if (grade >= 70) {
outputBox.printLine(“The grade is an C”);
} else {
outputBox.printLine(“The grade is an F”);
}
![Page 32: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/32.jpg)
Switch Selection Structure
• The Switch selection structure is basically short hand for multiple if/else statements. It allows you to perform statements for a specific value of a variable when there are multiple possibilities.– The switch expression must be an integer
or char result.
![Page 33: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/33.jpg)
Switch Selection Structureswitch (switch_expr) {
case item_1:
statement1;
statement2; …
break;
case item_2:
statement1; …
break;
default:
statement 1; …
break;
}
![Page 34: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/34.jpg)
Switch Selection Structureswitch ( grade ) {
case 'A':
++aCount;
break;
case 'B':
++bCount;
break;
...
default:
outputBox.printLine( "Incorrect grade. Enter new grade." );
break;
}
++aCountCase A’
False
Truebreak
++bCountCase B’
False
True break
...
outputBox.printLine ...default
False
Truebreak
![Page 35: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/35.jpg)
Switch Selection Structure
• The switch should always use the break statement for each case or the structure will not work properly.
• Your switch statements should always have a default case for completeness purposes.
• Anything you can represent with a switch you can represent as a nested if/else statement.
![Page 36: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/36.jpg)
Switch Selection Structure
Write a program to determine the cost of products sold in the last week at a mail order house. They have five products whose retail prices are: product 1 - $2.98; product 2 - $4.50; Product 3 - $9.98; Product 4 - $4.49; Product 5 - $6.87
The program should ask the user to enter the product number and the quantity sold for one day. The program should use a switch statement to determine the total retail price for each product.
![Page 37: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/37.jpg)
Switch Selection Structure
• What if you have multiple values you want to test for and have them execute the same “case”?
![Page 38: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/38.jpg)
Switch Selection Structureswitch (switch_expr) {
case item_1: item_2:
statement1;
statement2; …
break;
case item_3:
statement1; …
break;
default:
statement 1; …
break;
}
![Page 39: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/39.jpg)
Switch Selection Structure
• Write a program to read in letter grades and calculate how many of each letter are entered.
![Page 40: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/40.jpg)
ListBox Class
• A ListBox allows a program to present multiple alternative inputs to the user in a well-defined manner.– A ListBox must be associated with a
MainWindow.MainWindow mainWindow = new MainWindow();
ListBox listBox = new ListBox(mainWindow);
![Page 41: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/41.jpg)
ListBox Class
• When a ListBox is created it has nothing in it. The program must add items to the list.listBox.addItem(“Item One”);– As many items as needed can be added to
the list.
![Page 42: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/42.jpg)
ListBox Class• To retrieve the user’s input use the
getSelectedIndex() method.int selection = listBox.getSelectedIndex();
– The first item in the list has an index of zero (0).– If the user makes no selection, then the result is
ListBox.NO_SELECTION.– If the user cancels the input, then the result is
ListBox.CANCEL.
![Page 43: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/43.jpg)
Color Class
• Java represents colors using individual red, green, and blue components combined to create a color.– Each component is a value between 0 and
255.• Black = (0,0,0)• White = (255, 255, 255)
![Page 44: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/44.jpg)
Color Class
• The color class also provides some predefined colors. (see page 276)– To use the predefined colors you reference
them by their name:• Color.magenta
– To change the color of an object:objectName.setColor(Color.pink);
![Page 45: Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.](https://reader036.fdocuments.net/reader036/viewer/2022062517/56649f125503460f94c25080/html5/thumbnails/45.jpg)
DrawingBoard Class
• The DrawingBoard class can be used to draw shapes.– To draw a line:
objectName.drawLine( x1, y1, x2, y2);
– To draw a circle:objectName.drawCircle(centerX, centerY, radius);
– To draw a rectangle:objectName.drawRectangle(x, y, width, height);