Reputation: 23
For my Java class, I was asked to create a while loop and then convert it to a do-while loop and then compare the output. This is the question:
- Replace the while loop in question 4 with a do while loop. What is the possible difference between the output from question 4 and this question?
I can't find a difference in the output, possible or not. Here is the code and output for both below.
while loop
package fordemo;
import java.util.Scanner;
public class ForDemo {
public static void main(String[] args) {
System.out.println("Input a number:");
Scanner user_input = new Scanner (System.in);
int number = user_input.nextInt();
int n = -1;
while (n < number){
n=n+2;
System.out.print(n + " ");
}
}
}
run:
Input a number:
15
1 3 5 7 9 11 13 15 BUILD SUCCESSFUL (total time: 1 second)
do-while loop
package fordemo;
import java.util.Scanner;
public class ForDemo {
public static void main(String[] args) {
Scanner user_input = new Scanner (System.in);
System.out.println("Input a number:");
int number = user_input.nextInt();
int n = -1;
do {
n+=2;
System.out.print(n + " ");
}
while (n < number);
}
}
run:
Input a number:
11
1 3 5 7 9 11 BUILD SUCCESSFUL (total time: 1 second)
Upvotes: 0
Views: 676
Reputation: 882566
The difference between while (condition) {something}
and do {something} while (condition)
is that the latter always executes at least once.
The former checks the condition before the iteration, the latter checks it afterwards.
If you enter -1
or less, the former will give you nothing and the latter will give you 1
.
In its simplest form, you can look at the difference between the two lines:
while (false) { System.out.println("x"); }
do { System.out.println("x"); } while (false);
The former complains bitterly about unreachable code because it never enters the loop. The latter does not complain because it runs the loop once.
Upvotes: 5
Reputation: 6515
first test the condition then execute the body
while (n < number){ // Top tested loop or (pre test loop)
n=n+2;
System.out.print(n + " ");
}
first execute the body of the loop and then test the condition;
do {
n+=2;
System.out.print(n + " ");
}
while (n < number); // Bottom tested loop (post test loop)
Upvotes: 0
Reputation: 2622
The first time running the while loop, n is set to -1, then it checks if n
In the do-while loop your code sets n to -1, then raises it with 2. Prints the line and after that it checks if n
So if you input is -1 the while loop wont print anything (because -1 < -1 evaluates to false). But your do-while loop will print 1
Upvotes: 0
Reputation: 15708
The while statement continually executes a block of statements while a particular condition is true. The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, in your case if you input value less then -1
while loop
will executes 0
times whereas do-while
loop will executes 1 times
Upvotes: 0
Reputation: 533
A do while loop will execute at least once whereas a while loop may not even execute even once if its conditions are not met. The do while starts checking its condition after the first iteration.
Upvotes: 1