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 DBPro Corner / Skipping a number in a For Next Loop

Author
Message
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 8th Jan 2004 00:36
Basically my program has a list of arrays that I check with a For Next loop, but I was wondering if it was possible to skip a certain number in the loop and go onto the next if a certain condition was true? Ideally, I want something like this to happen:



That will return a "Command out of place" because you're not allowed to have the Next i up there. I know it's possible to do it like this:



But that's not preferential because I have lots of nested If Statements and Loops as it is and I don't want it to look even more messy.

Anyone? Thanks.


"Computers are useless they can only give you answers."
andrew11
21
Years of Service
User Offline
Joined: 23rd Feb 2003
Location: United States
Posted: 8th Jan 2004 00:46 Edited at: 8th Jan 2004 00:49
You can do this:


or maybe:


Not sure if it would work. Try it.

"All programmers are playwrites and all computers are lousy actors" -Anon
walaber
21
Years of Service
User Offline
Joined: 22nd Oct 2003
Location: Los Angeles, CA
Posted: 8th Jan 2004 04:50
i wonder what doule happen if you put an "inc i,1" command inside a for loop?

for example,

for 1=1 to 10
if i=3 then inc i,1

text 10,10,str$(i)

next i

if I am thinking about this right, you would never see "3"....

I'm not around my DB right now, so I can't test it... anyone know if you can do that inside a for loop?

Go Go Gadget DBPRO!
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 8th Jan 2004 04:55
Andrew, not trying to bust b*lls or start a flame session but both suggestions are bad coding practices, sorry to say, so anyone else reading this please undersatnd that goto=bad, and altering for-next outter variable value=very bad.

Exeat, just reverse your logic so you don't have to skip any iterations of the loop - be creative(like we know you are) with the if-then stuff.

-RUST-

andrew11
21
Years of Service
User Offline
Joined: 23rd Feb 2003
Location: United States
Posted: 8th Jan 2004 05:18
@CattleRustler

Ill admit goto is not good to use too often, but in some places, its ok to use. It really dosent hurt your program. But whats wrong with changing the variable in a For Next loop??? What is the difference??? What does it do thats "very bad"???

"All programmers are playwrites and all computers are lousy actors" -Anon
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 8th Jan 2004 07:35 Edited at: 8th Jan 2004 07:37
Andrew11

Quote: "Ill admit goto is not good to use too often, but in some places, its ok to use. It really dosent hurt your program."

If DBP supported "On Error Goto" then I would agree with you on this, but unfortunately dbp does not, so I must say that there is NEVER a time you should use goto. Never. Not when you have GOSUB available. If you find yourself using goto instead of gosub then there is a problem in the code logic. I am sure most vet programmers here would agree, and mind you I said "Not when you have GOSUB available". I am just trying to state that GOSUB is much more reliable and safe as it implies RETURN. Goto is open-ended and dangerous and will get you spaghetti-code(sp?) in the long run - for sure!

Quote: "But whats wrong with changing the variable in a For Next loop??? What is the difference??? What does it do thats "very bad"???"

Well, for me, if I define a FOR-NEXT loop to be some range of numbers then it was for a reason. Altering the outter loop count, to me (IMHO) seems silly and unecessary when you have all sorts of condition tests at your disposal. Maybe I am used to collections and the inherent FOR EACH-NEXT way of doing things in vb6/.net but it just seems if you need to change the variable that the whole loop is based on then something is very wrong - sorry i can't explain it better than that _ not trying to be a wise ass here, really, just trying to trickle down some observations based on my coding experience. Regardless of the language, some base principals hold true.

In the end, do what works best for you!
JAM

-RUST-

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 8th Jan 2004 11:06
This is one scenario where I would agree that GOTO is acceptable, so long as it is carried out as the first command in the loop, and it takes you to the very end of the loop. I work on commercial software with strict coding standards, and this is accepted.

Personally, I would do it with a DO...LOOP and an EXIT command to get out. This is also a commercially acceptable standard.

BatVink (formerly StevieVee)
http://facepaint.me.uk/catalog/default.php
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 8th Jan 2004 15:10 Edited at: 8th Jan 2004 15:15
This will do the trick without any philosophical debate.



However, the following code was a complete shock and did not do what I expected. (So much so that I'm tempted to call it a bug.)

Try to guess what it will print before running it...



The addition of some parenthesis fixes it.



--
TAZ

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th Jan 2004 16:48
NOT looks for a boolean expression, and using it like in the first code bit, I don't think it sees the whole expression as simply boolean. The parenthesis narrow the argument, or so that's what I think it does. Alternatively, you could just do IF a <> 1. Heh, I didn't even know DB had a NOT command.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 8th Jan 2004 18:43
heh heh, I found NOT in dbp by accident, I was typing...

IF NOT OBJECT EXIST(..)....

and it worked, lol

-RUST-

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 8th Jan 2004 19:02
if not a = 1

is the same as

if (not a) = 1

So here is what you get

NOT 0 = 1
NOT 1 = 0
NOT 2 = 1
Anything greater = 0
Anything less than 0 is itself (NOT -1 = -1)

BatVink (formerly StevieVee)
http://facepaint.me.uk/catalog/default.php
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 10th Jan 2004 15:37
Quote: "Andrew, not trying to bust b*lls or start a flame session but both suggestions are bad coding practices, sorry to say, so anyone else reading this please undersatnd that goto=bad, and altering for-next outter variable value=very bad."


Agreed. Which is the reason I never even thought to use one of them. When I learned VB I was basically told the exact same thing, "Goto is your enemy." lol.

I guess I may as well just go with a very large nested loop, or perhaps like Batvink suggested and use a Do Loop and then just exit when I get to the maximum number.

Thanks for the comments everyone.


"Computers are useless they can only give you answers."
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 10th Jan 2004 17:50
no prob Exeat, good luck

-RUST-

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Jan 2004 19:08
If you have a large number of numbers that you want to skip, you can use an array



No goto's, and only one 'if' in the loop

For free Plug-ins, source and the DBPro Interface library for Visual C++ 6 and .NET
http://www.matrix1.demon.co.uk
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 11th Jan 2004 01:35
Lol did you just copy the code snippet in my first post Ian? That's what I wanted to avoid because I have many nested Loops and If Statements, my source code is almost tabbed across half the page. hehe.


"Computers are useless they can only give you answers."
Xander
21
Years of Service
User Offline
Joined: 3rd Mar 2003
Location: In college...yeah!
Posted: 11th Jan 2004 02:16
Yeah, tabbing across half the page is bad. Then your lines get over 256 characters, and DB doesn't allow that...very annoying...

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Jan 2004 14:34
Doh! Sorry mate

How about this



I don't think that I'm repeating anyone this time

For free Plug-ins, source and the DBPro Interface library for Visual C++ 6 and .NET
http://www.matrix1.demon.co.uk

Login to post a reply

Server time is: 2024-11-25 23:40:58
Your offset time is: 2024-11-25 23:40:58