Demonstrate the short-circuit operators : Operators : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Operators »

 

Demonstrate the short-circuit operators









    

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the short-circuit operators. 
 
using System; 
 
public class SCops {    
  public static void Main() {    
    int n, d; 
 
    n = 10; 
    d = 2; 
    if(d != 0 && (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n); 
 
    d = 0; // now, set d to zero 
 
    // Since d is zero, the second operand is not evaluated. 
    if(d != 0 && (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n);  
     
    /* Now, try the same thing without short-circuit operator. 
       This will cause a divide-by-zero error.  */ 
    if(d != 0 & (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n); 
  }    
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Operators