Sign in to follow this  
Followers 0
kor

multiple And'ss and Or's in IF statement

12 posts in this topic

#1 ·  Posted (edited)

so I'm not sure the best way to approach this.

I have about 10 var's.

Lets just say $1, $2, $3.... $9, $10

I need a more efficient way to express this.

If $1 <> "" And $2 <> "" And $3 <> "" And $4 <> "" And $5 <> "" Then
; do something
ElseIf $1 = "" And $2 = "" And $3 = "" And $6 <> "" And $7 <> "" And $8 <> "" And $9 <> "" Then
; do something else
Else
; something different
EndIf

Basically I am trying to accomplish this:

If vars $1 through $5 are not blank, then do something

else if vars $6 through $9 are not blank AND vars $1 though $5 ARE blank, do something else

else if ALL vars are not blank then do something else

else do something

I dont know how to use If Else statements with multiple vars without having to do all the And's over and over again.

EDIT: another approach?

If $1 & $2 & $3 & $4 & $5 & $6 <> "" And $7 & $8 & $9 & $10 & $11 = "" Then
        $action1 = True
        $action2 = False
    ElseIf $1 & $2 & $3 & $4 & $5 & $6 = "" And $7 & $8 & $9 & $10 & $11 <> "" Then
        $action1 = False
        $action2 = True
    Else
        MsgBox(16, "Error", "All fields are required")
    EndIf
Edited by kor

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Dim $1,$2,$3,$4,$5,$6,$7,$8,$9
;Test 1
$1 = "aa"
Select
    Case $1&$2&$3&$4&$5
        MsgBox(0,"1","Theres something inside")
    Case Not $1&$2&$3&$4&$5 And $6&$7&$8&$9
        MsgBox(0,"2","6-9")
    Case Not $1&$2&$3&$4&$5&$6&$7&$8&$9
        MsgBox(0,"1","NULL")
EndSelect
$1 = "" ;reset
;test 2
$6 = 1
Select
    Case $1&$2&$3&$4&$5
        MsgBox(0,"2","Theres something inside")
    Case Not $1&$2&$3&$4&$5 And $6&$7&$8&$9
        MsgBox(0,"2","6-9")
    Case Not $1&$2&$3&$4&$5&$6&$7&$8&$9
        MsgBox(0,"2","NULL")
EndSelect
$6 = "" ;reset
;test 3
Select
    Case $1&$2&$3&$4&$5
        MsgBox(0,"3","Theres something inside")
    Case Not $1&$2&$3&$4&$5 And $6&$7&$8&$9
        MsgBox(0,"2","6-9")
    Case Not $1&$2&$3&$4&$5&$6&$7&$8&$9
        MsgBox(0,"3","NULL")
EndSelect

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

I'm having trouble seeing what your case statements are actually doing.

Share this post


Link to post
Share on other sites

How about using an array?

Global $aVars[10] = ["","","s","","","s","s","s","","s"]

For $i = 0 To 4
    If $aVars[$i] <> "" Then
        MsgBox(0,'Msg','Variable ' & $i & ' is Not ""')
    EndIf
Next

For $i = 5 To 9
    If $aVars[$i] = "" Then
        MsgBox(0,'Msg','Variable ' & $i & ' is ""')
    EndIf
Next

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

I like it! Arrays seem to be the way to go in this instance.

Share this post


Link to post
Share on other sites

I think I should mention that my vars are coming from GUICtrlCreateInput's, which aren't blank per se. here is my code.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiComboBox.au3>
#Include <Array.au3>

Global $staffvars[6]

$gui = GUICreate("Account Creator", 280, 275, 320, 227) ; left, top, width, height 
$PageControl1 = GUICtrlCreateTab(8, 8, 263, 225)

; Staff Tab
$stafftab = GUICtrlCreateTabItem("Staff Account")
GUICtrlCreateLabel("First Name:", 18, 38, 65, 20) ; left, top, width, height
$staffvars[0] = GUICtrlCreateInput("", 90, 36, 125, 20)
GUICtrlCreateLabel("Last Name:", 18, 63, 65, 20) ; left, top, width, height
$staffvars[1] = GUICtrlCreateInput("", 90, 61, 125, 20)
GUICtrlCreateLabel("Staff ID:", 18, 89, 65, 20) ; left, top, width, height
$staffvars[2] = GUICtrlCreateInput("", 90, 86, 125, 20) 
GUICtrlCreateLabel("Location:", 18, 114, 65, 20) ; left, top, width, height
$staffvars[3] = GUICtrlCreateCombo("", 90, 111, 125, 18, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "example|example2")
GUICtrlCreateLabel("Job Title:", 18, 139, 65, 20) ; left, top, width, height
$staffvars[4] = GUICtrlCreateInput("", 90, 137, 125, 20)    
GUICtrlCreateLabel("Classification:", 18, 164, 65, 20) ; left, top, width, height
$staffvars[5] = GUICtrlCreateCombo("", 90, 162, 125, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "1|2")

; Student Tab
$studenttab = GUICtrlCreateTabItem("another Account")

; Below
GUICtrlCreateTabItem("")
$create = GUICtrlCreateButton("&Create", 8, 242, 75, 25)
$reset = GUICtrlCreateButton("&Reset", 91, 242, 75, 25)
$exit = GUICtrlCreateButton("&Exit", 196, 242, 75, 25)
GUISetState(@SW_SHOW)

$msg = 0
While $msg <> $GUI_EVENT_CLOSE
    $msg = GUIGetMsg() 
    Select
        Case $msg = $create
            If GUICtrlRead($PageControl1) = 0 Then ; read what tab you are one
                _CreateStaff()
            ElseIf GUICtrlRead($PageControl1) = 1 Then
                _Createanother()
            Else
                msgbox(0, "error", "error creating")
            EndIf
        Case $msg = $reset
            _SelfRestart()
        Case $msg = $exit
            Exit
    EndSelect
WEnd

Func _SelfRestart()
    If @Compiled Then
        Run(FileGetShortName(@ScriptFullPath))
    Else
        Run(FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
    EndIf
    Exit
EndFunc ;==> _SelfRestart

Func _CreateStaff()
    _ArrayDisplay($staffvars)
    ConsoleWrite(GUICtrlRead($staffvars[0], 1) & @CR)
;   For $i = 0 To 5
;       If $staffvars[$i] = "" Then
;           MsgBox(0,'Msg','Variable ' & $i & ' is Not ""')
;       EndIf
;   Next
EndFunc ;==> _Create

I'm trying to read all the inputs of the array, but by default the GUICtrlCreateInput returns the ID number of the input. I am stuck on how to do the GUICtrlRead($staffvars[$i]) in a loop to make sure all the values are not blank.

Share this post


Link to post
Share on other sites

usage on 2D array

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiComboBox.au3>
#Include <Array.au3>

Global $staffvars[6][2]
$gui = GUICreate("Account Creator", 280, 275, 320, 227) ; left, top, width, height 
$PageControl1 = GUICtrlCreateTab(8, 8, 263, 225)

; Staff Tab
$stafftab = GUICtrlCreateTabItem("Staff Account")
GUICtrlCreateLabel("First Name:", 18, 38, 65, 20) ; left, top, width, height
$staffvars[0][0] = GUICtrlCreateInput("", 90, 36, 125, 20)
GUICtrlCreateLabel("Last Name:", 18, 63, 65, 20) ; left, top, width, height
$staffvars[1][0] = GUICtrlCreateInput("", 90, 61, 125, 20)
GUICtrlCreateLabel("Staff ID:", 18, 89, 65, 20) ; left, top, width, height
$staffvars[2][0] = GUICtrlCreateInput("", 90, 86, 125, 20) 
GUICtrlCreateLabel("Location:", 18, 114, 65, 20) ; left, top, width, height
$staffvars[3][0] = GUICtrlCreateCombo("", 90, 111, 125, 18, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "example|example2")
GUICtrlCreateLabel("Job Title:", 18, 139, 65, 20) ; left, top, width, height
$staffvars[4][0] = GUICtrlCreateInput("", 90, 137, 125, 20)    
GUICtrlCreateLabel("Classification:", 18, 164, 65, 20) ; left, top, width, height
$staffvars[5][0] = GUICtrlCreateCombo("", 90, 162, 125, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "1|2")

; Student Tab
$studenttab = GUICtrlCreateTabItem("another Account")

; Below
GUICtrlCreateTabItem("")
$create = GUICtrlCreateButton("&Create", 8, 242, 75, 25)
$reset = GUICtrlCreateButton("&Reset", 91, 242, 75, 25)
$exit = GUICtrlCreateButton("&Exit", 196, 242, 75, 25)
GUISetState(@SW_SHOW)

$msg = 0
While $msg <> $GUI_EVENT_CLOSE
    $msg = GUIGetMsg() 
    Select
        Case $msg = $create
            If GUICtrlRead($PageControl1) = 0 Then ; read what tab you are one
                _CreateStaff()
            ElseIf GUICtrlRead($PageControl1) = 1 Then
                _Createanother()
            Else
                msgbox(0, "error", "error creating")
            EndIf
        Case $msg = $reset
            _SelfRestart()
        Case $msg = $exit
            Exit
    EndSelect
WEnd

Func _SelfRestart()
    If @Compiled Then
        Run(FileGetShortName(@ScriptFullPath))
    Else
        Run(FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
    EndIf
    Exit
EndFunc ;==> _SelfRestart

Func _CreateStaff()
    For $x = 0 To 5
        $staffvars[$x][1] = GUICtrlRead($staffvars[$x][0])
        If $staffvars[$x][1] Then
            MsgBox(0,'Msg','Variable ' & $x & ' is '&$staffvars[$x][1])
        Else
            MsgBox(0,'Msg','Variable ' & $x & ' is NULL')
        EndIf
    Next
EndFunc ;==> _Create

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

Your 2D array makes the error pop up for EACH variable that is blank. So if a user has 3 vars blank the same error pops up 3 times. How can I remove such that the error only pops up once if ANY of the fields are blank. I've tried removing the If statement from the loop but I'm not having much luck.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ofc it is

its taken form your code

; For $i = 0 To 5

; If $staffvars[$i] = "" Then

; MsgBox(0,'Msg','Variable ' & $i & ' is Not ""')

; EndIf

; Next

So its not my array, its your code :)

Personally i don`t like to use loop for things like this.

Func _CreateStaff()
    Local $IsIt
    For $x = 0 To 5
        If Not GUICtrlRead($staffvars[$x][0]) Then $IsIt = 1
    Next
    If $IsIt Then MsgBox(16,"An error occupied","Please fill out all the fields before using create button again.")
EndFunc ;==> _CreateStaff

Set the var to some data if any field is empty, if var have some data popup msgbox

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

How is it that you have an IF statement with nothing after it.

If $Isis then

What are you comparing the var to, for the If Then statement.

I see Local $isIt which creates the variable, and your If Not statement sets the value to 1

But I don't understand the next line which doesn't compare $Isit to anything.

I'm also not seeing where you close the If Then statement with an EndIf. How is it that is legal?

Edited by kor

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

m not comparing im testing expression

Parameters

expression If the expression is true, the first statement block is executed.

Look at help file example (If...ElseIf...Else...EndIf ) and youl see

If StringIsXDigit ($var) Then

Local $isIt

Declaration default value is Null or "" or 0 (call it in your brain whatever you like)

So

If Not GUICtrlRead($staffvars[$x][0])

is testing the GUICtrlRead($staffvars[$x][0]) does it contain anything or its only NULL so if its Null then sets declared variable value to 1

'If Then' dont need to b closed with EndIf if its in one line statment

Look in the help file (If...Then) example and youl see

If $CmdLine[0] = 0 Then Exit

Remarks

This version of the If statement is used to execute a single statement without the overhead of an EndIf.

For someone that is heare from 03-April 09 you realy need to read more the help file in some situations (im still reading it all the time :) he is my best friend :) every day i learn something new) :P Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

My personal preference would be to stick with your original code (first version) so log as the number of variables to be checked does not get ridiculous.

My reasons would be.

  • The intention of the code is clear.
  • It will shortcircuit as soon as one of the comparisons fail.
  • The order of the comparisons optimized to the order most likely to fail first.
  • Less code is not always better.
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0