About Question enthuware.ocajp.i.v7.2.1044 :

Moderator: admin

Post Reply
thejamiebyrne
Posts: 1
Joined: Mon Oct 28, 2013 5:02 am
Contact:

About Question enthuware.ocajp.i.v7.2.1044 :

Post by thejamiebyrne » Mon Oct 28, 2013 7:43 am

After a lot of study I am still unsure of the difference between pre and post increment operator
when used in a loop.

In the question j is pre incremented in the 'for' statement but in the answer j is not incremented until 1. the statement is evaluated and 2. the loop is executed.

So am I correct in saying there is no difference between a pre and post increment in a for loop statement.



public class TestClass{
public static void main(String args[]){
int i;
int j;
for (i = 0, j = 0 ; j < 1 ; ++j , i++){
System.out.println( i + " " + j );
}
System.out.println( i + " " + j );
}
}

Answer: 0 0 will be printed followed by 1 1.

j will be less than 1 for only the first iteration. So, first it will print 0, 0. Next, i and j are incremented.   Because j is not less than 1 at the start of the loop, the condition fails and it comes out of the loop. Finally, it will print 1,1.

This should clear it up in my head :?

admin
Site Admin
Posts: 8248
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1044 :

Post by admin » Mon Oct 28, 2013 7:55 am

pre or post increment don't make any difference to the for statement all all because in a for statement the condition part is separate from the increment part.

pre/post make a difference in a statement like this:
int i = 0;
int j = ++i; // j will be set to 1 because of pre-increment. I.e. you are incrementing first and then using the value of i to assign it to j.
i = 0;
int k = i++; // j will be set to 0 because of post-increment. I.e. you are using the value of i to assign it to k first and then incrementing,

Now, look at this code:

Code: Select all

int i=0;
for(;++i<1;){
 System.out.println(i);
}
 System.out.println(i);
}
Run it with pre and post increment and see this difference.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Zoryanat
Posts: 20
Joined: Tue Aug 27, 2013 3:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1044 :

Post by Zoryanat » Mon Dec 02, 2013 5:59 pm

Could you please help me understand why it prints 00 followed by 11?
As far as i understood in this example we have i and j declared in two different scopes: one inside class and one inside for loop. So when we print inside the loop we are talking about i and j that were instantiated inside for loop. And when we print i and j outside of the loop, shouldn't it print 00 again, i mean, i and j were declared in class above for loop and suppose to get default values of zero, and whatever happens inside for loop should not affect them since this is different scope?...

What am I missing here?
Thanks!
Regards
Zoryana

admin
Site Admin
Posts: 8248
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1044 :

Post by admin » Mon Dec 02, 2013 8:01 pm

Where do you see i and j being declared in two scopes? There is only one scope for i and j, and that scope starts when they were declared at the top. Inside the loop, you are changing their values not their scope. That is why their values after the loop are visible after the loop ends.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1044 :

Post by subhamsdalmia » Tue May 19, 2015 8:32 am

As far as my understanding goes,
for loop only executes the incre-decrement of integer values after execution of 1st statement and hence ++j or j++ doesnt make a difference.
for ex,

public class Print1 {

public static void main(String args[]){
int i;
int j;
for (i = 0, j = 0 ; j < 1 ; ++j , i++){
System.out.println( i + " inside loop " + j );
}
System.out.println( i + " exiting the loop " + j );
}
}

prints :
0 inside loop 0
1 exiting the loop 1

public class Print1 {

public static void main(String args[]){
int i;
int j;
for (i = 0, j = 0 ; j < 1 ; ++j , i++)System.out.println( i + " outside loop " + j );{
System.out.println( i + " inside loop " + j );
}
System.out.println( i + " exiting the loop " + j );
}
}


prints :
0 outside loop 0
1 inside loop 1
1 exiting the loop 1

bortnikov_sa@mail.ru
Posts: 8
Joined: Wed Oct 25, 2017 2:53 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1044 :

Post by bortnikov_sa@mail.ru » Fri Nov 17, 2017 4:08 pm

I do not understand how to answer questions in your testing! I know the right answer, I know that 0 0 and 11 will be displayed. But the title of the question says "select ALL the correct answers". And among them there is an answer "11 will be printed once," which is also true. In some questions, there is obviously the right answer, but you need to choose not only it, but also others that are in fact true but do not describe the situation as a whole. Next time, if such a question arises, I'll bring it here. (I pass the test with the option "do not show the number of answers")

admin
Site Admin
Posts: 8248
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1044 :

Post by admin » Fri Nov 17, 2017 9:56 pm

The real exam will tell you the number of correct options. So you can select the best option.
If you use the "do not show the number of answers" options, some questions may become difficult to answer because of the issue you mentioned.
We will work on improving them.
If you like our products and services, please help us by posting your review here.

bortnikov_sa@mail.ru
Posts: 8
Joined: Wed Oct 25, 2017 2:53 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1044 :

Post by bortnikov_sa@mail.ru » Thu Nov 23, 2017 2:59 am

As I promised, I give an example of such a question: viewtopic.php?f=2&t=1411 In it, the logic of choosing the right answers is completely opposite.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests