Silly question - using OR and AND in IF statement

  • Thread starter Thread starter Don Quixote
  • Start date Start date
D

Don Quixote

Hi,

I used to know this but totally forgot how to do that:
How do you apply OR and AND conditions in IF statements, as in:

------------------------
set /p name="what is your name?"

if /i %name%==("danny" OR "tammy") echo I know you
-------------------------

I know "OR" isn't supposed to be there, but I thought it used to be
the double-pipe symbol (||), and double ambersand (&&) for AND, but I
think I might be confusing that with C-scripting.

Any ideas on how to make these type of conditions (on a single line,
that is)?

Thanks.
 
I used to know this but totally forgot how to do that:
How do you apply OR and AND conditions in IF statements, as in:

Maybe their are better ways but these work:

R:\>if x==x @echo Hi
Hi

R:\>if x==x if x==y @echo Hi

R:\>if not x==x if x==y @echo Hi

R:\>if x==x if not x==y @echo Hi
Hi

R:\>if x==y (@echo Hi) else (if x==x @echo Hi)
Hi
 
Hi,

I used to know this but totally forgot how to do that:
How do you apply OR and AND conditions in IF statements, as in:

------------------------
set /p name="what is your name?"

if /i %name%==("danny" OR "tammy") echo I know you
-------------------------

I know "OR" isn't supposed to be there, but I thought it used to be
the double-pipe symbol (||), and double ambersand (&&) for AND, but I
think I might be confusing that with C-scripting.

Any ideas on how to make these type of conditions (on a single line,
that is)?

There is no OR mechanism. You'd normally just repeat the IF tests for
each condition.

if /i %name%==danny echo I know you
if /i %name%==tammy echo I know you

You can combine it into a single line but it's basically the same
thing.

(if /i %name%==danny echo I know you) & if /i %name%==tammy echo I
know you

Similarly, you can fold the tests into a FOR loop.

for %%a in (danny tammy) do if /i %name%==%%a echo I know you

If you had a large list to check, you could use FIND to check for a
match.

echo danny tammy | find /i "%name% " > nul && echo I know you

An AND construct is created by repeating the IF test.

set name=danny
set group=users
if /i %name%==danny if /i %group%==users echo Both conditions
satisfied

Garry
 
Back
Top