Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can I use

for loop

then use if

then for loop(again)

then for loop(again)

else

end if

can I use like this in the code?

What I have tried:

can I use like this in the code (for inside if )
Posted
Updated 17-May-21 3:12am

Quote:
If and for loop using together

depends on details, but basically, yes you can.
Show your code.
 
Share this answer
 
Comments
Anoue,emoww11 17-May-21 3:45am    
what's the error in this
Function okay(data, lag, threshold, weights)
Dim X() As Integer
Dim Y() As Double
Dim Z() As Double
Dim n_data() As Double
Dim sum, avg, sum_squared, moddata As Double
nrows = data.Cells.Count
For element = 1 To nrows
n_data(element) = data.Cells(element)
Next element
For currentmoving = 50 To nrows
Y(currentmoving) = avg
For element = 1 To 50
sum_squared = sum_squared + (data.Cells(element + currentmoving - 50) - avg) ^ 2
Next element
Z(currentmoving) = Sqr(sum_squared / (50 - 1))
sum = 0
sum_squared = 0
Next currentmoving
For signal = 50 To nrows - 1
    'missing condition
If replace_this_line Then
X(signal, 1) = 1
n_data(signal) = 0.5 * data.Cells(signal) + (1 - 0.5) * n_data(signal - 1)
For element = 1 To 50
sum = sum + n_data(signal - 50 + element + 1)
Next element
Y(signal + 1) = sum / 50
For element = 1 To 50
sum_squared = sum_squared + (data.Cells(signal - 50 + element + 1) - Y(signal + 1)) ^ 2
Next element
Z(signal + 1) = Sqr(sum_squared / (50 - 1))
sum = 0
sum_squared = 0
Else
n_data(signal) = data.Cells(signal)
For element = 1 To 50
sum = sum + n_data(signal - 50 + element + 1)
Next element
Y(signal + 1) = sum / 50
For element = 1 To 50
sum_squared = sum_squared + (n_data(signal - 50 + element + 1) - Y(signal + 1)) ^ 2
Next element
Z(signal + 1) = Sqr(sum_squared / (50 - 1))
sum = 0
sum_squared = 0
End If
Next signal
okay = n_data()
End Function
 
Share this answer
 
Comments
Patrice T 17-May-21 3:58am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
And explain what is supposed to do this code and the problem you have with it.
Even after posting your code your question is still not clear, so I will explain some basic stuff and see if that helps you.

For/Next first:
Using For...Next statements (VBA) | Microsoft Docs[^]
Everything after the For ... will be repeated up to the matching NextYou can have anything in that repeated statement block that is a valid statement - and that includes other For/Next loops, If statements, virtually anything that is a valid statement - Writing Visual Basic statements (VBA) | Microsoft Docs[^]. When you have For-loops inside for-loops, it is known as "nested for loops" or "nesting"

It is not compulsory to include the counter variable name after Next , but it can help your code be more readable if you have a lot of For-loops, especially if they are nested.

What you cannot do is mix up the order of the next statements e.g. This is not legal code
VB
For Loop1 = 0 to 50
'Some code
For Loop2 = 0 to Loop1
'Some Code
Next Loop1
Next Loop2
The Inner Loop must be completed before the Outer Loop. Indenting your code helps make this more obvious too
VB
For Loop1 = 0 to 50
     'Some code
     For Loop2 = 0 to Loop1
          'Some code
     Next Loop2
Next Loop1
You can re-use the same counter variable as many times as you like, as long as you don't try to re-use it in the same set of nested loops e.g. This is legal
VB
For Loop1 = 0 to 50
     'Some Code
Next Loop1
'Some other code
For Loop1 = 17 to 34
     'Some Code
Next Loop1
But this is NOT
VB
For Loop1 = 0 to 50
     'Some Code
     For Loop1 = 17 to 34
          'Some Code
     Next Loop1
Next Loop1

If-statements:
Using If...Then...Else statements (VBA) | Microsoft Docs[^]
Just like the "boundaries" of For-loops are defined by the words For and Next with everything in-between being repeated that number of times, an If-statement is bounded by the words If and End If. It's a little more complicated because you can break If-statements up into sections using Else or ElseIf but the principals still apply - and indentation will help you read your code. You can have any sort of statement in the block between
- If and ElseIf or
- If and Else or
- Else and End If or
- If and Else or
- If and End If
If you want to use loops in those statement blocks, then you can. You can even use the same Loop counter - but the loop must be fully enclosed by the statement block. I.e. This is legal
VB
If myTest = myComparison Then
     For loop1 = 0 to 50 
          'Some Code
     Next loop1
Else
     For loop1 = 50 to 100
          'Some Code
     Next loop1
End If
But this is NOT
VB
If myTest = myComparison Then
     For loop1 = 0 to 50 
          'Some Code
Else
          'Some Code
     Next loop1
End If

A couple of other points - I copied your code to compile it to see if there were issues (Compile is on the Debug Menu in the VBA Editor) and it wouldn't. Not because there were any issues with your nesting of Ifs and Fors but because you hadn't declared some variables. I had to add
Dim nrows As Long, element As Long, currentmoving As Long, signal As Long, replace_this_line As Boolean
There are a lot of things you haven't initialised with values as well. Plus data, lag, threshold and weights should all really be given types instead of defaulting to Variant type.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900