About Question enthuware.ocajp.i.v7.2.916 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.916 :
Good day. In this question also may create for loop.
for(;!isDone();){}
for(;!isDone();){}
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
That's a good point. Seems like a hack but works.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.916 :
In the code from the question, the contents of the loop will always be run at least once before checking "isDone()" at the end. If we replace the original loop with "for(;!isDone();){}" we're not guaranteed to run the loop at least once, because the question says nothing about what "isDone()" actually does. For all we know it might return true even before reaching the loop. So not only does it look sort of look like a hack, but it might not give the same result either.
-
- Posts: 27
- Joined: Thu Feb 07, 2013 9:46 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
This question seems to have two answers as both of below code will work & is more readable. I don't know why do..while is only correct here.
for(;!isDone();) {//additional valid code}
do{//additional valid code}while( !isDone() );
for(;!isDone();) {//additional valid code}
do{//additional valid code}while( !isDone() );
Last edited by Sweetpin2 on Thu Mar 07, 2013 4:11 pm, edited 1 time in total.
-
- Posts: 27
- Joined: Thu Feb 07, 2013 9:46 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
Below is a sample code to justify my answer
public class For_Test {
static int i = 0;
public static void main(String[] args) {
for(;!isDone();)
{
System.out.println("Hello");
}
do
{
System.out.println("Hello World");
}while(!isDone());
}
static boolean isDone()
{
i++;
if(i==1) return false;
else return true;
}
}
public class For_Test {
static int i = 0;
public static void main(String[] args) {
for(;!isDone();)
{
System.out.println("Hello");
}
do
{
System.out.println("Hello World");
}while(!isDone());
}
static boolean isDone()
{
i++;
if(i==1) return false;
else return true;
}
}
Re: About Question enthuware.ocajp.i.v7.2.916 :
Now I can't remember the question exactly, but the question doesn't say anything about isDone() does it? This means that when you use a for-loop, you're not guaranteed to the run code within the brackets, while using the do-while-loop, you're guaranteed to run the code at least once?
-
- Posts: 6
- Joined: Wed Apr 20, 2016 9:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
The explanation for the for-loop is wrong. In case you choose to use a for-loop instead of a do-while-loop you would have to code it like this:
...a for-loop without initialization block and update statement acts just the same as a while-loop.
The question now is: Is a while-loop the same as a do-while-loop?
Code: Select all
for(;true;){
//additional valid code
if(isDone()) break;
}
The question now is: Is a while-loop the same as a do-while-loop?
-
- Posts: 17
- Joined: Wed Jan 07, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
The difference for while-loop and do-while-loop is:Jokumo♫ wrote:The explanation for the for-loop is wrong. In case you choose to use a for-loop instead of a do-while-loop you would have to code it like this:...a for-loop without initialization block and update statement acts just the same as a while-loop.Code: Select all
for(;true;){ //additional valid code if(isDone()) break; }
The question now is: Is a while-loop the same as a do-while-loop?
For while-loop, it will check the condition first, if condition matches, then execute the code that inside the while block.
For do-while-loop, it will always run at least once, then check the condition to check does the block of code need to run again or not
-
- Posts: 1
- Joined: Sun Oct 30, 2016 10:59 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
Your absolutely right on the way the do-while and while loop work. However, in this case you have to pay close attention to the while(true). The condition is true and will always be true (causing an infinite loop). This means there is no chance the while loop won't run at least once.zhengye1 wrote:The difference for while-loop and do-while-loop is:Jokumo♫ wrote:The explanation for the for-loop is wrong. In case you choose to use a for-loop instead of a do-while-loop you would have to code it like this:...a for-loop without initialization block and update statement acts just the same as a while-loop.Code: Select all
for(;true;){ //additional valid code if(isDone()) break; }
The question now is: Is a while-loop the same as a do-while-loop?
For while-loop, it will check the condition first, if condition matches, then execute the code that inside the while block.
For do-while-loop, it will always run at least once, then check the condition to check does the block of code need to run again or not
Therefore, in this case the program functionality is similar even though functionality of the loops are different.
-
- Posts: 15
- Joined: Tue Feb 25, 2014 3:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
Your code example is written so that isDone will return true at least once. What if isDone always returned false? Or what if your first value was 1? Then the for loop would not behave the same as a do-while nor will it behave the same as the bad example we are trying to correct in the question.Sweetpin2 wrote:Below is a sample code to justify my answer
public class For_Test {
static int i = 0;
public static void main(String[] args) {
for(;!isDone();)
{
System.out.println("Hello");
}
do
{
System.out.println("Hello World");
}while(!isDone());
}
static boolean isDone()
{
i++;
if(i==1) return false;
else return true;
}
}
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
Just wondering what type of a code would have made "use a for loop" the correct answer?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
for is perfectly suitable when you need to have an iteration variable and you want to use it inside the loop. Something like this:
Code: Select all
for(int i=0; i<10; i++){
array[i] = i*i; //using iteration variable to initialize all array elements
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
Actually I was more referring to the fact that the following explanation was given, but the answer itself was NOT chosen as the right one.
"The following is how it can be done using a for loop: for(;!isDone();) { //additional valid code }"
I wanted to find out what kind of code in the question, would have made the above answer, rather than the do-while, the correct answer?
Thanks
"The following is how it can be done using a for loop: for(;!isDone();) { //additional valid code }"
I wanted to find out what kind of code in the question, would have made the above answer, rather than the do-while, the correct answer?
Thanks
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.916 :
If the objective is to improve readability, this code wouldn't ever be the correct answer even though it is a valid piece of code.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 93 guests