java


Core Java Programming


I am writing a code using Eclipse SWT in such a way that if a criteria is met, the execution is advanced further and if not, it should resume to its previous
execution point. Can anyone help?
It looks like this
void main(){
methodA();
methodB();
}
methodA(){
if(x==y)
{
//continue the execution further
}
else
{
//once again go to back the calling method (i.e) methodA() and
//not methodB() as compiler usually does
}
}
Inshort:I need similar thing as goto keyword in C++
Thanks in advance,
Sathish
Are you trying to skip the methodB() call if methodA() fails or are trying to call methodA() again?
private boolean methodA() {
boolean _success = false;
if(x==y)
{
_success = true;
//continue the execution further
} else {
//.....
}
return _success;
}
Then in main()
if (methodA()) {
methodB();
}
Or if you want to call methodA() again in main:
while(!methodA()) {
//...?
}
methodB();
Some References:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
https://docs.oracle.com/javase/tutorial/java/TOC.html
Instead of using goto use a loop. This way you can keep running the code in methodA, until the condition is met, then continue with methodB:
void main(){
methodA();
methodB();
}
methodA(){
while(x!=y)
{
// the body of methodA
}
//continue the execution further
}
Here is a code, you can use.
public class Test{
public static void main(String[] args){
methodA();
methodB();
}
static void methodA(){
int x = 2;
int y = 4;
repeat: while(x!=y){
System.out.println(x + " is not equal to " + y);
x++;
continue repeat; // to show you how to have goto statement in Java; but it is not necessary in this case
}
if(x==y){
System.out.println(x + " is equal to " + y);
}
}
static void methodB(){
System.out.println("I'm methodB()");
}
}

Related Links

Improving the code that parses a Text File
run jaxb1 code on jdk1.6
Java chained inequality if (5<i<10)
Set jtable column to image button, but dynamically change image on specific row/column, not all rows in column
StackOverflow while generating random numbers without having two numbers fall in a certain range
How to handle iframe in WebDriver
Is it good practice to create an inner class for simple functionality?
Why can't a Struts 2 interceptor modify the 'result' page generated by subsequent interceptors?
How to tell when JUnit finishes by just using a TestWatcher?
How to make a Hibernate Mapping file in IntelliJ 11 Ultimate?
Solr query syntax such like SQL “IN” condition
Alternatives to not having Adobe AIR native extension for desktop Java
ClassCastException while Unmarshalling XML with JAXB
EJB QL Syntax error
Library to distribute software updates
java list of objects comparison

Categories

HOME
yii2
wso2
coq
hook
pypi
azure-data-factory
pycharm
objectgears
cplex
gis
filtering
maven-3
twitter-bootstrap-4
gps
angular-ui-bootstrap
networkx
dacpac
volttron
n-gram
lombok
progressive-web-apps
propel
normalizr
firefox-webextensions
swiftlint
mmap
sqlcipher
qhull
replaceall
jaxb2
subdomains
google-cloud-nl
srcset
bcd
ecto
bosh
twilio-api
oracle-fusion-middleware
contact-form
siesta-swift
sequential
host
http-digest
simplexml
space-complexity
hockeyapp
code-search-engine
ensembles
sfdc
menuitem
avconv
flickr-api
paxos
domain-model
slickedit
knockout-components
ifs
sqldf
titanium-android
yt-project
autorest
player
qpid
hover-over
yaws
suffix-tree
livequery
pervasive-sql
srand
jms2
simple-framework
event-bubbling
composite
angularjs-ng-click
cdt
phalanger
onactivityresult
xojo
access-rights
spring-io
cos
smtp-auth
anonymous-methods
mhtml
dmoz
trusted
coderush
chrono
windows-phone-7.1.1
enter
postgresql-performance
spring-portlet-mvc
libc++
doh
hamachi
xdomainrequest
javax.script
propagation
site.master
w3wp.exe
h.323
blitz++
dmx512
castle-monorail
ncqrs
thread-local-storage
genealogy
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App