About Question enthuware.ocajp.i.v7.2.1032 :
Moderator: admin
- 
				deepa.patre
- Posts: 15
- Joined: Thu Dec 13, 2012 9:44 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Hi,
i didn't understand one point. Why does the value of i remain as 0 not changing to 1?
i know it is post increment, but according to post increment the value of i changes when accessed next.... n also if i replace i++ with ++i there is no change in the output... please help me in understanding....
			
			
									
									
						i didn't understand one point. Why does the value of i remain as 0 not changing to 1?
i know it is post increment, but according to post increment the value of i changes when accessed next.... n also if i replace i++ with ++i there is no change in the output... please help me in understanding....
- 
				admin
- Site Admin
- Posts: 10441
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Your understanding is correct. But in this case the outer loop executes only once and it breaks before starting the next iteration. so i++ is never executed.deepa.patre wrote:Hi,
i didn't understand one point. Why does the value of i remain as 0 not changing to 1?
i know it is post increment, but according to post increment the value of i changes when accessed next.... n also if i replace i++ with ++i there is no change in the output... please help me in understanding....
- 
				zombie.face
- Posts: 1
- Joined: Sun Feb 02, 2014 10:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Hi,
What is the exit condition of the first loop? I am confused. Why the first loop exits?
			
			
									
									
						What is the exit condition of the first loop? I am confused. Why the first loop exits?
- 
				admin
- Site Admin
- Posts: 10441
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
It exits because of the statement break lab1;zombie.face wrote:Hi,
What is the exit condition of the first loop? I am confused. Why the first loop exits?
lab1 points to the first i.e. the outer loop. So that causes the outer loop to break irrespective of its condition.
- 
				alkour
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
In the explanation, is written that values of i an j changes as follows:
i = 0 j = 5
i = 0 j = 4
i = 0 j = 3
.....
As the inner loop with changing j has prefix --j
for( ; ; --j) if (i > j) break lab1;
I thought that first iteratin would start with:
i = 0 j = 4.
Not with j=5.
Am I correct?
			
			
									
									
						i = 0 j = 5
i = 0 j = 4
i = 0 j = 3
.....
As the inner loop with changing j has prefix --j
for( ; ; --j) if (i > j) break lab1;
I thought that first iteratin would start with:
i = 0 j = 4.
Not with j=5.
Am I correct?
- 
				admin
- Site Admin
- Posts: 10441
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Try printing the value of i in the loop 
			
			
									
									
						
- 
				alkour
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Yes. It is correct.
Output 
And it does not matter whether it will be prefix --j and postfix j++ in for loop.
It works the same.
			
			
									
									
						Code: Select all
    public static void main(String[] args) {
        int i = 0, j = 5;
        lab1: for ( ; ; i++){
            System.out.println("Outer loop: i = " + i +", j = "+ j); 
            for (; ; --j) { 
                System.out.println("Inner loop: i = " + i +", j = "+ j);
                if (i > j ) break lab1;
            }
        } 
        System.out.println(" i = " + i +", j = "+ j); 
    }   
}Code: Select all
Outer loop: i = 0, j = 5
Inner loop: i = 0, j = 5
Inner loop: i = 0, j = 4
Inner loop: i = 0, j = 3
Inner loop: i = 0, j = 2
Inner loop: i = 0, j = 1
Inner loop: i = 0, j = 0
Inner loop: i = 0, j = -1
 i = 0, j = -1
It works the same.
- 
				rafaparche
- Posts: 5
- Joined: Tue Apr 14, 2015 9:32 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Hello!
Why Eclipse mark i++ as dead code and --z as normal code?
Thanks!
			
			
									
									
						Why Eclipse mark i++ as dead code and --z as normal code?
Thanks!
- 
				admin
- Site Admin
- Posts: 10441
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Can't really comment on what IDEs do. For the exam, we recommend using the command line.
			
			
									
									
						- 
				Sovember
- Posts: 1
- Joined: Wed Nov 25, 2015 12:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Hi, sorry probably a very basic misunderstanding for me, just beginning studying java. 
Can I ask why it only goes through the first iteration of the loop? I thought the break statement only ran if the if statement conditions were met (i > j) ? Isn't i less than j?
			
			
									
									
						Can I ask why it only goes through the first iteration of the loop? I thought the break statement only ran if the if statement conditions were met (i > j) ? Isn't i less than j?
- 
				vlezz94
- Posts: 12
- Joined: Wed Sep 28, 2016 6:31 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Hi Paul!
Right now I'm struggling to understand why in this code the value of "i" is never incremented:
Also, in one of the previous tests I came across another question that had loop labels. In that question the "break -label-;" was nested too but it didn't compile. Why in this code compiles?
			
			
									
									
						Right now I'm struggling to understand why in this code the value of "i" is never incremented:
Code: Select all
public class BreakTest{
  public static void main(String[] args){
    int i = 0, j = 5;
    lab1 : for( ; ; i++){
      for( ; ; --j)  if( i >j ) break lab1;
    }
    System.out.println(" i = "+i+", j = "+j);
  }
}- 
				admin
- Site Admin
- Posts: 10441
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
i never gets incremented because the outer for loop breaks in the middle of the first iteration itself. The increment part of the outer for loop (i++)  never gets to execute.
Observe the inner loop. It keeps decrementing j until i becomes equal to j and then it breaks the outer loop using a labeled break.
You will need to post the code from the other question for me to help you with it because I do not remember the question.
HTH,
Paul.
			
			
									
									
						Observe the inner loop. It keeps decrementing j until i becomes equal to j and then it breaks the outer loop using a labeled break.
You will need to post the code from the other question for me to help you with it because I do not remember the question.
HTH,
Paul.
- 
				vlezz94
- Posts: 12
- Joined: Wed Sep 28, 2016 6:31 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
This is the code (The correct answer is that it doesn't compile):
So, when I run the program the i variable value is never incremented because there is a "break" statement. And if it wasn't in there the loop would be infinite and also it would never be incremented as well. I think I got it, thanks Paul!
			
			
									
									
						Code: Select all
void crazyLoop(){
   int c = 0;
   JACK: while (c < 8){
       JILL: System.out.println(c);
       if (c > 3) break JILL; else c++;
   }
}
So, when I run the program the i variable value is never incremented because there is a "break" statement. And if it wasn't in there the loop would be infinite and also it would never be incremented as well. I think I got it, thanks Paul!
- 
				admin
- Site Admin
- Posts: 10441
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Ok, in this code, the label JILL does not identify a loop, so when you call break JILL, there is nothing to break and so the compiler complains.
			
			
									
									
						- 
				dserge5etd
- Posts: 1
- Joined: Tue May 04, 2021 5:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
Hi, please help with another task 11.2.3545 :admin wrote: ↑Tue Nov 08, 2016 8:25 ami never gets incremented because the outer for loop breaks in the middle of the first iteration itself. The increment part of the outer for loop (i++) never gets to execute.
Observe the inner loop. It keeps decrementing j until i becomes equal to j and then it breaks the outer loop using a labeled break.
You will need to post the code from the other question for me to help you with it because I do not remember the question.
HTH,
Paul.
Code: Select all
        int i=0, j=0;
        X1: for(i = 0; i < 3; i++){
            X2: for(j = 3; j > 0; j--){
                if(i < j) continue X1;
                else break X2;
            }
        }
        System.out.println(i+" "+j);
    }also true for the following code X1: for(i = 0; i < 3; i++){ , but the right answer of 11.2.3545 : 3 3i never gets incremented because the outer for loop breaks in the middle of the first iteration itself. The increment part of the outer for loop (i++) never gets to execute.
- 
				admin
- Site Admin
- Posts: 10441
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1032 :
In 2.3545, the statement break X2; breaks the inner loop because the label X2 is applied on the inner loop. But in 2.1032, break lab1; breaks the outer loop because the label lab1 is applied on the outer loop.
			
			
									
									
						Who is online
Users browsing this forum: No registered users and 97 guests