locked
Order of execution in an if statement

    Question

  • Does the C# language definition guarantee that in the line

    if ( func_one() && func_two() )

    1) func_one() will be executed first
    2) func_two() will only be executed if and only if func_one() returns true

    My C# book evades the issue.
    Friday, December 21, 2007 1:58 PM

Answers

  • Yes C# does short circuit evaluation of boolean expressions.  Therefore in any boolean expression of type x op y, x is evaluated first and y is evaluated only if it is meaningful to do so.  In the case of an AND operation if x is false then y is not evaluated.  For OR if x is true then y is not evaluated. 

     

    This applies to all boolean expressions and not just those in an IF statement.  This is covered under section 14.11.1 of the C# Specification available online at MSDN.

     

    Michael Taylor - 12/21/07

    http://p3net.mvps.org

     

     

    Friday, December 21, 2007 2:12 PM
  • If you key VS2005 help Index for && symbol the explanation will be explicitly as to what you outlined in the first post.

     

    Saturday, December 22, 2007 12:02 AM

All replies

  • Yes C# does short circuit evaluation of boolean expressions.  Therefore in any boolean expression of type x op y, x is evaluated first and y is evaluated only if it is meaningful to do so.  In the case of an AND operation if x is false then y is not evaluated.  For OR if x is true then y is not evaluated. 

     

    This applies to all boolean expressions and not just those in an IF statement.  This is covered under section 14.11.1 of the C# Specification available online at MSDN.

     

    Michael Taylor - 12/21/07

    http://p3net.mvps.org

     

     

    Friday, December 21, 2007 2:12 PM
  • yes that's rigth this brief example can prove it.

    Code Snippet

     

    class Program

    {

    public static bool method1(bool valor)

    {

    Console.WriteLine("Method value {0}", valor.ToString());

    return valor;

    }

    static void Main(string[] args)

    {

    if(method1(false) && method1(true))

    Console.WriteLine("OK");

     

    Console.WriteLine("---------");

     

    if (method1(true) && method1(false))

    Console.WriteLine("OK");

     

    Console.Read();

    }

    }

     

     

     

    try to compile in visual studio, csc, or mono compiler and the result should be exactly the same.

    Friday, December 21, 2007 2:45 PM
  • Thank you both.

    I realise that it does evaluate them in order, but I wanted this future-proofed and portable. I had a nasty experience with a language called Powerbuilder that did not guarantee the order - at least not then.

    The expression I needed was 'short-circuit'. I have now found the section in Hejlsberg, Wiltamuth, Gold - the ultimate bathroom reader.

    The forums today are sufficiently slow that I can get a decent amount of work donw while waiting for something to post. (Or is it just me?)
    Friday, December 21, 2007 3:15 PM
  • If you key VS2005 help Index for && symbol the explanation will be explicitly as to what you outlined in the first post.

     

    Saturday, December 22, 2007 12:02 AM