msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

For If Next problem

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
chris.sommerville



Joined: 04 Oct 2007
Posts: 1

PostPosted: Fri Nov 10, 2006 7:00 pm    Post subject: For If Next problem Reply with quote

Hi all,

I have an If in a for loop, and what I want it to do is if a variable =
0 then next else continue with the code. Rough example:

For i = 0 to 20

If x = 0 then
EXIT FOR ------this si the line I'm not sure of
else
End If
[code block]

Next

Regards

Stopher

Archived from group: microsoft>public>vb>syntax
Back to top
View user's profile Send private message
Ken Halter



Joined: 04 Oct 2007
Posts: 4150

PostPosted: Fri Nov 10, 2006 7:29 pm    Post subject: Re: For If Next problem Reply with quote

wrote in message @h54g2000cwb.googlegroups.com...
> Hi all,
>
> I have an If in a for loop, and what I want it to do is if a variable =
> 0 then next else continue with the code. Rough example:
>
> For i = 0 to 20
>
> If x = 0 then
> EXIT FOR ------this si the line I'm not sure of
> else
> End If
> [code block]
>
> Next
>
> Regards
>
> Stopher

Exit For will jump out of the entire loop.

What you want is more like....

'=========
'Make sure this is at the top of each and every module in your app.
'I couldn't tell if you were actually trying to test a variable called 'X'
or
'you meant to test the 'i' variable.
Option Explicit

Private Sub Command1_Click()
Dim i As Integer
For i = 0 To 20
If i > 0 Then
'[code block]
Else
'handle the i = 0 case here
End If
Next
End Sub
'=========


--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm
Back to top
View user's profile Send private message
Dmitriy Antonov



Joined: 04 Oct 2007
Posts: 431

PostPosted: Fri Nov 10, 2006 10:36 pm    Post subject: Re: For If Next problem Reply with quote

wrote in message @h54g2000cwb.googlegroups.com...
> Hi all,
>
> I have an If in a for loop, and what I want it to do is if a variable =
> 0 then next else continue with the code. Rough example:
>
> For i = 0 to 20
>
> If x = 0 then
> EXIT FOR ------this si the line I'm not sure of
> else
> End If
> [code block]
>
> Next
>
> Regards
>
> Stopher
>

If you are not sure then just try it in debug mode and see how program
behaves. BTW, if there is nothing in the ELSE clause then you can remove
this keyword - for clarity.
You requirements are not clear - what does it mean "...then next else
continue...". Next what and Continue with which code. As presented, the code
will exit from the loop completely if x=0. If you want to go to the next
iteration instead then you can do it this way:

for i=0 to 20
if x0 then
'you code goes here
end if
next i

Another way is by using GoTo and a label, but it is, generally, not
recommended.

Dmitriy.
Back to top
View user's profile Send private message
Bob Butler



Joined: 04 Oct 2007
Posts: 1325

PostPosted: Fri Nov 10, 2006 7:43 pm    Post subject: Re: For If Next problem Reply with quote

wrote in message@h54g2000cwb.googlegroups.com
> Hi all,
>
> I have an If in a for loop, and what I want it to do is if a variable
> = 0 then next else continue with the code. Rough example:
>
> For i = 0 to 20
>
> If x = 0 then
> EXIT FOR ------this si the line I'm not sure of
> else
> End If
> [code block]
>
> Next

You've gotten examples for the general case; for the specific case make it
for i=1 to 20


--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Back to top
View user's profile Send private message
Argusy



Joined: 04 Oct 2007
Posts: 105

PostPosted: Sat Nov 11, 2006 2:54 pm    Post subject: Re: For If Next problem Reply with quote

>>Hi all,
>>
>>I have an If in a for loop, and what I want it to do is if a variable =
>>0 then next else continue with the code. Rough example:
>>
>>For i = 0 to 20
>>
>>If x = 0 then
>> EXIT FOR ------this si the line I'm not sure of
>>else
>>End If
>>[code block]
>>
>>Next
>>
>>Regards
>>
>>Stopher
>>
>
>
> If you are not sure then just try it in debug mode and see how program
> behaves. BTW, if there is nothing in the ELSE clause then you can remove
> this keyword - for clarity.
> You requirements are not clear - what does it mean "...then next else
> continue...". Next what and Continue with which code. As presented, the code
> will exit from the loop completely if x=0. If you want to go to the next
> iteration instead then you can do it this way:
>
> for i=0 to 20
> if x0 then
> 'you code goes here
> end if
> next i
>
> Another way is by using GoTo and a label, but it is, generally, not
> recommended.
>
> Dmitriy.
>
>

and expanding Dmitry's code, if you want to exit the loop if X = 0

x = 1 'presetting x, unless you KNOW x > 0 to start with
for i = 0 to 20
if x 0 then
'your code goes here
else
exit for
end if
next i

------UNLESS-----

x is contained entirely inside the loop

then your loop can be

for i = 0 to 20

block of code generating x (using i?)

if x = 0 then exit for 'NOTE - no following "end if"

next i

There are even more ways to get what you want, but I think you should
now have enough info from the group for you to think up your own approach.

Loops can be such iffy things, can't they (:>

Argusy
Back to top
View user's profile Send private message
Argusy



Joined: 04 Oct 2007
Posts: 105

PostPosted: Sat Nov 11, 2006 3:04 pm    Post subject: Re: For If Next problem Reply with quote

should that be

Ifs can be such loopy things

Argusy
>
> There are even more ways to get what you want, but I think you should
> now have enough info from the group for you to think up your own approach.
>
> Loops can be such iffy things, can't they (:>
>
> Argusy
>
Back to top
View user's profile Send private message
Larry Serflaten



Joined: 04 Oct 2007
Posts: 2644

PostPosted: Sat Nov 11, 2006 12:31 am    Post subject: Re: For If Next problem Reply with quote

wrote

> I have an If in a for loop, and what I want it to do is if a variable =
> 0 then next else continue with the code. Rough example:

When thinking about your logic, you might also look at the
negative case for possible reductions in code. If you want to
skip code when X is 0 then another way to look at it is that
you want to execute code when X is not 0.

For X = -10 to 10
If X 0 Then
[code block]
End If
Next

LFS

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
xml problem I have a problem, that happens after using the - when using there is a strange behaviour. For the following code : .... Option Explicit ..... dim mainTestName as s

ParamArray Problem Hello, I have trouble in VB6. I have a function of a extern component which takes a variant paramarray as parameter (ie pArrayVar() as Variant)). I can't modify this function. I would like to use this function but to put it an array : D

Pdf opening problem Hello I'm trying to open a PDF file inside a form but it doesn't seem to work properly(the application stops I have registered the ActiveX which came with Adobe Acrobat Reader 7 and added a control to my form. Here is the code i used ... --

Array problem Hi, suppose I have Array in Visual Basic dim k(100) as integer dim d(5) as integer how can I assign in one line 5 elements from "k" to "d " directly? e.g: d(1:5) = k(55:60) THX

Problem with locale Hi, I have the following problem: I read a numeric (double) value stored in simple text file (i.e "2.25") and convert it to a double using CDbl. Works fine until used on a client with non-english locale. For example, if German locale is set, which uses (,
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group