Java Chapter 4

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

! (logical NOT)

"reverses" the meaning of a condition.

The _____ operator can be used to ensure that two conditions are both true before choosing a certain path of execution.

&&

The __________operator only tests the second condition when the first is true; thereby ensuring that two conditions are both true before choosing a certain path of execution.

&& (conditional AND)

Describe the four basic elements of counter-controlled repetition.

1. A controlled variable (or loop counter) 2. The initial value of the control variable. 3. The increment (or decrement) by which the control variable is modified each time through the loop (known as each iteration of the loop). 4. The loop-continuation condition that determines if looping should continue.

%b format specifier

To display the word "true" or the word "false" based on a boolean expression's value.

An expression containing the || operator is true if either or both of its operands are true.

True

Listing cases consecutively with no statements between them enables the cases to perform the same set of statements.

True

The comma ( , ) formatting flag in a format specifier (e.g., %,20.2f) indicates that a value should be output with a thousands separator.

True

Discuss a situation in which it would be more appropriate to use a do...while statement than a while. Explain why.

Use do...while instead of while when a set of instructions/block of statements that need to executed at least once.Example: Need to input set of integers in range 1 through 10 and find cumulative sum. int number, sum=0; cout<<"Enter number:"; cin>>number do { sum=sum+number; cout<<"Enter number:"; cin>>number; }while(number>0&&number<10);

do-while

is a post-test loop that allows the body of the loop to be entered at least once before the condition is tested, i.e., the condition (also known as the test expression) is tested last.

These operators enable you to form more complex conditions by combining simple conditions

logical

The for loop is this type of loop

pretest

initialization

where the loop-control variable is declared and/or set. The loop-control variable declared in the for header belongs to the for. The loop-control variable also can be declared before the for loop at the class level or the method level. At the class level, the variable belongs to the entire class whereas a method level variable belongs only to the method

three repetition ways

while statement do...while statement for statement

This operator reverses a condition's meaning

!

The __________operator can be used to ensure that either or both of two conditions are true before we choose a certain path of execution.

|| (conditional OR)

boolean logical AND (&) and OR (|)

Are identical to the && and || operators, except that the & and | operators ALWAYS evaluate both of their operands (i.e. they do NOT perform short-circuit evaluation)

Find the error(s) in the following segments of code: The following code should print whether integer value is even or odd: switch (value % 2 ) { case 0: System.out.println("Even integer"); case 1:System.out.println("Odd integer"); }

Each case block should end with a break statement unless it is desired case 0 and case 1 print every time the program is executed. { case 0: System.out.println("Even integer"); break; case 1: System.out.println("Odd integer"); break; }

&& (conditional AND)

Ensure at some point in a program that two conditions are both true before we choose a certain path of execution. Operator && has a higher precedence than operator ||. Both associated left to right.

|| (conditional OR)

Ensure that either or both of two conditions are true before we choose a certain path of execution.

The following code should print the values 1 to 10: n = 1; while(n < 10) System.out.println(n++);

Error: An improper relational operator is used in the while's continuation condition. Correction: Use <= rather than <, or change 10 to 11.

switch (n) { case 1: System.out.println("The number is 1"); case 2: System.out.println("The number is 2"); break; default: System.out.println("The number is not 1 or 2"); break; }

Error: The missing code is the break statement in the statements for the first case. Correction: Add a break statement at the end of the statements for the first case. This omission is not necessarily an error if you want the statement of case 2: to execute every time the case 1: statement executes.

Find the error in each of the following code segments, and explain how to correct it: i = 1; while (i <= 10); ++i; }

Error: The semicolon after the while causes an infinite loop, and there's a missing left brace. Correction: Replace the semicolon with a {, or remove both the ; and }.

for(k = 0.1; k != 1.0; k += 0.1) System.out.println(k);

Error: Using a floating-point number to control a for statement may not work, because the floating-point numbers are represented only approximately by most computers. Correction: Use an integer, and perform the proper calculation in order to get the values you desire:for(k = 1; k!= 10; k++)System.out.println((double) k/10);

The break statement is required in the last case of a switch selection statement.

False

The default case is required in the switch selection statement.

False

The expression (( x > y) && (a < b)) is true if either x > y is true or a > b is true.

False

To test for a range of values in a switch statement, use a hyper (-) between the start and end values of the range in a case label.

False

Find the error(s) in the following segments of code: For( i = 100, i >= 1, i++)System.out.println(i);

In looping statement for 'F' cannot be capital, all the initialization, condition, and increment/decrement must be separated by semicolons. The loop is an infinite loop--i value must be decremented (decreasing) for(i = 100; i >= 1; i--) System.out.println(i);

Find the error(s) in the following segments of code: The following code should output the even integers form 2 to 100: counter = 2; do{ System.out.println(counter);counter += 2; } While (counter > 100);

In while looping statement 'w' must be lowercase. counter = 2; do{ System.out.println(counter); counter += 2;} while (counter > 100);

Only there forms of control are needed to implement an algorithm:

Sequence Selection Repetition

Java Control Statements are ....

Single entry point and the single exit point of each control statement.

for repetition statement

Specifies the counter-controlled-repetition details in a single line of code

short-circuit evaluation

The && and || operators "short-circuit", meaning they don't evaluate the right hand side if it isn't necessary.

Compare and contrast the break and continue statements.

The break statement, when executed in a while, for, do...while, or switch, causes immediate exit from that statement. The break statement is used to escape early form a loop or to skip the remainder of switch. The continue statement, when executed in a while, for, or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In a while and do...while statement, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for loop, the increment expression executes then the program evaluates the loop continuation test.

initialization section

The initialization section of the for loop allows the loop to initialize its own control variable which is i in this example. for(int i = 0; test; update)

Find the error(s) in the following segments of code: The following code should output the odd integers from 19 to 1: for (i = 19; i >= 1; i += 2) System.out.println(i);

The loop is infinite--i value must be decremented by 2. for (i = 19; i >= 1; i -= 2) System.out.println(i);

test section

The test section of the for statement acts in the same manner as the condition section of a while loop by testing the control variable. for(int i = 0; i < testValue; update)

update section

The update section of the for loop is the last thing to execute at the end of each loop by modifying the control variable for (int i = 0; i < testValue; i++)

conditional

The use of a conditional expression can in some cases simplify the code with respect to the use of an if-else statement

Compare and contrast the while and for repetition statements

The while statement can be used to implement any counter controlled loop but the for repetition statement is for specifying the counter controlled repetition detail in a single line of code.The for statements are used for counter controlled repetition and the while statements for sentinel controlled repetition. Both the while and for can each be used for either repetition type.

The __________ operator is true if and only if one of its operands is true and the other is false.

^ (boolean logical exclusive OR)

The do...while statement tests the loop continuation condition ______ executing the loop's body; therefore, the body always executes at least once.

after

The _____ statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

continue

Calculate the value of 2.5 raised to the power of 3, using the pow method.

double result = Math.pow(2.5, 3);

If the loop-continuation condition in a for header is initially _____, the program does not execute the for statement's body.

false

Typically, _____ statements are used for counter-controlled reputation and _____ statements for sentinel-controlled repetition.

for while

Repeat part (c) using a for statement.

for(i = 1; i <= 20; i++) { System.out.print(i); if (i % 5 == 0) System.out.println(); else System.out.print('\t'); }

What is in the header of a for statement?

for(initializaton; loopContinuationCondition; increment)statement

Print the integers from 1 to 20, using a while loop and the counter inside the variable i. Assume that the variable i has been declared, but not initialized. Print only five integers per line. [Hint: Use the calculation i % 5. When the value of this expression is 0, print a newline character; otherwise, print a tab character. Assume that this code is an application. Use the System.out.println() method to output the newline character, and use the System.out.print('\t') method to output the tab character.]

i = 1; while(i <= 20) { System.out.print(i); if(i % 5 == 0) System.out.println();elseSystem.out.print.('\t'); ++i; }

selections

if statement (single selection) if...else (double selection) switch (multiple selection)

Three forms of control

sequence, selection, repetition

break

statement completely exits the entire loop where the break is encountered, so it forces the loop to end.breaks are used to escape early or skip code.

continue

statement exits the current repetition (iteration) of the loop where the continue is encountered, but it allows re-entry into the loop.Continues are used to skip code.

switch multiple-selection statement

statement performs different actions based on the possible values of a constant integral expression of type byte, short, int or char.

Methods that perform common tasks and do not require objects are called _____ methods.

static

Sum the odd integers between 1 and 99, using for statement. Assume that the integer variables sum and count have been declared.

sum = 0; for (count = 1; count <= 99; count += 2) sum += count;

The _____ statement selects among multiple actions based on the possible values of an integer variable or expression or a String

switch

switch

switch is a decision structure that tests a variable for a range of values and actions associated with those values.


Ensembles d'études connexes

The Biggest Animal on the Planet

View Set

Computer Input - Assignment (ALA)Assignment

View Set

SVSU Psych Quiz Qs 2, 8, 3, 4, 5

View Set

Chapter 2 IP Addressing and Related Topics

View Set