Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Using if else in batch scripting  (Read 5072 times)

0 Members and 1 Guest are viewing this topic.

Yogesh123

    Topic Starter


    Beginner

    Using if else in batch scripting
    « on: September 16, 2009, 07:28:58 AM »
    How to check 3 conditions from batch scripting

    ex:-

    if (condition1) & (condition2) are true do this

    else do this

    Two-eyes



      Intermediate
    • Thanked: 4
      Re: Using if else in batch scripting
      « Reply #1 on: September 16, 2009, 09:46:57 AM »
      How about nesting ifs?  Something like:

      Code: [Select]
      IF condition1 (
        IF condition2 command
      ) ELSE command

      Two-Eyes %
      Quote
      I believe the bushes in my yard will BURN before God picks up a PC to send a message


      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Using if else in batch scripting
      « Reply #2 on: September 16, 2009, 10:15:40 AM »
      It depends. Are the three tests know to be valid tests? That is, do we already know that the test will give a True or False without raising an error.

      Nested IF statements are a good idea to prevent an invalid test problem.  An example of this is when you need to test for the presence of a file and if true use the file in some further test.

      Salmon Trout

      • Guest
      Re: Using if else in batch scripting
      « Reply #3 on: September 16, 2009, 12:31:35 PM »
      You can do simple if - else tests in batch; there is no THEN keyword. You keep the IF code and the ELSE code apart with parentheses. Remember that (a) the closing parenthesis of the code block to be executed if the initial IF test is passed, (b) the ELSE keyword, (c) the opening parenthesis of the code block to be executed if the ELSE is triggered, MUST ALL BE ON THE SAME LINE.

      Code: [Select]
      IF "%animal%"=="dog" (
          echo canine
          echo likes bones
           ) ELSE (
          echo not canine
          echo might not like bones
      )
      « Last Edit: September 16, 2009, 01:20:00 PM by Salmon Trout »

      Salmon Trout

      • Guest
      Re: Using if else in batch scripting
      « Reply #4 on: September 16, 2009, 12:47:42 PM »
      .

      Salmon Trout

      • Guest
      Re: Using if else in batch scripting
      « Reply #5 on: September 16, 2009, 01:10:31 PM »
      .

      Salmon Trout

      • Guest
      Re: Using if else in batch scripting
      « Reply #6 on: September 16, 2009, 01:47:36 PM »
      To do Boolean tests you have to do a bit of trickery

      Condition 1: animal is dog
      Condition 2: colour is red

      Code: [Select]
      REM AND
      set bool=0
      IF "%animal%"=="dog" set /a bool=%bool%+1
      IF "%colour%"=="red" set /a bool=%bool%+1
      if %bool% EQU 2 (
          echo condition 1 AND condition 2 are both true
      ) ELSE (
          echo condition 1 AND condition 2 are NOT both true
          )
         
      REM OR
      set bool=0
      IF "%animal%"=="dog" set /a bool=%bool%+1
      IF "%colour%"=="red" set /a bool=%bool%+1
      if %bool% GEQ 1 (
          echo Either condition 1 OR condition 2 is true
      ) ELSE (
          Neither condition 1 nor condition 2 is true
          )

      To avoid spoiling all your fun, I'll leave you to figure out NOT using this approach...
      « Last Edit: September 16, 2009, 02:00:41 PM by Salmon Trout »

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Using if else in batch scripting
      « Reply #7 on: September 16, 2009, 02:44:47 PM »
      Did you leave out an echo?
      Here is XOR  
      Code: [Select]
      if %bool% EQU 1 (
          echo  condition 1 XOR condition 2 is true
      ) ELSE (
          echo condition 1 XOR condition 2 is false
          )
      Which is what people think OR means!
      « Last Edit: September 16, 2009, 04:11:16 PM by Geek-9pm »

      Salmon Trout

      • Guest
      Re: Using if else in batch scripting
      « Reply #8 on: September 16, 2009, 03:41:42 PM »
      Did you leave out an echo?

      Yup

      Exclusive OR Venn diagram: the red part is true



      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Using if else in batch scripting
      « Reply #9 on: September 16, 2009, 08:35:37 PM »
      XOR is one or the other but not both.

      A XOR B = (NOT (A=B) and (A OR B))
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Using if else in batch scripting
      « Reply #10 on: September 16, 2009, 08:48:01 PM »
      Quote
      Care should be taken when converting an English sentence into a formal Boolean statement. Many English sentences have imprecise meanings, e.g. "All that glisters is not gold,"[1] which could mean that "nothing that glisters is gold" or "some things which glister are not gold".

      AND and OR can also be used interchangeably in English, in certain cases:

          * "I always carry an umbrella for when it rains and snows."

          * "I always carry an umbrella for when it rains or snows."

          * " I never walk in the rain or snow."
      :P
      http://en.wikipedia.org/wiki/Boolean_logic


      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Using if else in batch scripting
      « Reply #11 on: September 16, 2009, 08:52:43 PM »
      "I always carry an umbrella for when it rains and snows."

      this is valid because there is an implication:

      "I always carry an umbrella for when it rains and for when it snows."
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Using if else in batch scripting
      « Reply #12 on: September 16, 2009, 09:10:55 PM »
      "I always carry an umbrella for when it rains and snows."

      this is valid because there is an implication:

      "I always carry an umbrella for when it rains and for when it snows."
      OK, Maybe we can close this thread if you will just  write a batch file using the IF...ELSE structure to demonstrate the above logic.  :o

      Yogesh123

        Topic Starter


        Beginner

        Re: Using if else in batch scripting
        « Reply #13 on: September 16, 2009, 10:40:02 PM »
        Thanks Salmon Trout
        -----
        IF "%animal%"=="dog" set /a bool=%bool%+1
        IF "%colour%"=="red" set /a bool=%bool%+1
        if %bool% EQU 2 (
            echo condition 1 AND condition 2 are both true
        ) ELSE (
            echo condition 1 AND condition 2 are NOT both true
            )
        ---------
        This thing is really working great, now i can allow any no. of conditions in a IF loop.
        initially i thought like "C" language i can enter elseif condition.
        Thanks again.