Chapter 6 More Conditional and Loops Flashcards
When a Java program is running, what happens if the expression evaluated for a switch statement does not match any of the case values
associated with the statement?
When a Java program is running, if the expression evaluated for a
switch statement does not match any of the case values associated
with the statement, execution continues with the default case. If no
default case exists, processing continues with the statement following
the switch statement.
What happens if a case in a switch statement does not end with a
break statement?
If a case does not end with a break statement, processing continues into
the statements of the next case. We usually want to use break statements in order to jump to the end of the switch.
What is the output of the GradeReport program if the user enters 72?
What if the user enters 46? What if the user enters 123?
If the user enters 72, the output is: That grade is average. If the user
enters 46, the output is: That grade is not passing. If the user enters
123, the output is: That grade is not passing.
Transform the following nested if statement into an equivalentswitch statement.
if (num1 == 5) myChar = 'W'; else if (num1 == 6) myChar = 'X'; else if (num1 == 7) myChar = 'Y'; else myChar = 'Z';
An equivalent switch statement is: switch (num1) { case 5: myChar = 'W'; break; case 6: myChar = 'X'; break; case 7: myChar = 'Y'; break; default: myChar = 'Z'; }
What is the difference between a conditional operator and a conditional statement
The conditional operator is a trinary operator that evaluates a condition
and produces one of two possible results. A conditional statement, such as the if and switch statements, is a category of statements that allow conditions to be evaluated and the appropriate statements executed as a result.
Write a declaration that initializes a char variable named id to ‘A’ if the boolean variable first is true and to ‘B’ otherwise.
char id = (first) ? ‘A’ : ‘B’;
Express the following logic in a succinct manner using the conditional operator.
if (val <= 10)
System.out.println(“The value is not greater than 10.”);
else
System.out.println(“The value is greater than 10.”);
System.out.println(“The value is “ + ((val <= 10) ? “not” :
“”) + “greater than 10.”);
Compare and contrast a while loop and a do loop
A while loop evaluates the condition first. If it is true, it executes the
loop body. The do loop executes the body first and then evaluates the
condition. Therefore, the body of a while loop is executed zero or more
times, and the body of a do loop is executed one or more times
What output is produced by the following code fragment? int low = 0, high = 10; do { System.out.println(low); low++; } while (low < high);
The output is the integers 0 through 9, printed one integer per line.
What output is produced by the following code fragment? int low = 10, high = 0; do { System.out.println(low); low++; } while (low <= high)
The code contains an infinite loop. The numbers 10, 11, 12, and so on
will be printed until the program is terminated or until the number gets
too large to be held by the int variable low.
Write a do loop to obtain a sequence of positive integers from the user, using zero as a sentinel value. The program should output the sum of the numbers.
Scanner scan = new Scanner(System.in); int num, sum = 0; do { System.out.print("enter next number (0 to quit) > "); num = scan.nextInt(); sum += num; } while (num != 0); System.out.println(sum);
When would we use a for loop instead of a while loop?
A for loop is usually used when we know, or can calculate, how many
times we want to iterate through the loop body. A while loop handles
a more generic situation
What output is produced by the following code fragment?
int value = 0; for (int num = 10; num <= 40; num += 10) { value = value + num; } System.out.println(value);
The output is: 100
What output is produced by the following code fragment? int value = 0; for (int num = 10; num < 40; num += 10) { value = value + num; } System.out.println(value);
The output is: 60
What output is produced by the following code fragment?
int value = 6;
for (int num = 1; num <= value; num ++)
{
for (int i = 1; i <= (value — num); i++)
System.out.print(“ “);
for (int i = 1; i <= ((2 * num) — 1); i++)
System.out.print(“*”);
System.out.println();
}
The output is: * *** ***** ******* ********* ***********