Chapter 3 Decision Structure (reading)

¡Supera tus tareas y exámenes ahora con Quizwiz!

The ! operator reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true.

!

The______ operator performs a logical NOT operation. It is a unary operator that takes a boolean expression as its operand and reverses its logical value.

!

if the expression is true, the _____operator returns false, and if the expression is false, it returns true.

!

Connects two boolean expressions into one. Both expressions must be true for the overall expression to be true.

&&

The ____ operator can be used to simplify programs that otherwise would use nested if statements.

&&

The _____ operator performs short-circuit evaluation. Here's how it works: If the expression on the left side of the && operator is false, the expression on the right side will not be checked.

&&

The _______ operator is known as the logical AND operator. It takes two boolean expressions as operands and creates a boolean expression that is true only when both subexpressions are true.

&&

Remember, _____is the assignment operator and ______tests for equality.

=, ==

When using the conditional operator, the ____character appears first in the conditional expression, then the _____character.

? :

_________ operators connect two or more relational expressions into one or reverse the logic of an expression.

Logical

You cannot use relational operators to compare String objects. Instead you must use a ______method.

String

Rewrite the following if-else statements as statements that use the conditional operator. a) if (x > y) z = 1; else z = 20; b) if (temp > 45) population = base * 10; else population = base * 2; c) if (hours > 40) wages *= 1.5; else wages *= 1; d) if (result >= 0) System.out.println("The result is positive."); else System.out.println("The result is negative."); 3.9 The

a) z = x > y ? 1 : 20; b) population = temp > 45 ? base * 10 : base * 2; c) wages = hours > 40 ? wages * 1.5 : wages * 1; d) System.out.println(result >=0 ? "The result is positive" : "The result is negative");

You must write a complete _______ expression on both sides of a logical && or || operator. For example, the expression x > 0 && < 10 is not valid because < 10 is not a complete expression. The expression should be written as x > 0 && x < 10.

boolean

Normally the if statement conditionally executes only one statement. To conditionally execute more than one statement, you must enclose them in ______.

braces

The if statement can also conditionally execute a group of statements, as long as they are enclosed in a set of ______.

braces

This is not a syntax error, but it can lead to logical errors. The program does not branch out of a switch statement until it reaches a _______statement or the end of the switch statement.

break

When you use the compareTo method to compare two strings, the strings are compared ____ ____ _____.

character by character

A _____must appear after the CaseExpression in each case statement.

colon

The String class also provides the _______method, which can be used to determine whether one string is greater than, equal to, or less than another string.

compareTo

You cannot use the == operator to compare the contents of a String object with another string. Instead you must use the equals or ________method.

compareTo

The ___ _______ is powerful and unique. Because it takes three operands, it is considered a ternary operator.

conditional operator

You can use the ____ _______ to create short expressions that work like if-else statements.

conditional operator

A local variable's scope always starts at the variable's _____, and ends at the closing brace of the block of code in which it is declared.

declaration

This is not a syntax error, but can lead to a logical error. If you omit the _____section, no code will be executed if none of the CaseExpressions match the SwitchExpression.

default

This is not a syntax error, but can lead to logical errors. If you omit the trailing_____ from an if-else-if statement, no code will be executed if none of the statement's boolean expressions are true.

else

To compare the contents of two String objects correctly, you should use the String class's _____ method.

equals

To perform case-insensitive string comparisons, use the String class's _________and _____methods.

equalsIgnoreCase, compareToIgnoreCase

A ________is a boolean variable that signals when some condition exists in the program.

flag

In Java when a___ ____ value is divided by 0, the program doesn't crash. Instead, the special value Infinity is produced as the result of the division.

floating-point

The_____ statement is used to create a decision structure, which allows a program to have more than one path of execution.

if

3.6 Write an if statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10.

if (a < 10) { b = 0; c = 1; }

3.11 Write an if-else statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10. Otherwise, it should assign -99 to the variable b and assign 0 to the variable c.

if (a < 10) { b = 0; c = 1; } else { b = -99; c = 0; }

3.2 Write an if statement that multiplies payRate by 1.5 if hours is greater than 40.

if (hours > 40) payRate *= 1.5;

3.4 Write an if statement that sets the variable fees to 50 if the boolean variable max is true.

if (max) fees = 50;

3.7 Write an if statement that displays "Goodbye" if the variable myCharacter contains the character 'D'.

if (myCharacter == 'D') System.out.println("Goodbye");

3.20 Assume the variable name references a String object. Write an if statement that displays "Do I know you?" if the String object contains "Timothy".

if (name.equals("Timothy")) System.out.println("Do I know you?");

3.22 Modify the statement you wrote in response to Checkpoint 3.20 so it performs a case-insensitive comparison.

if (name.equalsIgnoreCase("Timothy")) System.out.println("Do I know you?");

3.21 Assume the variables name1 and name2 reference two different String objects, containing different strings. Write code that displays the strings referenced by these variables in alphabetical order.

if (name1.compareTo(name2) < 0) System.out.println(name1 + " " + name2); else System.out.println(name2 + " " + name1);

3.3 Write an if statement that assigns 0.2 to commission if sales is greater than or equal to 10000.

if (sales >= 10000) commission = 0.2;

3.10 Write an if-else statement that assigns 0.10 to commission unless sales is greater than or equal to 50000.0, in which case it assigns 0.2 to commission.

if (sales >= 50000.0) commission = 0.2; else commission = 0.1;

3.8 Write an if-else statement that assigns 20 to the variable y if the variable x is greater than 100. Otherwise, it should assign 0 to the variable y.

if (x > 100) y = 20; else y = 0

3.5 Write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100.

if (x > 100) { y = 20; z = 40; }

3.9 Write an if-else statement that assigns 1 to x when y is equal to 100. Otherwise, it should assign 0 to x.

if (y == 100) x = 1; else x = 0;

3.1 Write an if statement that assigns 0 to x when y is equal to 20.

if (y == 20 ) x = 0;

The ___ _____ statement will execute one group of statements if its boolean expression is true, or another group if its boolean expression is false.

if-else

The ______ _____ statement is an expansion of the if statement.

if-else

The String class provides the equalsIgnoreCase and compareToIgnoreCase methods. These methods work like the equals and compareTo methods, except the case of the characters in the strings is _______.

ignored

In Java, a program crashes when it divides an ______by 0.

integer

Because the compiler must determine the value of a CaseExpression at compile time, CaseExpressions must be either _____values or final variables.

literal

The if statement causes one or more statements to execute only when a boolean expression is ______.

true

Remember that a String object is referenced by a variable that contains the object's _____ ________.

memory address

When you use a relational operator with the reference variable, the operator works on the _______ __________ that the variable contains, not the contents of the String object.

memory address

To test more than one condition, an if statement can be ______inside another if statement.

nested

If you prematurely terminate an if statement with a semicolon, the compiler will not display an error message, but will assume that you are placing a ______statement there.

null

The _____statement, which is an empty statement that does nothing.

null

Java requires that the boolean expression being tested by an if statement is enclosed in a set of ________. An error will result if you omit the parentheses or use any other grouping characters.

parentheses

The ______of a variable is limited to the block in which it is declared.

scope

When you write a ______at the end of an if clause, Java assumes that the conditionally executed statement is a null or empty statement.

semicolon

The _______statement can only evaluate expressions that are of the int, short, byte, char, or String data types.

switch

The _______statement is a multiple alternative decision structure. It allows you to test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute.

switch

The _____statement can be used as an alternative to an if-else-if statement that compares the same variable or expression to several different values.

switch

Connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one.

||

Like the && operator, the _____ operator performs short-circuit evaluation. If the expression on the left side of the || operator is true, the expression on the right side will not be checked.

||

The ______ operator is known as the logical OR operator. It takes two boolean expressions as operands and creates a boolean expression that is true when either of the subexpressions is true.

||


Conjuntos de estudio relacionados

Chapter 9: Growing and Internationalizing the Entrepreneurial Firm

View Set

Exam 3 Pharmacology Study Questions

View Set

Chapter 33: Drugs for Inflammation and Fever

View Set

Health 6 Steps of the Goal Setting Process

View Set

IFT 302 - Foundations of Information and Computer System Security

View Set

Macroeconomics: Money, Banking, and Financial Institutions

View Set