Lec10: Do-While Loops Flashcards
If the compiler encounters a statement that uses a variable before the variable is declared, an error will result.
True, this is known as a scope problem.
This type of loop will always be executed at least once.
Post-test loop (do-while loop)
Data-Validation: For error-checking to be truly useful, we need to allow the user to re-enter their input until they get it right. This requires repetition in general and specifically, a loop. Fill in the blank for the do-while condition so that the user stays inside the loop ONLY if they enter an invalid value. Notice that the blank will be repeated in BOTH the if statement and the while loop condition.
do{
System.out.print(“Enter an even integer: “);
number = keybd.nextInt();
if(___________________________)
System.out.println(“Bad input. Try again!”);
}while(____________________________);
System.out.println(“Thank you!”);
number % 2 != 0
How many times will the following do-while loop be executed?
int x = 11;
do
{
x += 20;
} while (x > 100);
1
What will be the value of x after the following code is executed?
int x = 10;
do
{
x *= 20;
}while (x > = 200);
Infinite loop
How many times will the following do-while loop be executed?
int x = 11;
do
{
x += 20;
}while (x <= 100);
5
This type of loop is ideal in situations where you always want the loop to iterate at least once.
do-while loop
The do-while loop must be terminated with a semicolon.
True
Which of the following code fragments will NOT print out 53 asterisks on one line?
(a)int count = 0; (b) int count = 0;
do{ do{
System.out.print(“”); System.out.print(“”);
}while (++count < 53); }while (count++ < 53);
(c)int count = 1; (d) int count = 1;
while(count++ <= 53) while(count++ < 54)
{ System.out.print(“”);
System.out.print(“”);
}
(b)
Data-Validation: For error-checking to be truly useful, we need to allow the user to re-enter their input until they get it right. This requires repetition in general and specifically, a loop. Fill in the blank for the while condition so that the user stays inside the loop ONLY if they enter an invalid value. Suppose we need the user to enter an integer between 1 and 5. Choose the correct condition for the while loop.
System.out.print(“Enter an integer from 1-5: “);
number = keybd.nextInt();
while(____________________________)
{
System.out.println(“Bad input. Try again!”);
System.out.print(“Enter an integer from 1-5:”);
number = keybd.nextInt();
}
System.out.println(“Thank you!”);
number < 1 || number > 5
What is the output of the following Java code?
int num = 10;
while(num > 10)
num = num - 2;
System.out.println(num);
10
What is the output of the following Java code?
int x = 1;
do{
System.out.print(x + “ “);
} while( x > 0);
System.out.println();
A. 1
B. 1 0 -1
C. 1 0
D. None of these
D. None of these.
Suppose sum and num are int variables, and the input captured from the keyboard (in the order shown) from the code below is :
18 25 61 6 -1
What is the output of the following code? (Assume that console is the name of a Scanner object named initialized to the standard input device.)
sum = 0;
System.out.print(“Enter a number: “);
num = console.nextInt( );
while( num != -1)
{
sum =sum + num;
System.out.print(“Enter a number: “);
num = console.nextInt( );
}
System.out.println(sum);
110
What is the output of the following Java code?
count = 1;
num = 25;
while( count < 25)
{
num = num - 1;
count ++;
}
System.out.println(count + “ “ + num);
25 1
What is the output of the following program segment?
int x = 14;
int y = 60;
while( (y - x) % 3 != 0)
{
System.out.print(y + “ “);
y = y - 5;
}
System.out.println( );
60 55