Unit 5?/chp.5/6

Ace your homework & exams now with Quizwiz!

Short curcuit evaluation *example with &&

When you use the && operator, if the first tested expression is false, the second expression is never evaluated because its value does not matter.

What is the value of b and of c after the program executes? b = 4; c = b++

When you use the postfix ++, the value of the expression before the increase is stored. The result is still that b is 5, but c is only 4. The value of b is assigned to c and then b is incremented. In other words, if b = 4, the value of b++ is also 4, but after the statement is completed, the value of b is 5.

do you have to type the whole if statement on one line?

You could type the entire if statement on one line and it would execute correctly; however, the two-line format for the if statement is more conventional and easier to read

what do the three sections in a for loop do?

Initializing the loop control variable Testing the loop control variable Updating the loop control variable

when in Java, you combine Boolean tests into a single expression using the logical AND and OR operators is?

Such an expression is a compound Boolean expression or a compound condition.

(T/F)Assume that x = 4 and y = 5. The value of y += x is 9.

T

(what is wrong with this statement who's purpose is to decide whether quizScore is equal to ten) quizScore = 10

The assignment operator is used not the equivalency operator so it attempts to assign quizScore to 10 instead of evaluating if it is equal to 10.

Where does the if statement end?

The statement ends after the action that should execute if the Boolean expression in the if statement is true. So that is where you should type the semicolon.

?priming read

the first input statement prior to a loop that will execute subsequent input statements for the same variable.

equivalency operator

the operator composed of two equal signs that compares values and returns true if they are equal.(==)

NOT operator

the operator that negates the result of any Boolean expression. which is written as the exclamation point ( ! )

counting

the process of continually incrementing a variable to keep track of the number of occurrences of some event.

Validating data

the process of ensuring that a value falls within a specified range.

accumulating

the process of repeatedly increasing a value by some amount to produce a total.

Loop fusion

the technique of combining two loops into one.

way to remember AND and OR operators

they are evaluated in alphabetical order

For clarity, many programmers prefer to surround each Boolean expression that is part of a compound Boolean expression...

with its own set of parentheses, as in the following example

which has a higher precedence. && or ||

within an arithmetic expression, multiplication and division are always performed prior to addition or subtraction. In the same way, && has higher precedence than ||.

can you preform more than one test in the second section of an for loop?

yes

can you make calls to methods for any of the three section in a for loop?

yes, however for the middle section the value return has to be a boolean value and the third section has to return a data type accepted into the 2nd sections method.

is the following legal; for(g = 0, h = 1; g < 6; ++g)

you can initialize more than one variable in the first section of the for statement by placing commas between the separate statements.

what can you put between a if statements parentheses?

you can place any Boolean expression.

If you think your application is in an infinite loop what do you do?

you can press and hold the Ctrl key, and then press C or the Break key; the looping program should terminate

what comparisons can you use when making decisions?

you can use any expression that evaluates as true or false, such as a simple boolean variable or a call to a method that returns a boolean value.

When you want some action to occur even if only one of two conditions is true, you can use nested if statements, or you can use the

you can use the logical OR operator, which is written as ||

In Java, when you want to take an action if a Boolean expression is true you use a what?

you use an if statement.

If you want to take an action when a Boolean expression is true but take a different action when the expression is false,

you use an if...else statement

How do you normally type an if statement?

you usually type if and the Boolean expression on one line, press the Enter key, and then indent a few spaces before coding the action that occurs if the Boolean expression evaluates as true.

in a switch statement, break, does what?

break optionally terminates a switch statement at the end of each case.

how do you preform multiple actions in the third section of an for loop

by separating each action with commas

in a switch statement, case, does what?

case is followed by one of the possible values for the test expression and a colon.

in a switch statement, default, does what?

default optionally is used prior to any action that should occur if the test variable does not match any case.

nested if statements

describes if statements when one is contained within another.

short-circuit evaluation

describes the feature of the AND and OR(nested ifs and elses) operators in which evaluation is performed only as far as necessary to make a final decision.

how can you use short circuit operators to make looping codes more efficient.

for example, if you know a certain answer is more likely in an or decision then make that the first option so the second half does not need to execute.

what are the three most basic operations

and, or, and not

side effect

any action in a method other than returning a value.

and a diamond to represent

any decision

In flow charts you use a rectangle to represent what?

any unconditional step

compound condition

the condition that is tested in a compound Boolean expression.

What order are the following evaluated in?) &&, ||, !=

!=, &&, ||

what is the following expressions equivalent?) !(x > 0) !(x < 0) !(x >= 0) !(x <= 0)

(x <= 0) (x >= 0) (x < 0) (x > 0)

dangling else

A logic error that occurs in a nested if statement when an else clause is associated with the inside if instead of with the outside if as intended.

are you ever required to use the OR operator?

As with the && operator, you are never required to use the || operator because using nested if statements always achieves the same result. However, using the || operator often makes your code more concise, less error-prone, and easier to understand.

(T/F)the logic is the same whether each else and the subsequent if are on the same line or different lines.

Because Java ignores whitespace, the logic is the same(T)

every computer decision results in a

Boolean value

what are the six comparison operators

Equal to( == ) Greater than ( > ) Less than( < ) Greater than or equal to ( >= ) Less than or equal to ( <= ) Not equal to ( != )

(T/F)Assume that x = 4 and y = 5. The value of y == x++ is true.

False) If x is 4 and y is 5, then the value of x++ is 4, and so y is not equal to 4

what can you do as an alternative to nested if statem

For an alternative to some nested if statements, you can use the logical AND operator between two Boolean expressions to create a compound Boolean expression that is true when both of its operands are true.

(T/F)Within the parentheses of a for loop, the last section must alter the loop control variable.

Frequently, the third section of a for loop is used to alter the loop control variable, but it is not required.

can you use a variable declared in a for statement elsewhere in the program?

If you declare a variable within a for statement, the variable can only be used in the block that depends on the for statement; when the block ends, the variable goes out of scope.

what happens if you leave out the break statements in a switch statement?

If you omit the break statements than all of the possible options that can occur after if in the block of code will occur. This can be useful if you want for example a list of what you still have left to do in a day. If it is five o'clock than it will run down all the options including and after 5 o'clock left to do not just what you have to do at 5.

comparing two objects with == is true when?

In other words, == yields true for two objects only when they refer to the same object in memory, not when they are different objects with the same value.

short circuit evaluation * example with ||

In other words, because only one of the Boolean expressions in an || expression must be true to cause the dependent statements to execute, if the expression to the left of the || is true, then there is no need to evaluate the expression to the right of the ||.

what must you include in the loop body in order for the loop to end?

In the body of the loop, you must include a statement that alters the loop control variable

can you nest loops?

Just as if statements can be nested, so can loops. You can place a while loop within a while loop, a for loop within a for loop, a while loop within a for loop, or any other combination.

when are nested if statements useful?

Nested if statements are particularly useful when two or more conditions must be met before some action is taken.

Why does coding multiple if statements and else statements within other if and else statements work.

No matter how many levels of if...else statements are needed to produce a solution, the else statements are always associated with their ifs on a "first in-last out" basis. Kind of like with how a block of code works with levels.

should you compare strings using == or !=

No, even if they are the same word there are lots of ways that it can go wrong. Instead you should use .equal or .compareto

a variable declared in a block such as one after an if statement will work if used elsewhere outside of block?

No, it is local to the block and it will be forgotten unless the other block is nested within the block the variable was declared in.

Can you put lines that are not part of an if else statement in between the if part and else part?

No, they have to be back to back or it will ruin separate the two statements and it is illegal to have an else statement without an if statement before it that is part of it.

can you use == to compare to Strings?

Remember, Strings are objects, so do not use == to compare Strings

Does the compiler take indentation into account?

Remember, the compiler does not take indentation into account, but consistent indentation can help readers understand a program's logic.

*Example of NOT operator

The value of any false expression becomes true when preceded by the NOT operator.

(will this statement work) if(student1.getGpa() > student2.getGpa()) System.out.println("The first student has a higher gpa");

The values represented by student1.getGpa() and student2.getGpa() are both doubles, so they can be compared using any of the relational operators. In Java, object names are references, but values that are simple data types are not

What is the best way to compare values of objects?

To compare the values of objects, you should write specialized methods.

(T/F)Assume that x = 4 and y = 5. The value of ++y + ++x is 11.

True

What is a way to avoid precedence of operators when combining them in the same if or if else statements

Use parentheses. This helps not only with organization but also in avoid errors.

what is the difference between a prefix operator and a postfix operator?

When a prefix operator is used in an expression, the value after the calculation is used, but when a postfix operator is used in an expression, the value before the calculation is used

What does the compiler see when reading the following code?) smallerNum = (a < b) ? a : b;

When evaluating the expression a < b, if a is less than b, the entire conditional expression takes the value to the left of the colon, a, which then is assigned to smallerNum. If a is not less than b, then the expression assumes the value to the right of the colon, and b is assigned to smallerNum.

Do you put a semicolon at the end of each both if and else parts in an if else statement?

When you execute an if...else statement, only one of the resulting actions takes place depending on the evaluation of the Boolean expression. Each statement, the one dependent on the if and the one dependent on the else, is a complete statement, so each ends with a semicolon.

What is the value of b and of c after the program executes?) b = 4; c = ++b;

When you use the prefix ++, the result is calculated, and then its value is used. The result is that both b and c hold the value 5 because b is increased to 5 and then the value of the expression is assigned to c.

what can you code with an if else clause?

Within an if or an else clause, you can code as many dependent statements as you need, including other if and else statements.

can you nest an if statement in a else statement?

Yes, you can not just nest if statements in if statements but also else statements.

are you ever required to use the && operator?

You are never required to use the && operator because using nested if statements always achieves the same result, but using the && operator often makes your code more concise, less error-prone, and easier to understand.

how do you create a for loop

You begin a for loop with the keyword for followed by a set of parentheses. Within the parentheses are three sections separated by exactly two semicolons.

can you code an else statement without and if statement?

You can code an if without an else, but it is illegal to code an else without an if that precedes it.

what do you do if you want a decision to do multiple things depending on the outcome.

You can create a block for the statement after the the first line with the if and comparison

can you initizalize a variable in a for statement?

You can declare a variable within a for statement, as in the following: for(int val = 1; val < 11; ++val)

(T/F) You can improve loop performance when two conditions must both be true by testing for the most likely occurrence first.

You can improve loop performance when two conditions must both be true by testing for the least likely occurrence first. That way, the second test will need to be performed less frequently.

can you leave more than one section in a for loop empty?

You can leave one or more portions of a for loop empty, although the two semicolons are still required as placeholders. For example, if x has been initialized in a previous program statement, you might write the following: for(; x < 10; ++x)

what can you use while loops for?

You can use a while loop when you need to perform a task either a predetermined or unpredictable number of times.

can you use the prefix ++ and postfix ++ operators with constants?

You cannot use the prefix ++ and postfix ++ operators with constants. An expression such as ++84; is illegal because an 84 must always remain an 84. However, you can create a variable named val, assign 84 to it, and then write ++val; or val++; to increase the variable's value

logical OR operator

an operator used between Boolean expressions to determine whether either expression is true; written as two pipes (||).

how do you avoid a dangling else if you want and if else statement with a nest if but not also a nested else.

You would use brackets because java recognizes brackets and can determine in something is nested or not a part of unlike with blank space like tabs.

empty body

a block with no statements in it.

a boolean expression includes?

a boolean variable a method that returns a boolean value a comparison

When you use the && operator, you must include a complete _____ on each side.

a complete Boolean expression on each side

while loop

a construct that executes a body of statements continually as long as the Boolean expression that controls entry into the loop continues to be true.

single-alternative selection

a decision structure that performs an action, or not, based on one alternative. (If statement)

counted loop

a definite loop.

counter-controlled loop

a definite loop. Contrast with event-controlled loop.

sequence structure

a logical structure in which one step follows another unconditionally.

decision structure

a logical structure that involves choosing between alternative courses of action based on some value within a program.

indefinite loop

a loop in which the final number of iterations is unknown. Contrast with definite loop.

posttest loop

a loop in which the loop control variable is tested after the loop body executes. Contrast with pretest loop.

pretest loops

a loop in which the loop control variable is tested before the loop body executes. Contrast with posttest loop.

for loop

a loop that can be used when a definite number of loop iterations is required.

outer loop

a loop that contains another loop.

do...while loop

a loop that executes a loop body at least one time; it checks the loop control variable at the bottom of the loop after one repetition has occurred.

definite loop

a loop that executes a predetermined number of times; a counted loop. Contrast with indefinite loop.

inner loop

a loop that is contained entirely within another loop.

infinite loop

a loop that never ends.

do-nothing loop

a loop that performs no actions other than looping.

function

a method with no side effect, in some programming languages.

can you use <, >, ≤=, or ≥= to compare objects

a program containing such comparisons does not compile. You can use the equals and not equals comparisons (== and !=) with objects but when you use them, you compare the objects' memory addresses instead of their values.

dual-alternative selection

a selection that results in one of two possible courses of action.(If..else statement uses)

range check

a series of statements that determine within which of a set of ranges a value falls.

"Display an error message when an employee's hourly pay rate is less than $5.85 and when an employee's hourly pay rate is greater than $60."(what is wrong if anything with the following code) if(payRate < LOW && payRate > HIGH) System.out.println("error in pay rate");

a single variable, a payRate value can never be both below 5.85 and over 60 at the same time, so the output statement can never execute, no matter what value payRate has. In this case, you can write the following code that uses the || operator to display the error message under the correct circumstances

empty statement

a statement that contains only a semicolon.

if...else statement

a statement that provides the mechanism to perform one action when a Boolean expression evaluates as true, and to perform a different action when a Boolean expression evaluates as false.

switch statement

a statement that uses up to four keywords to test a single variable against a series of exact integer or character values. The keywords are switch, case, break, and default.

loop

a structure that allows repeated execution of a block of statements.

pseudocode

a tool that helps programmers plan a program's logic by writing plain English statements without concern for programming language syntax.

flowchart

a tool that helps programmers plan a program's logic by writing the steps in diagram form, as a series of shapes connected by arrows.

sentinel

a value that stops a loop.

loop control variable

a variable whose value determines whether loop execution continues.

when comparing to objects if the following evaluates to true it means the objects are _____ of the same object myHouse == momsHouse

aliases

what happens if the user enters data outside the expected data types?

an error occurs and the program terminates.

compound Boolean expression

an expression that contains an AND or OR operator.

event-controlled loop

an indefinite loop in which the number of executions is determined by user actions. Contrast with counter-controlled loop.

add and assign operator

an operator that alters the value of the operand on the left by adding the operand on the right to it; composed of a plus sign and an equal sign (+=).

remainder and assign operator

an operator that alters the value of the operand on the left by assigning the remainder when the left operand is divided by the right operand; composed of a percent sign and an equal sign (%=).

divide and assign operator

an operator that alters the value of the operand on the left by dividing the operand on the right into it; composed of a slash and an equal sign (/=).

multiply and assign operator

an operator that alters the value of the operand on the left by multiplying the operand on the right by it; composed of an asterisk and an equal sign (* =).

subtract and assign operator

an operator that alters the value of the operand on the left by subtracting the operand on the right from it; it is composed of a minus sign and an equal sign (−=).

prefix ++

an operator that is composed by placing two plus signs to the left of a variable; it adds 1 to the variable, then evaluates it. Contrast with postfix ++.

prefix increment operator

an operator that is composed by placing two plus signs to the left of a variable; it adds 1 to the variable, then evaluates it. Contrast with postfix ++.

postfix ++

an operator that is composed by placing two plus signs to the right of a variable; it evaluates the variable, then adds 1 to it. Contrast with prefix ++.

postfix increment operator

an operator that is composed by placing two plus signs to the right of a variable; it evaluates the variable, then adds 1 to it. Contrast with prefix ++.

ternary operator

an operator that needs three operands.

conditional operator

an operator that requires three expressions separated with a question mark and a colon; the operator is used as an abbreviated version of the if. . .else structure.

logical AND operator

an operator used between Boolean expressions to determine whether both are true; written as two ampersands (&&).

How would the following code be written as an if else statement?) smallerNum = (a < b) ? a : b;

if (a < b) smallerNum = a; else smallerNum = b;

How does a for loop go through each of it sections and what does it do in them.) for(val = 1; val < 11; ++val) { System.out.println(val): }

in the first section is where the variable is initialized, this section will only execute once. In the second section is where the program tests the loop control variable if true to move to the body of the paragraph and return to the 3rd section once finished. The third section updates the loop control variable by adding 1 and then to returns to the middle section again with the new variable. The cycle ends when the statement is false.

The expression if(x = true) will compile only if x is

is a boolean variable, because it would be legal to assign true to x.

(what does the following code do?) isPerfectScore = (quizScore == 10);

isPerfectScore is a Boolean variable, the following statement compares quizScore to 10 and stores true or false in isPerfectScore

what does the following code do?) count = 12; answer = ++count;

it adds one to count and then reassigns it to answer.

what does the following code do?) count = 12; asnwer = count++;

it assigns or evaluates 12 to answer and then adds 1 to that answer and assigns it to count.

(T/F)it is most efficient to ask the question that is most likely to be false first

it is most efficient if the question is most likely true. That way, you most frequently avoid asking multiple questions

what would deMorgan's law do to the following expression?) !(a || b)

it would mean that an equivalent expression would be equal to not a and b of (!a && !b)

?priming input

the first input statement prior to a loop that will execute subsequent input statements for the same variable.

iteration

one loop execution.

prefix and postfix decrement operators

operators that subtract 1 from a variable before and after evaluating it, respectively.

A Java if statement always includes?

parentheses

The two vertical lines used in the OR operator are sometimes called

pipes

what is the simplest statement you can use to make a decision?

single-alternative selection(If statement)

An alternative to using a Boolean expression in an if statement, such as quizScore == 10, is to

store the Boolean expression's value in a Boolean variable.

in a switch statement, switch, does what?

switch starts the statement and is followed immediately by a test expression enclosed in parentheses.

what must you make sure to do when nesting loops as with nesting anything else.

that you do not overlap loops. You can have loops within loops but not overlap them.

incrementing

the act of adding 1 to a variable.

decrementing

the act of subtracting 1 from a variable.

loop body

the block of statements that executes when the Boolean expression that controls the loop is true.


Related study sets

Critical Thinking Midterm Study Guide

View Set

Organelle Quiz 1 - Organelles involved in making proteins

View Set