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

Can we use parameters in oozie decision node?
Calculating Document Frequency in HashMap java
DynamoDB Global Secondary Index with Exclusive Start Key
Maven site-plugin remove or change Parent Project Link
Simple averaging error, cannot find the right code
HK2 annotations not being handled
IntelliJ IDEA warning highlighting on not using an abstract method?
Java buttons repaint
How do I add data into my SQLite Database? [closed]
How to detect if user clicked on an image on a map in Android?
libGDX cone rotation while maintaing location in X,Y,Z
Timer to repeat SOMETHING until SOMETHING_ELSE
Generics cannot cast Object to interface class java
Converting seconds into milliseconds - as a method parameter?
Infinite Loop in Java 2d array program
Java click button to open URI: “Firefox can't find the server at www.%u.com.”

Categories

HOME
java
xamarin
wso2-am
clips
listview
blogger
debugging
zeromq
tinymce
homebrew
office365api
platform-builder
fme
rsync
jsrender
fingerprint
append
amazon-cloudformation
windows-10-universal
gnupg
adobe-analytics
size
try-catch
highlight.js
ef-migrations
google-static-maps
firefox-webextensions
physics-engine
errorlevel
functional-testing
facebook-instant-articles
sylius
chromium-embedded
kudan
icloud-api
centos6.5
strncpy
ping
dbclient
jaxb2
madlib
instant-messaging
google-sites-2016
catel
ghost4j
stacked
vxworks
hot-module-replacement
occlusion
sharefile
jna
splice
squib
optix
sql-server-agent
quadratic-programming
reactive-cocoa-5
eclipse-gef
avconv
kbuild
pearson
linode
mcafee
jquery-filter
firmata
deadbolt-2
httplistener
home-directory
underscore.js-templating
trash
merge-conflict-resolution
storekit
prettytensor
sigabrt
oauth2client
maven-tomcat-plugin
jms2
google-places
django-unittest
muse
sankey-diagram
processmodel
fluid-dynamics
unity3d-gui
sonarqube5.1.2
dereference
skos
kineticjs
android-nested-fragment
codeigniter-routing
preferences
heisenbug
typeof
reactfx
c18
farseer
aqtime
soundtouch
dbconnection
easy-install
gdata-api
venn-diagram
jspinclude
bubble-chart
reddot
jquery-ui-layout
pyinotify
xfbml
html-input
suppress
compiler-specific
eqatec
ntvdm.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