Relational Operators Relational operators are used to compare

  • Slides: 45
Download presentation
Relational Operators • Relational operators are used to compare two numeric values and create

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.

Relational Operators

Relational Operators

Relational Operators 4>6 7 + 3 / 2 == 5 + 5 / 2

Relational Operators 4>6 7 + 3 / 2 == 5 + 5 / 2 Assume a = 4 and x = 9, a <= b

Logical Operators • Logical Operators are used to compare boolean values and create a

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

Logical Operators • && (logical And): Expression 1 True False Expression 2 True False

Logical Operators • && (logical And): Expression 1 True False Expression 2 True False Expression 1 && Expression 2 True False

Logical Operators • II (logical Or): Expression 1 True False Expression 2 True False

Logical Operators • II (logical Or): Expression 1 True False Expression 2 True False Expression 1 || Expression 2 True False

Logical Operators • && vs. & and || vs. | – Both && and

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

Logical Operators • ^ (logical Exclusive Or): Expression 1 True False Expression 2 True

Logical Operators • ^ (logical Exclusive Or): Expression 1 True False Expression 2 True False Expression 1 ^ Expression 2 False True False

Logical Operators • ! (logical Not): Expression 1 True False ! Expression 1 False

Logical Operators • ! (logical Not): Expression 1 True False ! Expression 1 False True

Logical Operators • (4 >= 7) && (3 + 4 == 7) || (4

Logical Operators • (4 >= 7) && (3 + 4 == 7) || (4 < 7) && true

Control Structures • Control structures are used to organize actions (statements). • Examples: –

Control Structures • Control structures are used to organize actions (statements). • Examples: – Sequence Structure – Selection Structure – Repetition Structure

Main. Window main. Window = new Main. Window(); Output. Box output. Box = new

Main. Window main. Window = new Main. Window(); Output. Box output. Box = new Output. Box( mainwindow); output. Box. print. Line(“ Welcome to”); output. Box. print. Line( “Java Programming!”); Sequence Structures public static void main(String[] args) { Main. Window main. Window = new Main. Window; Output. Box output. Box = new Output. Box(main. Window); output. Box. print. Line( “Welcome to”); output. Box. print. Line( "Java Programming!”); }

Selection Structures • Selection Structures allow you to write code that will select and

Selection Structures • Selection Structures allow you to write code that will select and execute specific code statements instead of other code statements.

If Selection Structure • The basic structure of an if statement in Java is:

If Selection Structure • The basic structure of an if statement in Java is: if (boolean_expression) { statement; }

If Selection Structure • If the boolean_expression is true then the statement is executed.

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.

If Selection Structure hungry == true False True output. Box. print. Line (“find some

If Selection Structure hungry == true False True output. Box. print. Line (“find some food”); If (hungry == true) { output. Box. print. Line(“find some food”); }

If Selection Structure • Let’s create the statements for the following problem: We want

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.

If/else Selection Structure • The if/else statement allows you to write code that executes

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.

If/else Selection Structure • The basic structure of an if/else statement in Java is:

If/else Selection Structure • The basic structure of an if/else statement in Java is: if (boolean_expression) } statement 1; } else { statement 2; }

If/else Selection Structure If (hungry == true) { output. Box. print. Line(“find some food”);

If/else Selection Structure If (hungry == true) { output. Box. print. Line(“find some food”); } else { output. Box. print. Line(“get more work done”); } output. Box. print. Line( “get more work done); False Hungry == true True output. Box. print. Line(“ find some food”);

If/else Selection Structure • The statement inside an if/else structure may be another if/else

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) { statement 1; } else { if (boolean_expression_2) { statement 2; } else { statement 3; } }

If/else Selection Structure • It may also be written as: if (boolean_expression_1) { statement

If/else Selection Structure • It may also be written as: if (boolean_expression_1) { statement 1; } else if (boolean_expression_2) { statement 2; } else { statement 3; }

If/else Selection Structure • Let’s look at our grade program again and rewrite it

If/else Selection Structure • Let’s look at our grade program again and rewrite it using the if/else structure.

If/else Selection Structure • The if/else and if statements we have worked with so

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

If/else Selection Structures • The format for an if/else statement which includes a compound

If/else Selection Structures • The format for an if/else statement which includes a compound statement in Java is: if (boolean_expression) { statement 1; statement 2; } else { statement 3; statement 4; statement 5; statement 6; }

Nesting If Statements • Nesting if statements makes your program more powerful because it

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

Nested If Statements • What is printed when the following is evaluated: if (y

Nested If Statements • What is printed when the following is evaluated: if (y == 8) if (x == 5) output. Box. print. Line(“ 1”); else output. Box. print. Line(“ 2”); output. Box. print. Line(“ 3”); output. Box. print. Line(“ 4”);

The Conditional Operator • The conditional operator is basically a short cut to the

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)

The Conditional Operator If (grade > 70) { output. Box. print. Line (“C”); }

The Conditional Operator If (grade > 70) { output. Box. print. Line (“C”); } else { output. Box. print. Line(“F”); } output. Box. print. Line(grade > 70 ? “C” : “F”);

The Conditional Operator Write the conditional operator for the following if/else statement. boolean a

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) { output. Box. print. Line(“true”); } else { output. Box. print. Line(“false”); }

The Conditional Operator Write the conditional operator for the following if/else statement. If (grade

The Conditional Operator Write the conditional operator for the following if/else statement. If (grade >= 90) { output. Box. print. Line(“The grade is an A”); } else if (grade >= 80) { output. Box. print. Line(“The grade is an B”); } else if (grade >= 70) { output. Box. print. Line(“The grade is an C”); } else { output. Box. print. Line(“The grade is an F”); }

Switch Selection Structure • The Switch selection structure is basically short hand for multiple

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.

Switch Selection Structure switch (switch_expr) { case item_1: statement 1; statement 2; … break;

Switch Selection Structure switch (switch_expr) { case item_1: statement 1; statement 2; … break; case item_2: statement 1; … break; default: statement 1; … break; }

Switch Selection Structure Case A’ True ++a. Count break ++b. Count break output. Box.

Switch Selection Structure Case A’ True ++a. Count break ++b. Count break output. Box. print. Line. . . break False Case B’ False . . . default False True switch ( grade ) { case 'A': ++a. Count; break; case 'B': ++b. Count; break; . . . default: output. Box. print. Line( "Incorrect grade. Enter new grade. " ); break; }

Switch Selection Structure • The switch should always use the break statement for each

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.

Switch Selection Structure Write a program to determine the cost of products sold in

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.

Switch Selection Structure • What if you have multiple values you want to test

Switch Selection Structure • What if you have multiple values you want to test for and have them execute the same “case”?

Switch Selection Structure switch (switch_expr) { case item_1: item_2: statement 1; statement 2; …

Switch Selection Structure switch (switch_expr) { case item_1: item_2: statement 1; statement 2; … break; case item_3: statement 1; … break; default: statement 1; … break; }

Switch Selection Structure • Write a program to read in letter grades and calculate

Switch Selection Structure • Write a program to read in letter grades and calculate how many of each letter are entered.

List. Box Class • A List. Box allows a program to present multiple alternative

List. Box Class • A List. Box allows a program to present multiple alternative inputs to the user in a well-defined manner. – A List. Box must be associated with a Main. Window main. Window = new Main. Window(); List. Box list. Box = new List. Box(main. Window);

List. Box Class • When a List. Box is created it has nothing in

List. Box Class • When a List. Box is created it has nothing in it. The program must add items to the list. Box. add. Item(“Item One”); – As many items as needed can be added to the list.

List. Box Class • To retrieve the user’s input use the get. Selected. Index()

List. Box Class • To retrieve the user’s input use the get. Selected. Index() method. int selection = list. Box. get. Selected. Index(); – The first item in the list has an index of zero (0). – If the user makes no selection, then the result is List. Box. NO_SELECTION. – If the user cancels the input, then the result is List. Box. CANCEL.

Color Class • Java represents colors using individual red, green, and blue components combined

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)

Color Class • The color class also provides some predefined colors. (see page 276)

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: object. Name. set. Color(Color. pink);

Drawing. Board Class • The Drawing. Board class can be used to draw shapes.

Drawing. Board Class • The Drawing. Board class can be used to draw shapes. – To draw a line: object. Name. draw. Line( x 1, y 1, x 2, y 2); – To draw a circle: object. Name. draw. Circle(center. X, center. Y, radius); – To draw a rectangle: object. Name. draw. Rectangle(x, y, width, height);