Asked by:
VBScript: Is there a better way to write this conditional statement ...
General discussion

Hi,
Is there a better way to write this conditional statement? It works but it is so long.
Thanks in advance!
'Loop Script
For size = 64 to 8976
print "Packet length is " & size
For i = 1 to 1
If size > 64 AND size = 256 Then
size = 511
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size > 512 AND size = 528 Then
size = 767
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size = 784 Then
size = 1023
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size > 1024 AND size = 1040 Then
size = 1279
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size = 1296 Then
size = 1535
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size > 1536 AND size = 1552 Then
size = 1791
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size = 1808 Then
size = 2047
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size > 2048 AND size = 2064 Then
size = 2303
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size = 2320 Then
size = 2559
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size > 2560 AND size = 2576 Then
size = 2815
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size = 2832 Then
size = 3071
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size > 3072 AND size = 3088 Then
size = 3327
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size = 3344 Then
size = 3583
print vbcrlf
print size
print vbcrlf
Exit For
ElseIf size > 3584 AND size = 3600 Then
size = 3839
print vbcrlf
Exit For
ElseIf size = 3856 Then
size = 4095
print vbcrlf
Exit For
ElseIf size > 4096 AND size = 4112 Then
size = 4351
print vbcrlf
Exit For
ElseIf size = 4368 Then
size = 4607
print vbcrlf
Exit For
ElseIf size > 4608 AND size = 4624 Then
size = 4863
print vbcrlf
Exit For
ElseIf size = 4880 Then
size = 5119
print vbcrlf
Exit For
ElseIf size > 5120 AND size = 5136 Then
size = 5375
print vbcrlf
Exit For
ElseIf size = 5392 Then
size = 5631
print vbcrlf
Exit For
ElseIf size > 5632 AND size = 5648 Then
size = 5887
print vbcrlf
Exit For
ElseIf size = 5904 Then
size = 6143
print vbcrlf
Exit For
ElseIf size > 6144 AND size = 6160 Then
size = 6399
print vbcrlf
Exit For
ElseIf size = 6416 Then
size = 6655
print vbcrlf
Exit For
ElseIf size > 6656 AND size = 6672 Then
size = 6911
print vbcrlf
Exit For
ElseIf size = 6928 Then
size = 7167
print vbcrlf
Exit For
ElseIf size > 7168 AND size = 7184 Then
size = 7423
print vbcrlf
Exit For
ElseIf size = 7440 Then
size = 7679
print vbcrlf
Exit For
ElseIf size > 7680 AND size = 7696 Then
size = 7935
print vbcrlf
Exit For
ElseIf size = 7952 Then
size = 8191
print vbcrlf
Exit For
ElseIf size > 8192 AND size = 8208 Then
size = 8447
print vbcrlf
Exit For
ElseIf size = 8464 Then
size = 8703
print vbcrlf
Exit For
ElseIf size > 8704 AND size = 8720 Then
size = 8959
print vbcrlf
Exit For
ElseIf size = 8976 Then
print "last size is " & size
Exit For
End If
Next
Next
Adohr
 Changed type Bill_Stewart Friday, January 26, 2018 3:03 PM
 Moved by Bill_Stewart Friday, January 26, 2018 3:04 PM This is not "do my homework for me" forumn
All replies




Also the logic is just plain wrong. Things like this "ElseIf size > 6144 AND size = 6160 Then" just don't make any sense. Perhaps some study of logic would help.
The whole script is just pointless. "For I = 1 to 1"  why? Why loop only once?
"Print" is not a VBS command and cannot be used in VBS.
\_(ツ)_/
 Edited by jrv Friday, December 1, 2017 11:10 PM

The following is what you have written minus all of the mistakes and unnecessary items:
For size = 64 to 8976 If size = 256 Then newsize = 511 ElseIf size = 528 Then newsize = 767 ElseIf size = 784 Then newsize = 1023 ElseIf size = 1040 Then newsize = 1279 ElseIf size = 1296 Then newsize = 1535 ElseIf size = 1552 Then newsize = 1791 ElseIf size = 1808 Then newsize = 2047 ElseIf size = 2064 Then newsize = 2303 ElseIf size = 2320 Then newsize = 2559 ElseIf size = 2576 Then newsize = 2815 ElseIf size = 2832 Then newsize = 3071 ElseIf size = 3088 Then newsize = 3327 ElseIf size = 3344 Then newsize = 3583 ElseIf size = 3600 Then newsize = 3839 ElseIf size = 3856 Then newsize = 4095 ElseIf size = 4112 Then newsize = 4351 ElseIf size = 4368 Then newsize = 4607 ElseIf size = 4624 Then newsize = 4863 ElseIf size = 4880 Then newsize = 5119 ElseIf size = 5136 Then newsize = 5375 ElseIf size = 5392 Then newsize = 5631 '... more Else newsize = 1 End If If newsize > 0 Then WScript.Echo "Size:" & size & " Newsize:" & newsize Next
A 'Case" statement would be even more compact. I would probably use a dictionary.
\_(ツ)_/
 Edited by jrv Friday, December 1, 2017 11:33 PM
