Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Goto: How should I work the code out?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 tom_mai78101   Members   -  Reputation: 575

Like
0Likes
Like

Posted 04 October 2012 - 02:25 AM

Here's a Java pseudo-code I'm working on shown below:

[source lang="java"]public void someFunction(//Some parameters) { //some boolean flags here. switch (p.orientation) { case VERTICAL: if (booleanFlag1){ if (booleanFlag2){ //Some other codes. goto Label; } //Some codes } else if (booleanFlag3){ if (booleanFlag4){ //Some other codes. } //Some codes } reflectResponse(); break; default: return; }Label: //Some more codes... (required)}[/source]

I'm very tempted to use "goto" in Java, but as always, goto isn't allowed because of said issue. I will digress from that.

I only know from Java doc here that said "break" keyword works for for...loop, do...while..., and switch...case... only. Anything I can do?

Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1162

Like
1Likes
Like

Posted 04 October 2012 - 02:40 AM

Why can't you do this:
public void someFunction(//Some parameters) {
//some boolean flags here.
switch (p.orientation) {
  case VERTICAL:  
   if (booleanFlag1){
    if (booleanFlag2){
	 //Some other codes.
    }
    else if
    {
	 //Some codes
    }
   }
   else if (booleanFlag3){
    if (booleanFlag4){
	 //Some other codes.
    }
    //Some codes
   }
   reflectResponse(); 
   break;
  default:
   return;
}
//Some more codes... (required)
}
This does the same thing as your code, but doesn't use a goto statement. Unless there is something I'm missing?

#3 thok   Members   -  Reputation: 693

Like
1Likes
Like

Posted 04 October 2012 - 02:47 AM

Here's a Java pseudo-code I'm working on shown below:

[source lang="java"]public void someFunction(//Some parameters) {//some boolean flags here.switch (p.orientation) { case VERTICAL: if (booleanFlag1){ if (booleanFlag2){ //Some other codes. goto Label; } //Some codes } else if (booleanFlag3){ if (booleanFlag4){ //Some other codes. } //Some codes } reflectResponse(); break; default: return;}Label://Some more codes... (required)}[/source]

I'm very tempted to use "goto" in Java, but as always, goto isn't allowed because of said issue. I will digress from that.

I only know from Java doc here that said "break" keyword works for for...loop, do...while..., and switch...case... only. Anything I can do?

  • You cannot use goto in Java.
  • If you want to execute some code _only_ in the VERTICAL case when booleanFlag1 and booleanFlag2 are both true, don't do it this way. This is what you're trying to do, right? Instead, put this code in a separate function or method and call it inside the second `if` clause.


#4 Ashaman73   Crossbones+   -  Reputation: 7793

Like
5Likes
Like

Posted 04 October 2012 - 03:34 AM

In your pseudocode just exchange goto through break, that's it.

Longer answer: break will break from the current for/while/switch statement, regardless if it is embedded itself in an other non-loop/switch statement, like an if-statement.

Edited by Ashaman73, 04 October 2012 - 03:38 AM.


#5 tom_mai78101   Members   -  Reputation: 575

Like
0Likes
Like

Posted 04 October 2012 - 04:00 AM

Why can't you do this:
<snip>
This does the same thing as your code, but doesn't use a goto statement. Unless there is something I'm missing?


If both "booleanFlag1" and "booleanFlag2" are true, I need to execute code in "booleanFlag2" and then execute code in "booleanFlag1". So, the extra "else if" would not be the correct way.


  • You cannot use goto in Java.
  • If you want to execute some code _only_ in the VERTICAL case when booleanFlag1 and booleanFlag2 are both true, don't do it this way. This is what you're trying to do, right? Instead, put this code in a separate function or method and call it inside the second `if` clause.



1. I understand that. Hence I was asking if there are any alternatives.
2. It's not just VERTICAL case, there are other more cases. And no, it's not. I wanted to execute codes when booleanFlag1 and booleanFlag2 is true, but skip the code in booleanFlag1 and the rest of the code before the "Label". If booleanFlag2 is not true, continue to execute code in booleanFlag1, and call on reflectResponse() before continuing to execute more code.


In your pseudocode just exchange goto through break, that's it.

Longer answer: break will break from the current for/while/switch statement, regardless if it is embedded itself in an other non-loop/switch statement, like an if-statement.


You mean, "break <label>"? I googled it, and it turns out this is an actual Java code. Haven't seen this before. Thanks! :D

#6 Ashaman73   Crossbones+   -  Reputation: 7793

Like
0Likes
Like

Posted 04 October 2012 - 04:32 AM

You mean, "break "? I googled it, and it turns out this is an actual Java code. Haven't seen this before. Thanks! :D

Yes, you can label a loop (write it before the loop starts), but in your case you just need break; (without label).

#7 RulerOfNothing   Members   -  Reputation: 1162

Like
1Likes
Like

Posted 04 October 2012 - 05:01 AM

View PostRulerOfNothing, on 04 October 2012 - 06:40 PM, said:
Why can't you do this:

This does the same thing as your code, but doesn't use a goto statement. Unless there is something I'm missing?

If both "booleanFlag1" and "booleanFlag2" are true, I need to execute code in "booleanFlag2" and then execute code in "booleanFlag1". So, the extra "else if" would not be the correct way.

If you could use goto like in your first example, then the code in booleanFlag1 would be skipped.

#8 tom_mai78101   Members   -  Reputation: 575

Like
0Likes
Like

Posted 04 October 2012 - 09:30 AM

If you could use goto like in your first example, then the code in booleanFlag1 would be skipped.

Yes, that would be true. I'm sorry for being mistakened. :(




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS