Logic

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Short-circuit evaluation for a Boolean expression

In short-circuit evaluation, the subexpression in a compound Boolean expression are evaluated from left to right, and evaluation automatically stops as soon as the value of the entire expression is known. For example: A || B, where A and B are Boolean expression. if A is true, then the whole expression is irrespective of the value of B. So B, the second operand, is not evaluated.

true

The primitive type boolean has one of two values: true or false. A boolean variable can be initialized as follows: boolean legal = true;

false

The primitive type, boolean, has one of two values: true or false. A boolean variable can be initialized as follows: boolean found = false; Note that an instance variable of type boolean has a default value of false.

If A is a Boolean variable whose value is true, if B is a Boolean variable whose value is true, and if C is a Boolean variable with value false , what will be returned by the following statement? return !(A || B || C);

false !(A || B || C) is equivalent to NOT (true OR true OR false). Inside the parentheses, work from left to right: true OR true OR false is true. Finally, NOT true is false

If A is a boolean variable whose value is true, if B is a boolean variable whose value is true, and if C is a boolean variable with value false, what will be returned by the following statement? return (A || B) && C;

false (A || B) && C is equivalent to (true OR true) AND false. Since parentheses have the highest precedence, the expression becomes true AND false, which is false

If A is a boolean variable whose value is false, if B is a boolean variable whose value is true, and if C is a boolean variable with value false, what will be returned by the following statement? return A || B && C;

false A || B && C is equivalent to false OR (true AND false) since AND has precedence over OR. The expression becomes false Or false, which is false

5 + 3 < 17 / 2

false Note that + and / have precedence over <. Therefore, the statement is equivalent to 8 < 9, which is false.

2 < 7 && 4 > 8

false Note that < and > have precedence over &&. Therefore, the statement is equivalent to true AND false, which is false.

!true

false The Boolean expression (NOT true) is false

false || false

false The Boolean expression (false OR false) is false

true && false

false The Boolean expression (true AND false) is false.

If found is a boolean variable whose value is true and if special is a boolean variable with value false, what will be returned by the following statement? return found && special;

false true AND false equals false

Write the Java equivalent of the following mathematical test: if -6 <= x <=9

if ( x >= -6 && x <= 9) . . . The mathematical statement -6 <= x <= 9 is equivalent to both x >= -6 AND x <= 9. Use the correct compound operators, && (and), <= (less than or equal to), and >= (greater than or equal to).

Write the Java equivalent of the following mathematical test: if x <= 9 OR x >= 60

if (x <= 9 || x >= 60) . . . Use the correct compound operators, || (or), <= (less than or equal to), and >= (greater than or equal to).

Write one statement that is equivalent to the statement: if (x == 1 || y < 2) return true; else return false;

return (x == 1) || ( y < 2); OR return x == 1 || y < 2; Return the truth of the Boolean expression ((x == 1) || (y < 2)). Note that you do not need the parentheses because || (which means "or") has lower precedence than either == or <.

What will be the effect of executing the following code segment, given that int num1 is 0 and int num2 is 10? if (num1 + num2 > 0) || (num2 / num1 > 80) statement1; else statement2;

statement 1 will be executed. The compound test evaluates to true. This is an example of short-circuit evaluation, in which the right-hand part of the test is not evaluated since the value of the compound or statement will be true no matter the value of the right-hand part. Therefore, there will not be a run-time error due to division by zero.

What will be the effect of executing the following code segment, given that int num1 is 0 and int num2 is 10? if (num1 + num2 > 0) && (num1 / num2 > 80) statement1; else statement2;

statement 2 will be executed. The compound test evaluates to false since true AND false is false. The else part of the statement will therefore be executed.

What will be the effect of executing the following code segment, given that int num1 is 0 and int num2 is 10? if (num1 / num2 < 0) || (num1 + num2 > 0) statement1; else statement2;

statement1 will be executed. The test is equivalent to false OR true, which is true

!false

true The Boolean expression (NOT false) is true

true || false

true The Boolean expression (true OR false) is true.

true || true && false

true The Boolean expression (true OR true AND false) is true. Since AND has precedence over OR, calculate (true AND false) first. It equals false. Therefore, the given expression is equivalent to (true OR false), which is true

(!true) || true

true The Boolean expression [(NOT true) OR true] is equivalent to (false OR true), which is true

If found is a boolean variable whose value is true and if special is a boolean variable with value false, what will be returned by the following statement? return found || special;

true true OR false equals true

A Boolean expression

A Boolean expression is a Java expression that has one of two values: true or false. Boolean expressions are used in conditional statements, such as if, if . . . else, and while loops. For example, in the statement if (x == y) . . . x == y is a Boolean expression. Other examples of Boolean expressions: x < 4, y != 10, etc.

A logic or an intent error

A logic or an intent error has occurred when a program compiles and runs but fails to do what the programmer intended. In other words, the logic in the program is incorrect. Some examples: a sorting program that fails to sort correctly, a search program that erroneously fails to find a key in a list, a search program that outputs incorrect information, and so on.

A logical operator

A logical operator in Java programming (sometimes called a "Boolean operator") is an operator that returns a Boolean result that is based on the Boolean result of one or two other expressions. ! NOT if (!found) && AND if (x < y) && (p != q) || OR if (p == q) || (j >= 4)

boolean

A primitive data type whose value is true or false. It can be used in the declaration of a variable or as the return type of a method. For example: boolean found = false; public boolean isFound(String subStr) {

A relational operator

A relational operator compares two numbers and returns a boolean value (true or false). Java has 6 relational operators: < (less than), > (greater than), == (equal to), != (not equal to), <= (less than or equal to), >= (greater than or equal to). For example: 6 != 3 //not equal to, returns true 4 <= 2 //less than or equal to, returns false

What will be the effect of executing the following code segment, given that int num1 is 0 and int num2 is 10? if (num2 / num1 < 0) || (num1 + num2 > 0) statemednet1; else statement2;

An ArithmeticException will be thrown. The left side of the test attempts to divide by zero. Note that the right side of the test won't be evaluated

What will be the effect of executing the following code segment, given that int num1 is 0 and int num2 is 10? if (num1 + num2 > 0) && (num2 / num1 > 80) statement1; else statement2;

An ArithmeticExeption will be thrown. The right side of the test attempts to divide by zero. Note that short-circuit evaluation doesn't occur here, because the value of the compound test is not known until the right-hand piece is evaluated: true AND false is false, whereas true AND true is true.


Kaugnay na mga set ng pag-aaral

AIS Chapter 6 IT Governance Questions

View Set

Trail Guide to The Body: Chapter 2 - Shoulder & Arm

View Set