Chapter 6: Boolean Expressions and if-else statements

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

Which has higher precedence? && or | |

&&

What should be used to compare strings?

.equals method String fileName; if (fileName.equals("words.txt")) ....

How is = different from ==?

= is for assignment == is for comparison (is equal to)

What operators have enum commands?

== and !=

What are the six relational operators?

> < >= <= == !=

Can the same case in a switch have two breaks?

A case may have several breaks, but all except the last one must be inside an if, else, or loop

What are the three logical operators?

Binary Operators: && which means and | | which means or Unary Operator ! which means not

Which has higher precedence? Binary arithmetic operators or relational operators?

Binary operators Apply ( +, -, *, /) before (< > ==)

What does the default optional clause do?

Do nothing; ;to tell the compiler what to do when a special case occurs

Why use enumerations?

Easier to read and compiler can catch errors. 1. int seasonA = 2; (not sure what 2 refers to, can't catch compiler error) 2. String seasonB = "SUMMER"; (easy to read but what if I misspell summer) 3. int seasonC = Season1.SUMMER; (easy to read and compiler will catch error but represent seasonC as a number) 4. Season seasonD = Season.SUMMER;

How do enums compare with classes?

Enum constants are implicitly static and final. public enum Season { SPRING, AUTUMN, SUMMER, WINTER} public class Season1 { public static final int SPRING = 0; public static final int AUTUMN = 1; public static final int SUMMER = 2; public static final int WINTER = 3; }

What will this print? int number = 45; if ( number == 45 ) { System.out.println("Hello"); } else System.out.println("Good"); System.out.println("Bye");

Hello Bye (the second statement is not part of the else statement)

Explain the short-circuit evaluation of if (condition1 && condition2)

If condition1 is false, then condition2 is not evaluated (the result is false anyways)

Explain the short-circuit evaluation of if (condition1 || condition2)

If condition1 is true, then condition2 is not evaluated (the result is true anyways) if (x >= 0 && Math.sqrt( x) < 15.0) Don't have to worry about second part if first condition is true

When is this expression true? condition1 || condition2

If condition1 or condition2 (or both) are true

Because the else clause is optional, how does a program read the if statement?

If the condition is true, the program executes statement1. If the condition is false, the program skips the block of statements under if.

What is short-circuit evaluation?

If the value of the first (left) operand in a binary logical operation is sufficient to determine the result of the operation, the second operand is not evaluated.

Why do we have to be careful when applying == and != to objects?

Instead of comparing the values of two objects, we are comparing two references to them instead.

Does it make sense to declare constants for boolean?

No because you can just use true or false in the code.

Are braces necessary if the compound block within braces consist of only one statement?

No, but it is advisable to keep them if you want to add additional statements. if (condition) statementA; else statementB;

When is this expression true? !condition1

Only if condition1 is false

The expression evaluated in a switch is usually what?

The expression evaluated in a switch must have an integral or string type (int, char, or String). Usually it is not an expression but actually a variable int k = ... switch (k) String dayString = ... switch(dayString)

What is the "dangling else" bug in nested if-else statements? if ( condition1) if (condition2) statement1; else statement2;

This program would be compiled as: if (condition1) { if (condition2) statement1; else statement2; } the else statement is under if (condition2), not if (condition1)

What does ! ( p || q) mean and equivalent to?

To get a true value, if both p and q are false, then the whole expression is false. !p && !q If both are false, the expression will be false. Thus, the original expression will be true.

What does ! ( p && q) mean and equivalent to?

To get a true value, if either p or q is false, then the whole expression is false. !p | | !q If the first one is false or the second one is false, the original expression will be true.

When would we want to use == and != operators to compare strings?

To test whether our two objects are the same. To compare to null.

Which has higher precedence? Binary or Unary logical operators

Unary Apply ! before && and | |

When do we use the enum Data Type?

We use it when an object's attribute or state can have only one of a small set of values

When do we use == and != with objects? (2 reasons)

When we want to know if two objects are the same object. When we want to compare to null. String text = file.readLine(); if (text != null) ...

Can the same action be activated by more than one label?

Yes. case '/'; case ':': <...statements....> break

What are breaks used for in a switch?

a break instructs the code to break out of the switch and go to the statement after the switch

Each case must be labelled by what?

a literal or symbolic constant. A case cannot be labeled by a variable or an expression that is not constant case 1; case "Monday";

Which has higher precedence? arithmetic/relational operators or binary logical operators

arithmetic/relational operators

The result of a relational operator has the _________ type.

boolean

What can I write instead of this? boolean exceedsLimit; if ( speed > limit) { exceedsLimit = true; else exceedsLimit = false; }

boolean exceedsLimit = speed > limit The value of exceedsLimit will be true only if speed > limit.

Write a boolean expression to test if int x will fall between 0 and 50.

boolean intTest = 0 <= x && x <= 50;

Which of these expressions can be used in a switch? -chars -ints -doubles -enums

chars-yes ints-yes doubles-no enums-yes

What data types should we avoid when using == or !=?

double and float because the arithmetic may have rounding errors double x = 15.0/11.0; System.out.println( 11.0 * x == 15.0); This compiler will print out false.

What do enum variables represent?

enum variables do not represent numbers, characters, or strings. They represent objects

What is the general format for boolean?

fields: private boolean hasMiddleName; local variables: boolean aVar;

Give an example of using if-else-if chain with Grade Averages.

if ( avg >= 90) grade = 'A'; else if ( avg >= 80) grade = 'B'; else if ( avg >= 70) grade = 'C'; else if ( avg >= 60) grade = 'D'; else grade = 'F';

With the nested ifs, what is the equivalent of this expression? if ( condition1) if ( condition2) statement;

if ( condition1 && condition2) statement;

Give an example of using nested if else with surcharge calculation

if (age <= 25) { if (accidents) statement1; else statement2; } else // age > 25 { if (accidents) statement3; else statement4; }

What is the general form of the if-else statement?

if (condition) { statementA1; statementA2; ... } else { statementB1; statementB2; ... }

When is this expression true? condition1 && condition2

if both condition1 and condition2 are true

When do we use if-else-if and when do we used nested if-else?

if-else-if: branching in multiple ways (grades) nested if-else: hierarchical branching

what and how does the name of the enum type usually spelled?

name of enum usually starts with capital letter enum values are usually spelled in all caps

In boolean expressions, how are enum values used (written)?

name-dot prefix private enum Speed { LOW, MEDIUM, FAST} Speed currentSpeed = Speed.LOW if ( Speed.LOW == currentSpeed ) ....

Relational operators can be applied to _____________ data types and _____________.

numeric if ( sum != 0) characters if ( letter == 'Y')

In switch statements, how are enum values used (written)?

only values without the prefix switch (currentSpeed) { case LOW; statement1; break; case MEDIUM; .... }

Use an enumeration to define how SUMMER is my favorite season

public enum Season { SPRING, AUTUMN, SUMMER, WINTER} public static void main (String [] args) { Season favoriteSeason = Season.SUMMER; }

Give an example of switch with dayString

switch (dayString) { case "Sunday"; statement1; break; case "Monday"; statement2; break; case "Tuesday"; statement3; break; }

What happens if you forget a break statement?

the code "falls through" and continues with the statements in the next case

Why are switch statements used?

to replace long if-else-if statements


Conjuntos de estudio relacionados

Health Assessment Peripheral Vascular and Lymph System

View Set