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

How to implement a chat client
Java-To insert a substring into a main string after a given substring of main String [closed]
How to get a looping program in Java
How HashSet internally adds element on it?
Make activity not destroyable or pausable until a pattern doesn't match
How can I create an OAuth2 secured REST API using Spring?
Dynamically Generate Jackson JSON properties
Change brightness of whole scene
Dynamic edges in Gephi (Java)
Oracle with java program
Java - Decimal Format Precision lost when returning a Float or Decimal
Java split string by whitespace and punctuation but include only punctuation in result
Domain Driven Design in Java - Turning all primitives into Value Objects? [duplicate]
Java program for calculating interest [duplicate]
Getting error while creating enum java class
Service response mocking framework java

Categories

HOME
maven
ibm-bluemix
compiler-construction
arduino-uno
log4j
fluentd
path-finding
relay
mean-stack
bookshelf.js
rdf
mvvmcross
onelogin
facebook-messenger-bot
google-cloud-ml
remote-access
windows-server-2012
ups
lombok
worldwind
systemc
fatal-error
apache-metamodel
excel-vba-mac
ml
scriptcs
csrf-protection
facebook-instant-articles
blazemeter
emgucv
opennlp
brunch
info.plist
pingfederate
kendo-datasource
primitive
unboundid
instant-messaging
nameservers
javascriptcore
.net-4.0
catel
pim
commit
asset-pipeline
subset-sum
bower-install
android-ble
main
x11-forwarding
espeak
serverside-rendering
jna
android-fingerprint-api
document.write
graphenedb
unoconv
android-tabhost
fedex
gzipstream
grid.mvc
dynamic-reports
vao
slick-3.0
convertapi
fault
osx-mavericks
python-c-api
aurelia-validation
setter
jxcore
worker-thread
python-cryptography
pillow
flow-control
gcsfuse
synchronous
eventkit
pintos
ableton-live
httplistener
gridview-sorting
lua-5.1
python-stackless
whois
pickadate
vhd
iis-arr
ibaction
appfabric-cache
operation
composite
clipperlib
lustre
facebook-graph-api-v2.4
cdt
phalanger
javafx-webengine
nsbutton
zend-route
xojo
mesa
rtmfp
didselectrowatindexpath
oracle-warehouse-builder
industrial
uv-mapping
listings
dataservice
trusted
buildr
libc++
vdsp
coredump
jmock
html-input
icanhaz.js
vc90
ti-dsp
ctp4
premature-optimization
pascal-fc
genealogy
post-build
msdev

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