About Question enthuware.ocajp.i.v7.2.1280 :
Moderator: admin
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Yes, sorry, I meant x==0 is true.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
I tried to run it by adding package and main method like this:
I'm not getting any output. What is wrong with the code?
(I'm a newbie. Studied and then started with mocks but new to practical coding. Please help!)
(When I tried to include loopTest within the main method, there were other errors, like: loopTest cannot have void return type.) So, how could I change the code?
Code: Select all
package happeningPackage;
public class TestClass {
public static void main(String[] args) {}
public void loopTest(int x){
loop: for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
System.out.println(i);
if (x == 0) { continue loop; }
System.out.println(j);
}
}}}
(I'm a newbie. Studied and then started with mocks but new to practical coding. Please help!)
(When I tried to include loopTest within the main method, there were other errors, like: loopTest cannot have void return type.) So, how could I change the code?
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Please post exact error message that you get. Screenshot would be even better.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Here is the snap when I ran it.
(Hope its not too difficult to read. Had to reduce resolution and compress it to keep it within the limit:256kb)
(Hope its not too difficult to read. Had to reduce resolution and compress it to keep it within the limit:256kb)
- Attachments
-
- Screen Shot 2018-03-10 at 7.23.08 PM.zip
- (249.13 KiB) Downloaded 570 times
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
I don't see any meaningful information in the Eclipse screen shot that you've posted.
As I mentioned to you before in another thread, you need to use the command line to compile and run the code. So please try compiling it on command line and post the exact error message that you get.
If you are a beginner, you should simply avoid using any IDE at this stage.
As I mentioned to you before in another thread, you need to use the command line to compile and run the code. So please try compiling it on command line and post the exact error message that you get.
If you are a beginner, you should simply avoid using any IDE at this stage.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Yeah. I'll do that, although I have managed to run many other programs on eclipse. I couldn't figure as to why this wasn't giving any output
Thank you, for all the guidance though!
Thank you, for all the guidance though!
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Well, from the code that you've posted the main method is empty. I don't see loopTest method being called. So no output.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Oh right. Thank you..
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Why is that the compiler does not complain about the System.out.println(j); being unreachable?
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Please go through this thread from the beginning. It is explained in detail.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Dec 21, 2018 4:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
When x is 0, the statement continue loop; is executed. Note that loop: is for the outer loop.
Can anyone explain this to me:
loop: for (int i = 1; i < 5; i++){
I'm used to simply seeing nested for loops.
But this one have "loop:" before it. How does it work? thank you.
Can anyone explain this to me:
loop: for (int i = 1; i < 5; i++){
I'm used to simply seeing nested for loops.
But this one have "loop:" before it. How does it work? thank you.
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
You can apply a label to any statement. Since a for statement is just like any other statement, you can apply a label to a for statement as well. That is exactly what loop: is in this case.
You can read more about labels here: https://howtodoinjava.com/java/basics/l ... s-in-java/
Labels are use mostly in conjunction with break and continue statements. You can read more about it here: https://www.journaldev.com/980/java-bre ... ment-label
You can read more about labels here: https://howtodoinjava.com/java/basics/l ... s-in-java/
Labels are use mostly in conjunction with break and continue statements. You can read more about it here: https://www.journaldev.com/980/java-bre ... ment-label
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
So I tried the code by making x final. It still doesn't give unreachable code error.Below is my codeadmin wrote: ↑Mon Jul 04, 2016 1:25 amThink about it this way. How do "you" know that it is unreachable? You know it because you know that x is 0 and x==0 will be false. You have executed the code in your mind and made that determination.
But the compiler doesn't know that x will be 0 at runtime. It cannot execute code. It can only make use of the information that it is 100% sure about at compile time. This means if you have a final variable, then the compiler can make use of that fact because it knows that its value will be the same at run time also. But a normal variable's value can be different.
You may go through this interesting discussion: http://stackoverflow.com/questions/3795 ... iler-error
public class TestClass6{
final int x=0;
public void loopTest(){
loop: for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
System.out.println(i);
if (x == 0) { continue loop; }
System.out.println(j);
}
}
}
public static void main(String args[]){
new TestClass6().loopTest();
}
}
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Please go through the SOF link given in the post that you've quoted. if statement is an exception. Java language allows an unreachable if block to support conditional compilation.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Thank you . Yeah now I remember from other post of your's.if statement is an exception. I understand now. So for infinite loop case if there is statement after the loop ,it is unreachable . But for if condition it is exception. Is my understanding right?
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Yes, correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 26
- Joined: Wed Oct 02, 2019 1:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Okay, I understand the question just fine, and I got it right, but I like reading the explanations just in case I come across a small detail I was unaware of, and I can't understand what's being said here:
The bolded bit makes no sense to me. I don't know if there's a word missing or if there's a grammar error or if my brain's just having issues parsing it, but I can't figure out what you mean.So, only one iteration (that too not full) is performed for the inner loop.
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
The only iteration that occurs for this loop is not executed fully because of if (x == 0) { continue loop; } part. The last statement of the inner loop is not executed.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Mar 08, 2020 3:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
The code and is unfamiliar to me.
I assume this is a label or the language now allows naming a loop (and here the name used is loop). I cannot figure out what to call this language feature in order to search for it and learn more. What is it?
Code: Select all
loop:
Code: Select all
continue loop;
I assume this is a label or the language now allows naming a loop (and here the name used is loop). I cannot figure out what to call this language feature in order to search for it and learn more. What is it?
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Yes, loop is a label. continue <label>; can be found under labelled break and continue section.
https://docs.oracle.com/javase/tutorial ... ranch.html
https://docs.oracle.com/javase/tutorial ... ranch.html
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
In another question (dont remember the number unfortunately) there was a similar case with break labels. In that question it said that the label needs to be in the same context as the break/continue statement, otherwise it becomes unreachable / not known to the break/continue statement.
If my memory is correct, the example was very very similar. And I would have thought that here, in this question, the continue loop; statement does not see the loop label as it is outside of another FOR loop.
Could you please comment on this? Maybe you know which question I mean? It would be great if you could explain the difference or a clear explanation in what context (i.e. how far) labels are known.
Thanks a lot for your hel
If my memory is correct, the example was very very similar. And I would have thought that here, in this question, the continue loop; statement does not see the loop label as it is outside of another FOR loop.
Could you please comment on this? Maybe you know which question I mean? It would be great if you could explain the difference or a clear explanation in what context (i.e. how far) labels are known.
Thanks a lot for your hel
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
You might have seen something similar in the explanation to question 2.1281. But it is not the same. That explanation is talking about the location of break and continue with labels.
If you use a break/continue with a label, then that label must be on a block that is enclosing the break/continue statement.
You can apply a label to any statement in general. There is no restriction. But you can't just break to any label. (otherwise, that would amount to a goto statement, which, of course, java does not have.)
If you use a break/continue with a label, then that label must be on a block that is enclosing the break/continue statement.
You can apply a label to any statement in general. There is no restriction. But you can't just break to any label. (otherwise, that would amount to a goto statement, which, of course, java does not have.)
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
OK, thanks for the explanation. The question you refer to is not the one I meant. If I find it again, I will let you know.
But how is it defined, what a block is, where it starts and where it ends??
Cannot find clear information about this.
Here, if I remember correctly, the break statement was in a loop. The label before the for loop. Seems to me like two different blocks. I am missing clear borders/clear understanding where this starts and ends.
Can you point me in the right direction, please?
But how is it defined, what a block is, where it starts and where it ends??
Cannot find clear information about this.
Here, if I remember correctly, the break statement was in a loop. The label before the for loop. Seems to me like two different blocks. I am missing clear borders/clear understanding where this starts and ends.
Can you point me in the right direction, please?
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
You may go through section 14.15 The break Statement of JLS 11 (or the section with the same title in JLS 8).
Observe this statement from this section:
Observe this statement from this section:
You may check out the details of the continue statement as well in the same chapter.A break statement must refer to a label within the immediately enclosing method, constructor, or initializer. There are no non-local jumps. If no labeled statement with Identifier as its label in the immediately enclosing method, constructor, or initializer contains the break statement, a compile-time error occurs.
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
http://enthuware.com/forum/viewtopic.php?f=2&t=2236
This is the question I was referring to. And now I understand the difference. Because JILL is not followed by brackets {}, its scope is only the following statement (the println()) which doesnt make sense as you can never actually break to this label. So in order to make this work, one would have to add brackets after JILL and include the if and else part).
This is the question I was referring to. And now I understand the difference. Because JILL is not followed by brackets {}, its scope is only the following statement (the println()) which doesnt make sense as you can never actually break to this label. So in order to make this work, one would have to add brackets after JILL and include the if and else part).
Who is online
Users browsing this forum: No registered users and 101 guests