• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

regarding CONTINUE

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers I am trying to check CONTINUE STATEMENT with a method return, I have one example, do you know how to deal with method result and then check that result in IF statement whether it's as expected or not if not then you continue otherwise whatever.

here it goes..

class Exam {

void display() {
int i;
for(i=0; i<5; i++)
{
if(Exam.add() == 2) // well thats what i thought, may be
// wrong,its showing error saying
// unreachable code
{
continue;
System.out.println("Hello.");
}
}
}

int add(){

int sum = 2;
return sum;
}

}

The idea behind this is , i want to continue if i get the correct result of the method i check first.

- thanks
- chintan
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, you need to change the way you call the add() method.

The reason that it's giving you an unreachable code error is that there is no way for the System.out.println statement to be executed because it directly follows the continue statement.

I think what you want to do is just make the continue statement the body of the if.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1- You can't call instance method add() in a way static methods (class method) are called. Either make add() method static or call it using instance of the Exam class.

2- After that, the main continue issue,
line
System.out.println("Hello.");
is not reachable. Because before that there is a continue statement. The compiler is 100% sure that it would be injustice with the print statement, because there wont be any chance, it could be executed.


With Regards,
cmbhatt
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree about the error i have discussed but then it's going to be very limited use of continue i mean it's for only one condition, the ceasing statement, how would i add up to get it done instead of checking just one condition. to me then it's very limited use of continue.

can you elaborate more on that giving example , that would be very clear to me
thanks

- chintan
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chintan,

See the code below:



Now the compiler is sure about print statement that under some condition only there is continue, otherwise println() will be executed. Compiler catches you doing partiality with any statement (saying Oh very clever! you are writing some statements after plain "continue" but leaving no chance to get them executed). Compiler slaps you with not reachable code error.


Regards,
cmbhatt
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello CM Bhatt

thanks for your pretty much clear example, i just want to make sure with one thing that when condition will be true in if clause and we have continue down line, it just ceases whatever gets matched in the IF statement.. right ? did i get it right or i need to add up something more. so in our case output will "2 times print statements thats all right "..

Thanks in advance

- chintan
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i thought boolean condition till 5 only.

if its till 10 then its gonna be 6 times right ceasing statements at 0, 3 and 5 right
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Chintan,

It will cease to print exactly three times, when it encounter if condition true.



Regards,
cmbhatt
 
mooooooo ..... tiny ad ....
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic