6.3.7.1. For Loop Bugs 1.
Solution.
Bug: Commas are used instead of semicolons in the header. Loop fixed below.
for (int k = 5; k < 100; k++)
System.out.println(k);
conditional loop | loop bound | sentinel bound |
counting loop | loop entry condition | unit indexing |
do-while statement | nested loop | updater |
infinite loop | postcondition | while statement |
initializer | precondition | zero indexing |
limit bound | priming read | |
loop body | repetition structure |
for
statement has the following syntax: for ( initializer ; loop entry condition ; updater )
{
for loop body
}
Bound | Example |
Counting | k \(\lt\) \(100\) |
Sentinel | input != 9999 |
Flag | done != true |
Limit | amount \(\lt\) \(0.5\) |
while
statement takes the following form: while ( loop entry condition )
{
loop body
}
do-while
statement has the following general form: do
{
loop body
} while ( loop condition );
for (int k = 5; k < 100; k++)
System.out.println(k);
for (int k = 0; k < 12 ; k--)
System.out.println(k);
for
loop that prints the following sequence of numbers: 1, 5, 9, 13, 17, 21, 25. for (int k = 1; k <= 25; k = k+4)
System.out.print(k + " ");
for
loop to print the following geometric pattern: #
# #
# # #
# # # #
# # # # #
for (int row = 1; row <= 5; row++) { // For each row
for (int col = 1; col <= row; col++) // Columns per row
System.out.print('#');
System.out.println(); // New line
} // row
int k = 5;
while (k < 20) {
System.out.println(k);
k++ << Missing semicolon
}
int k = 0;
while (k < 12;) { << Extra semicolon
System.out.println(k);
k++;
}
int k = 0;
while (k < 10)
{
System.out.println(k);
k++; // add increment counter k and { } in loop body.
}
int k = 0; << Missing initializer
while (k < 10) {
System.out.println(k);
k++;
}
while
loop that prints the following sequence of numbers: 0, 6, 12, 18, 24, 30, 36. int k = 0; // Initializer
while (k <= 36) { // Loop-entry condition
System.out.println(k);
k += 6; // Updater
}
while
statement. public static void sub1Div2(int N) {
while(N != 0) {
System.out.print(N + " ");
if (N % 2 == 0)
N = N / 2;
else
N = (N - 1) / 2;
}
System.out.println( N );
} // sub1Div2()
int k = 0;
do while (k < 100) << Misplaced condition
{
System.out.println(k);
k++;
} << Belongs here
int k = 0;
do {
System.out.println(k);
k++;
} while (k < 12) << Missing semicolon
do-while
loop that prints the following sequence of numbers: 1, 8, 15, 22, 29, 36, 43. n = 1; // Initializer
do {
System.out.print(n + " ");
n += 7; // Updater
} while (n <= 43); // Loop re-entry condition
public int getAndValidatePizzaPrice() { // Uses KeyboardReader
int pizza = 0;
do {
reader.prompt("Input a pizza price (8, 10, or 15) ");
reader.prompt("or 99 to end the list >> ");
pizza = reader.getKeyboardInteger();
if ((pizza != 99) && (pizza != 8) && (pizza != 10) && (pizza != 15))
System.out.println("Error: you've entered an "
+ "invalid pizza price\n"); // Error input
else // OK input
System.out.println("You input " + pizza + "\n");
} while ((pizza != 99) && (pizza != 8) && (pizza != 10) && (pizza != 15));
return pizza;
} // getAndValidatePizzaPrice()
public int getAndValidatePizzaPrice() { // Uses KeyboardReader
int pizza = 0;
do {
reader.prompt("Input a 1,2 or 3 to indicate pizza"
+ "price ( 1(8), 2(10), or 3($15) ) ");
reader.prompt("or 0 to end the list >> ");
pizza = reader.getKeyboardInteger();
if ((pizza < 0) || (pizza > 3)) // Error check
System.out.println("Error: you've entered an "
+ "invalid value\n");
else // OK input
System.out.println("You input " + pizza + "\n");
} while ( (pizza < 0) || (pizza > 3) );
if (pizza == 1)
return 8;
else if (pizza == 2)
return 10;
else if (pizza == 3)
return 15;
else
return 0;
} // getAndValidatePizzaPrice()
while
structure, or a do-while
structure should be used, and write a pseudocode algorithm.for each name in the visitor's log
print the name
do-while
structure in which you repeatedly read a number and validate it. do
read a number
if number is invalid,
print error message
while number is invalid
for each character in the Web page address
if it is a backslash
replace it with slash
while
loop to guard against an empty list. initialize maxMPG to smallest possible number
while there are more cars in the database
if current car's MPG is greater than maxMPG
replace maxMPG with it
switch
Multiway Selection Structureint k = 0;
switch (k) // Syntax error: missing braces
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
default:
System.out.println("default");
break;
}
int k = 0;
switch (k + 1)
{
case 0:
System.out.println("zero");
break; // Missing break;
case 1:
System.out.println("one");
break; // Missing break;
default:
System.out.println("default");
break; // Missing break;
}
int k = 6;
switch (k / 3.0) // Syntax error: not an integral value
{
case 2:
System.out.println("zero");
break;
case 3:
System.out.println("one");
break;
default:
System.out.println("default");
break;
}
switch (flavor)
{
case 1:
System.out.println("Vanilla");
break;
case 2:
System.out.println("Chocolate");
break;
case 3:
System.out.println("Strawberry");
break;
default:
System.out.println("Error");
}
int j = 0; k = 5;
do {
if (k % 5 == 0) {
// Precondition: j <= k
j += k;
k--;
}
else k *= k;
} while (j <= k);
// Postcondition: j > k
// Precondition: N >= 0
// Postcondition: power(x,n) == x to the n
public double power(double x, int n ) {
double pow = 1;
for (int k = 1; k <= n; k++)
pow = pow * x;
return pow;
} // power()