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

Quality and Best Programming Structrues in android java [closed]
Sorting JPanels based on component
Disabling click sound Android Studio
Spring & Hibernate without jpa
¿How make the correct animation? (Android java without xml)
Show layout boundary without setting
How to take time out of user input date?
How to fetch data from the result of an entity class in hibernate
Writing a for-each loop to get objects from an ArrayList [closed]
Custom Relative Layout in XML Issue
Play [2.4.2] - Add external jar to project
Java: Using gridbaglayout with parent class and subclass
Change textview through a button in a widget
Connect to online mysql database using java [duplicate]
Android: Google Maps myLocation button - perform zoom in
What is a good alternative to using an empty abstract class?

Categories

HOME
ibm-bluemix
pandas
pdf
bluetooth
gitlab
coq
isabelle
iterator
onedrive
platform-builder
jsp-tags
yarn
gps
opengl-es-2.0
webrequest
contact
spring-kafka
postgres-xl
collectd
netflix
iolanguage
pugjs
evopdf
oxyplot
zapier
arabic
wkwebview
bootstrap-tour
emgucv
h2db
nouislider
iframe-resizer
titanium-mobile
noraui
calibre
windows-dev-center
textmate
mozilla
cloud-code
ios5
http-digest
powershell-dsc
angular-resource
jquery-nestable
logparser
jlink
return-value
tizen-native-app
brightcove
setter
mcafee
rotativa
phishing
mathematica-frontend
mongocsharpdriver
websitepanel
packagist
pagerank
goose
unity-networking
vhd
maven-tomcat-plugin
t4mvc
map-projections
apache-commons-fileupload
iiviewdeckcontroller
truevault
bigbluebutton
c3
jscript.net
android-imagebutton
socketexception
flask-cors
typeof
datagridviewcolumn
stxxl
ruby-datamapper
simba
dmoz
unc
multipage
code-cleanup
subgurim-maps
xmemcached
horizontal-accordion
disclosure
iphone-web-app
jmock
datareader
site.master
sproutcore-2
revisions
xsdobjectgen
data-driven
swing-app-framework
anti-piracy
document-conversion

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