Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Quick help on nested if inside for loop.. how to exit for loop

Author
Message
Golelorn
3
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 18th Oct 2017 01:54 Edited at: 18th Oct 2017 01:57
Hey guys,

I am trying to debug a code of mine. I need to escape a deeply nested if statement and go to the next iteration. I think exit is just breaking me out of one if statement, instead of bringing me to the next iteration.



I am pretty sure the exit is taking me out of the if statement and going to the next statements(doing things), but I want to completely go to the next iteration of the for loop.

Can someone show me how I can achieve this? Or am I looking at this wrong?
blink0k
AGK Developer
7
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 18th Oct 2017 02:06 Edited at: 18th Oct 2017 02:06
Supertino
2
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 18th Oct 2017 08:29 Edited at: 18th Oct 2017 08:30
You could cheat and use goto's... #runs and hides#

BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 18th Oct 2017 08:47
Quote: "You could cheat and use goto's... #runs and hides#"


#we will find you#
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
puzzler2018
User Banned
Posted: 18th Oct 2017 10:11 Edited at: 18th Oct 2017 10:12
Never use Goto's



Really messy

D
Golelorn
3
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 18th Oct 2017 13:06
Quote: "Maybe try the continue command"


That worked. Thank you!!
Conjured Entertainment
AGK Developer
14
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 18th Oct 2017 16:28 Edited at: 18th Oct 2017 22:47
Quote: "You could cheat and use goto's..."

I still use GOTO now and then.


Quote: "Never use Goto's"

Quote: "#we will find you#"




EDIT

OOOPS
I misread your post at first.

I thought you said nested LOOPS not nested IFs

Sorry if my post didn't make any sense.

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Dybing
8
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 18th Oct 2017 21:00 Edited at: 18th Oct 2017 21:04
The correct answer to your problem is not only simple, but will also save you from similar situations: Whenever your nesting go three or more levels deep, refactor!

If you are not familiar with the term, it means re-organizing your code. In this situation, extracting out pieces of it, and tuck them away into simpler to manage little functions. Which have the added property of being named by you into something that says something useful about what is being done in that particular piece of code. Well named variables and functions are much better than comments for making your code easy to read and follow.

One thing you should practice as well in this regard, is to not mix levels of abstraction. Keep the nuts'n'bolts of output and input in their own functions - ideally inside their own files - and game-logic entirely separate from these. Makes everything so much more malleable and easy to expand upon.

Though mind you, doing it this way, you will need very strict data-control. Mutable variables and structures need be passed explicitly to functions. If you are a habitual mutable global abuser, it'll just be a huge mess of hopelessly implicitly linked code that will have you mystified and quite possibly result in a severe drinking problem from trying to dull the debugging pain!

Trust me, I'm a programmer, who don't drink. Much. Anymore
Golelorn
3
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 19th Oct 2017 19:06
Dybing,

Good tips. I do need to name my functions and variables better.
blink0k
AGK Developer
7
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 19th Oct 2017 21:45
Hey Dybing. Do you have any example code? Maybe a small game?
PartTimeCoder
AGK Tool Maker
4
Years of Service
User Offline
Joined: 9th Mar 2015
Location: Suffolk UK
Posted: 19th Oct 2017 23:22
Quote: "The correct answer to your problem is not only simple, but will also save you from similar situations:"


Completely agree with everything Dybing said.

Quote: "
Do you have any example code?"


I have had a few discussions on this subject and go into a little detail in this semi related thread https://forum.thegamecreators.com/thread/220616

Structuring code is pretty much an art form, using well thought out data structures in place of globals and state machines or select blocks in place of nested if statements can turn your canvas from a child's scribble into Rembrandt or da Vinci, I'm no Picasso but I have learned enough to know that rushed untidy code is not only hard to read its damn near impossible to maintain and bugtrack.

In the OP's example code, a slight reshuffle will solve:
Quote: "//I do not want to run this batch of code if count is < 20 and the objects are the same name"


you have already checked if count<20 on line 11 so inside this block you only need to check the name inside another if statement

like Dybing said, refactor

not knowing what the rest of the code is doing and the content of the support functions its hard for me to reshuffle the code but a small advice, a condition should only be checked once per iteration and any code that depends on that condition should be grouped within that block, any further conditionals that depend on the state of the original should also be within that block ... if that makes any sense



Supertino
2
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 20th Oct 2017 08:26 Edited at: 20th Oct 2017 10:08
PartTimeCoder
AGK Tool Maker
4
Years of Service
User Offline
Joined: 9th Mar 2015
Location: Suffolk UK
Posted: 20th Oct 2017 09:39
Again...... GoTo's were never great!

#BanSpaghettiCode
Conjured Entertainment
AGK Developer
14
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 20th Oct 2017 15:11 Edited at: 20th Oct 2017 16:01
Quote: "Again...... GoTo's were never great!"

I guess that depends on when you started coding.

In my college course in BASIC (I was already self taught in BASIC for many years prior to going to the university), the instructors examples used GOTOs.

GOTO was commonly used and not thought of as a bad thing, because it was used properly. (my instructor used it incorrectly in one example, but I caught it)

Just like OOP was around in the 1970's, but wasn't commonly used until the 1990's because in the 70's it was considered too complex.

Of course, the hardware was much different then, and as it advanced the thoughts about good and bad programming techniques changed over time.

It is only a matter of personal opinion as to whether GOTO is good or bad, just like anything else.

I never had a problem using GOTO, so I failed to see why so many people are against it.

There are many ways to create endless loops without using GOTO, so that argument is weak, even though it seems to be about the only reason GOTO gets negative feedback.

Jumping to one spot of a program using GOTO can be just as effective as a Function call or using GOSUB, as long as you GOTO another spot when you need too.

Back then your BASIC programs had line numbers that were mandatory when writing the code, so that helped GOTO have its place because jumping to a line number using GOTO made a lot of sense.

We used to write our code in increments of 10 with our line numbers (10,20,30,etc) that way we could add lines in between later without having to renumber the entire program.

It was a different world back then, in the programming world too, so at one time GOTO was useful, but as far as being great, that goes back to personal opinion so there is no right or wrong to it.


Quote: "#BanSpaghettiCode "

lol
Spaghetti code has its place too.

Some programmers used to intentionally write their code in ways that made it difficult for others to understand.

It was all about job security, if you wanted to be the one hired to modify your own code rather than have someone else cut your throat (under price you) and do it instead.

Many times is was cheaper for them to have the original programmer untangle their mess when it needed to be modified, rather than have that cheaper guy spend 3 times as long trying to figure it out or rewrite it from scratch.

For business owners, yes ban it, but for programmers it can prevent others from modifying your stuff with ease. (we didn't use comments either)

The only problem is that even the original programmer can get confused if they have been away from the code for too long, so you have to have a method to your madness!

BASIC gives you the freedom to write your code almost any way you want to, that is why we see so many different examples on how to code the same objective.

If desire$ ="structured programming" then use$= "COBOL"

lol

I think a lot of people get misled about this topic from the "non-believers" too.

For instance, IF you GOTO (lol) the article on Spaghetti Code at Wikipedia, THEN you will find the example pieces of BASIC code...



However, the first bit of GOTO code could have been done differently, which would have resulted in the same number of lines as the so called correct way...



In my opinion, it all comes down to results, and if the code does what it is supposed to do, then it doesn't really matter which way you chose to do it.

After all, it is your code, so doing it your way is okay.

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Dybing
8
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 20th Oct 2017 18:39 Edited at: 20th Oct 2017 19:17
Quote: "Hey Dybing. Do you have any example code? Maybe a small game?"


Why certainly. From my current project. But it is quite involved, so I'll just show some snippets.

Here is the main menu loop, exiting which also exits the app:



The menuActions() routine is the side-menu buttons. Highlight on hover over, and latches to green on click - and change the main-window to another screen. The code look like this:



Everything here that actually do anything, like read input and change the color of buttons, is handled by functions that exist in a separate files named input.agc and output.agc. For instance my mouse-functions:



And so on down the rabbit-hole it goes. In this particular project, about half of the code so far is by and large unmodified copy-paste from previous projects I've done. All I change is the higher-level logic, whilst the nuts'n'bolts stay more or less the same. For instance, the mouse functions are untouched from my previous project (still a WiP, the two will be linked, what this current project is for, is to act as the local server and serve stats and stuff to a big-screen), where I've just added the getMouseHover() function, because this app will run on PC and use a mouse, not a handheld device using greasy fingers.

What it looks like (with mouse hovering over the first big plus sign):



Again, very much WiP, knocking it out as fast as I can at work, and will be ready for presentation in I'm guesstimating a couple of weeks. Most of the project is available here: https://github.com/rDybing/msSignServe

That repo is gimped though. For one thing, the media folder is not in it, and neither is the mock.agc file that allows me to do quick tests without being tethered to the AWS server and wait for responses from there.

Testing is muy importante!

Quote: "(in relation to goto's) there is no right or wrong to it."


Yes there is. It is wrong. Plain and simple. I was a goto sinner as well in my mis-spent childhood. Both ZX Spectrum Basic, and Z80 and MC68000 assembly had gotos just about everywhere. But then I discovered procedural programming in AMOS and expanding that with modularity in Modula-2 and C, and Objects in C++ and C#. OK, we don't have objects in AGK. Or modularity in the sense that a file can set a namespace. But still. Whilst OO is over-rated, and we don't really need it, modularity isn't - it is vital - so use it even if you have to fake it
PartTimeCoder
AGK Tool Maker
4
Years of Service
User Offline
Joined: 9th Mar 2015
Location: Suffolk UK
Posted: 20th Oct 2017 19:06
Quote: "There are many ways to create endless loops without using GOTO, so that argument is weak"


That is very true and the reason why I advise in the thread I linked to that logic and functionality should be kept separate, calling a function from a loop that calls another and that function calls another and so on 10 times amounts to the same as a bunch of GoTo's and a big tangled mess.

I slam the GoTo's because in my early days I created some tangled code that Bill Gates himself would have a brain ache trying to understand, I learned the hard way how not to write code but I still use them on occasion but only in a complex function to jump to a certain condition but I never GoTo out of the function so its more of a spaghetti hoop than a bolognaise, lol

You do raise some very good points though I can see the reasons behind writing cryptic code, its just not my thing.

Quote: "Why certainly."


That's tidy coding.
Dybing
8
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 20th Oct 2017 22:50
Quote: "That's tidy coding."
Why thanks - I do my best. And for a few reasons that is more a comment on Conjured Entertainment's arguments:

Obfuscation: Why? It do not ensure 'job security' at all. Quite the contrary. If I sniff any sort of willful obfuscation in any of our company's codebase, the guilty one would be out the door before he or she could spell 'Sabotage'. And I review code religiously.

If trying to land a programmer job, you will need examples of finished projects and source code. If the interviewer can read your code pretty well with very few WTFs a minute (the global standard of code quality) you'll look all the much better for it. Programming in this day and age is very much a team effort - and even where it isn't, where you are flying solo - whoever hires you will want to ensure that the project can go on if something should happen to you. Like say getting run over by a bus - or you suddenly inherited a fortune and decided it is time to retire.

There is also the consideration that code is read a whole lot more than it is written. If expanding upon a code base, or trying to track down an elusive bug, clean code will make it all that much easier. In the end, you want a code-base that is more about composition at a high level of abstraction, where all the low-level stuff is tested working flawlessly. Preferably with their own unit-tests, but who got time (read: money to spend) to do that for full coverage?!

Even with a clean(-ish) code-base, should some snotty-nosed competition try and underbid to weasel themselves into a contract to fix or maintain, there is still a significant cost to new developers spending time to get to grips with the existing code-base. In the end, it'll cost the owner of the code-base more to bring on a new crew no matter what. Unless the owner think the old code is so messy, it'll be worth it to clean it up - from scratch if need be. And if so, the original crew or author won't enter the consideration.

You're only as good as your last job. The project may have failed for a variety of reasons, but if your code is just fine according to the standards of your peers looking to hire you to another project or job, you're golden.

Failing to write good code by inexperience or intention, stringing it together with the programming equivalents of duct-tape and rubber-bands, all you'll ensure is not getting re-hired or get referrals. The product will be sub-par and bugs will flourish with poorly structured code that can not be expanded upon. And in either case, intentionally obfuscating code is a sure sign of a programmer that is insecure in his or hers own competence at any rate. Good programmers never have trouble getting work.

My 2c
blink0k
AGK Developer
7
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 20th Oct 2017 23:02
Thanks dybing. Very interesting
Conjured Entertainment
AGK Developer
14
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 21st Oct 2017 02:10 Edited at: 21st Oct 2017 02:30
Quote: " And for a few reasons that is more a comment on Conjured Entertainment's arguments:"


I think you may need to re-read my post, because it was not an argument.

My reference to "job security" was simply saying why some people used to program confusing code.

I think you may have missed the used to and was parts of what I said, because I was speaking in past tense.

I was not suggesting that future programmers should do this, but rather why some of the past programmers that I knew did it.

I was not trying to make an argument, but rather sharing a little of my past to give some insight on the mentality of people who did it.

As I said, it was a different world back then, and a lot of indies had very little protection of their code, and a lot of it got ripped off.

Anyway...

As far as your suggestions for new programmers trying to land a job nowadays, I do not disagree with your advice nor question your experience.

However, I don't think programming in BASIC (the spaghetti code using GOTO which is what I was talking about) is going to matter much in that venture.

Not many casual games here are going to be team efforts either, so worrying about getting run over by buses or hitting the lottery will not matter much either.

Yeah, if you want to land a six figure income programming C++ with a team for a big corporation then listen to the experts and be the perfect cog, but if you want to code in BASIC as an indie or hobbyist then do it your own way and have fun doing it!

Sure, the company's program should be written the way they see fit, but the independent programmer has artistic freedom to do/create whatever they imagine in a way that suits them.

Different strokes for different folks, and my only argument is that there is no wrong way to code if it works, only opinions of what is the better or right way to do it.

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
PartTimeCoder
AGK Tool Maker
4
Years of Service
User Offline
Joined: 9th Mar 2015
Location: Suffolk UK
Posted: 22nd Oct 2017 15:02
Quote: "and my only argument is that there is no wrong way to code if it works"


I respectfully disagree, obfuscation is one thing but clunky unoptimized code is another and in my opinion its not wrong if it works but its also not right, it just works, of course I understand it takes many years of writing code that just works to learn how to make it, dare I say, right.

But..... "I was not trying to make an argument" and "only opinions of what is the better or right way to do it" agree 100%, differing opinions are what make us unique and its good to share them in open debate, I came into this thread giving advice and ended up learning something myself.

Login to post a reply

Server time is: 2020-02-26 07:56:39
Your offset time is: 2020-02-26 07:56:39